Skip to content

Commit 99925e7

Browse files
committed
fix logging support
1 parent 16c4730 commit 99925e7

File tree

23 files changed

+228
-116
lines changed

23 files changed

+228
-116
lines changed

exercises/03.sampling/01.problem.simple/README.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ void agent.server.server.sendLoggingMessage({
8585
Check the specification for more info:
8686
https://modelcontextprotocol.io/specification/2025-06-18/server/utilities/logging#log-message-notifications
8787

88+
To be able to send a logging message, you need to first advertise that we support logging and add support for the `SetLevelRequestSchema` request. You'll do this in <InlineFile file="./src/index.ts" />. As a general reminder, here's how you add request handlers:
89+
90+
```ts
91+
// this.server.server to access the underlying server instance
92+
this.server.server.setRequestHandler(
93+
RequestSchema, // this is the request schema you're adding support for
94+
async (request) => {
95+
// do something with the request
96+
return // return the appropriate response
97+
},
98+
)
99+
```
100+
101+
---
102+
88103
This step will help you get comfortable with the basic request/response flow for
89104
sampling in MCP, and set the stage for more advanced prompt engineering in the
90105
next step.

exercises/03.sampling/01.problem.simple/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
22
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
3+
// 💰 you'll need this:
4+
// import { SetLevelRequestSchema } from '@modelcontextprotocol/sdk/types.js'
35
import { DB } from './db/index.ts'
46
import { initializePrompts } from './prompts.ts'
57
import { initializeResources } from './resources.ts'
68
import { initializeTools } from './tools.ts'
79

810
export class EpicMeMCP {
911
db: DB
12+
// 🐨 add a state object with a loggingLevel property set to 'info'
1013
server = new McpServer(
1114
{
1215
name: 'epicme',
@@ -35,6 +38,9 @@ You can also help users add tags to their entries and get all tags for an entry.
3538
this.db = DB.getInstance(path)
3639
}
3740
async init() {
41+
// 🐨 add logging handler with this.server.server.setRequestHandler and the SetLevelRequestSchema
42+
// 🐨 the callback should set the loggingLevel in the state from request.params.level and return an empty object
43+
// 🦉 this is annoying, and hopefully can be managed by the SDK in the future: https://github.com/modelcontextprotocol/typescript-sdk/issues/871
3844
await initializeTools(this)
3945
await initializeResources(this)
4046
await initializePrompts(this)

exercises/03.sampling/01.problem.simple/src/sampling.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ export async function suggestTagsSampling(agent: EpicMeMCP, entryId: number) {
1313
// 🐨 logging message to send the model response to the client
1414
// 📜 https://modelcontextprotocol.io/specification/2025-06-18/server/utilities/logging#log-message-notifications
1515
// 💰 agent.server.server.sendLoggingMessage (with level of 'info', logger of 'sampling', and data with the model response)
16+
// 💰 only send the logging message if the agent.state.loggingLevel is 'debug' or 'info'
17+
// 🦉 This is kind of annoying, and hopefully can be managed by the SDK in the future: https://github.com/modelcontextprotocol/typescript-sdk/issues/871
1618
}

exercises/03.sampling/01.solution.simple/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
22
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
3+
import { SetLevelRequestSchema } from '@modelcontextprotocol/sdk/types.js'
34
import { DB } from './db/index.ts'
45
import { initializePrompts } from './prompts.ts'
56
import { initializeResources } from './resources.ts'
67
import { initializeTools } from './tools.ts'
78

89
export class EpicMeMCP {
910
db: DB
11+
state = { loggingLevel: 'info' }
1012
server = new McpServer(
1113
{
1214
name: 'epicme',
@@ -35,6 +37,13 @@ You can also help users add tags to their entries and get all tags for an entry.
3537
this.db = DB.getInstance(path)
3638
}
3739
async init() {
40+
this.server.server.setRequestHandler(
41+
SetLevelRequestSchema,
42+
async (request) => {
43+
this.state.loggingLevel = request.params.level
44+
return {}
45+
},
46+
)
3847
await initializeTools(this)
3948
await initializeResources(this)
4049
await initializePrompts(this)

exercises/03.sampling/01.solution.simple/src/sampling.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ Please respond with a proper commendation for yourself.
3030
maxTokens: 10,
3131
})
3232

33-
void agent.server.server.sendLoggingMessage({
34-
level: 'info',
35-
logger: 'tag-generator',
36-
data: {
37-
message: 'Received response from model',
38-
modelResponse: result.content.text,
39-
},
40-
})
33+
if (['debug', 'info'].includes(agent.state.loggingLevel)) {
34+
void agent.server.server.sendLoggingMessage({
35+
level: 'info',
36+
logger: 'tag-generator',
37+
data: {
38+
message: 'Received response from model',
39+
modelResponse: result.content.text,
40+
},
41+
})
42+
}
4143
}

exercises/03.sampling/02.problem.advanced/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
22
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
3+
import { SetLevelRequestSchema } from '@modelcontextprotocol/sdk/types.js'
34
import { DB } from './db/index.ts'
45
import { initializePrompts } from './prompts.ts'
56
import { initializeResources } from './resources.ts'
67
import { initializeTools } from './tools.ts'
78

89
export class EpicMeMCP {
910
db: DB
11+
state = { loggingLevel: 'info' }
1012
server = new McpServer(
1113
{
1214
name: 'epicme',
@@ -35,6 +37,13 @@ You can also help users add tags to their entries and get all tags for an entry.
3537
this.db = DB.getInstance(path)
3638
}
3739
async init() {
40+
this.server.server.setRequestHandler(
41+
SetLevelRequestSchema,
42+
async (request) => {
43+
this.state.loggingLevel = request.params.level
44+
return {}
45+
},
46+
)
3847
await initializeTools(this)
3948
await initializeResources(this)
4049
await initializePrompts(this)

exercises/03.sampling/02.problem.advanced/src/sampling.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,17 @@ Please respond with a proper commendation for yourself.
9090
.map((id) => allTags.find((t) => t.id === id))
9191
.filter(Boolean)
9292

93-
void agent.server.server.sendLoggingMessage({
94-
level: 'info',
95-
logger: 'tag-generator',
96-
data: {
97-
message: 'Added tags to entry',
98-
addedTags,
99-
entry: updatedEntry,
100-
},
101-
})
93+
if (['debug', 'info'].includes(agent.state.loggingLevel)) {
94+
void agent.server.server.sendLoggingMessage({
95+
level: 'info',
96+
logger: 'tag-generator',
97+
data: {
98+
message: 'Added tags to entry',
99+
addedTags,
100+
entry: updatedEntry,
101+
},
102+
})
103+
}
102104
}
103105

104106
const existingTagSchema = z.object({ id: z.number() })

exercises/03.sampling/02.solution.advanced/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
22
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
3+
import { SetLevelRequestSchema } from '@modelcontextprotocol/sdk/types.js'
34
import { DB } from './db/index.ts'
45
import { initializePrompts } from './prompts.ts'
56
import { initializeResources } from './resources.ts'
67
import { initializeTools } from './tools.ts'
78

89
export class EpicMeMCP {
910
db: DB
11+
state = { loggingLevel: 'info' }
1012
server = new McpServer(
1113
{
1214
name: 'epicme',
@@ -35,6 +37,13 @@ You can also help users add tags to their entries and get all tags for an entry.
3537
this.db = DB.getInstance(path)
3638
}
3739
async init() {
40+
this.server.server.setRequestHandler(
41+
SetLevelRequestSchema,
42+
async (request) => {
43+
this.state.loggingLevel = request.params.level
44+
return {}
45+
},
46+
)
3847
await initializeTools(this)
3948
await initializeResources(this)
4049
await initializePrompts(this)

exercises/03.sampling/02.solution.advanced/src/sampling.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,17 @@ If you have some suggestions, respond with an array of tag objects. Existing tag
8585
.map((id) => allTags.find((t) => t.id === id))
8686
.filter(Boolean)
8787

88-
void agent.server.server.sendLoggingMessage({
89-
level: 'info',
90-
logger: 'tag-generator',
91-
data: {
92-
message: 'Added tags to entry',
93-
addedTags,
94-
entry: updatedEntry,
95-
},
96-
})
88+
if (['debug', 'info'].includes(agent.state.loggingLevel)) {
89+
void agent.server.server.sendLoggingMessage({
90+
level: 'info',
91+
logger: 'tag-generator',
92+
data: {
93+
message: 'Added tags to entry',
94+
addedTags,
95+
entry: updatedEntry,
96+
},
97+
})
98+
}
9799
}
98100

99101
const existingTagSchema = z.object({ id: z.number() })

exercises/04.long-running-tasks/01.problem.progress/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
22
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
3+
import { SetLevelRequestSchema } from '@modelcontextprotocol/sdk/types.js'
34
import { DB } from './db/index.ts'
45
import { initializePrompts } from './prompts.ts'
56
import { initializeResources } from './resources.ts'
67
import { initializeTools } from './tools.ts'
78

89
export class EpicMeMCP {
910
db: DB
11+
state = { loggingLevel: 'info' }
1012
server = new McpServer(
1113
{
1214
name: 'epicme',
@@ -35,6 +37,13 @@ You can also help users add tags to their entries and get all tags for an entry.
3537
this.db = DB.getInstance(path)
3638
}
3739
async init() {
40+
this.server.server.setRequestHandler(
41+
SetLevelRequestSchema,
42+
async (request) => {
43+
this.state.loggingLevel = request.params.level
44+
return {}
45+
},
46+
)
3847
await initializeTools(this)
3948
await initializeResources(this)
4049
await initializePrompts(this)

0 commit comments

Comments
 (0)