Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
107767a
save commit
KKonstantinov Dec 7, 2025
68ff665
context API - backwards compatible introduction
KKonstantinov Dec 7, 2025
f58b491
fixes
KKonstantinov Dec 7, 2025
459bff4
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov Dec 7, 2025
a23e2f2
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov Dec 7, 2025
4ff84a1
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov Dec 8, 2025
e89d9d4
moved properties under objects
KKonstantinov Dec 8, 2025
187a3cd
prettier fix
KKonstantinov Dec 8, 2025
96169b3
move logger methods under loggingNotification
KKonstantinov Dec 8, 2025
a57840c
merge commit
KKonstantinov Dec 9, 2025
161f584
merge commit - v2
KKonstantinov Dec 23, 2025
d5f5047
merge commit - v2
KKonstantinov Dec 23, 2025
109dc52
base context, client context, server context separation
KKonstantinov Jan 21, 2026
deca7fd
rename method to createRequestContext, update docs
KKonstantinov Jan 21, 2026
36be75e
fix server conformance
KKonstantinov Jan 21, 2026
f609478
fix conformance
KKonstantinov Jan 21, 2026
3c4f9d8
move types
KKonstantinov Jan 21, 2026
2c31eb2
merge commit
KKonstantinov Jan 22, 2026
86549fa
rename extra vars to ctx
KKonstantinov Jan 22, 2026
920da7e
prettier fix
KKonstantinov Jan 22, 2026
639d7bd
add changeset
KKonstantinov Jan 22, 2026
ae9c253
merge commit
KKonstantinov Jan 23, 2026
a5704c5
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov Jan 23, 2026
f39dc4e
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov Jan 26, 2026
cb4c500
switch server and client to work with ServerContextInterface and Clie…
KKonstantinov Jan 27, 2026
f5b27d4
Merge branch 'feature/ctx-in-callbacks' of github.com:KKonstantinov/t…
KKonstantinov Jan 27, 2026
9b0ed8d
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov Jan 29, 2026
7767079
Merge branch 'main' of github.com:modelcontextprotocol/typescript-sdk…
KKonstantinov Feb 2, 2026
f765e07
update context interface
KKonstantinov Feb 2, 2026
184dbca
Merge branch 'feature/ctx-in-callbacks' of github.com:KKonstantinov/t…
KKonstantinov Feb 2, 2026
69b1ae6
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov Feb 2, 2026
9132b5a
update migration docs
KKonstantinov Feb 2, 2026
08da5e0
merge commit
KKonstantinov Feb 3, 2026
5b05ff6
merge commit
KKonstantinov Feb 3, 2026
ebac5d4
update conformance ctx - sampling fail
KKonstantinov Feb 3, 2026
b79763f
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov Feb 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/hot-trees-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@modelcontextprotocol/express': patch
'@modelcontextprotocol/hono': patch
'@modelcontextprotocol/node': patch
'@modelcontextprotocol/eslint-config': patch
'@modelcontextprotocol/test-integration': patch
'@modelcontextprotocol/client': patch
'@modelcontextprotocol/server': patch
'@modelcontextprotocol/core': patch
---

add context API to tool, prompt, resource callbacks, linting
72 changes: 57 additions & 15 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ When making breaking changes, document them in **both**:
- `docs/migration.md` — human-readable guide with before/after code examples
- `docs/migration-SKILL.md` — LLM-optimized mapping tables for mechanical migration

Include what changed, why, and how to migrate. Search for related sections and group
related changes together rather than adding new standalone sections.
Include what changed, why, and how to migrate. Search for related sections and group related changes together rather than adding new standalone sections.

## Code Style Guidelines

Expand Down Expand Up @@ -146,37 +145,71 @@ When a request arrives from the remote side:
2. **`Protocol.connect()`** routes to `_onrequest()`, `_onresponse()`, or `_onnotification()`
3. **`Protocol._onrequest()`**:
- Looks up handler in `_requestHandlers` map (keyed by method name)
- Creates `RequestHandlerExtra` with `signal`, `sessionId`, `sendNotification`, `sendRequest`
- Creates a context object (`ServerContext` or `ClientContext`) via `createRequestContext()`
- Invokes handler, sends JSON-RPC response back via transport
4. **Handler** was registered via `setRequestHandler('method', handler)`

### Handler Registration

```typescript
// In Client (for server→client requests like sampling, elicitation)
<<<<<<< HEAD
client.setRequestHandler(CreateMessageRequestSchema, async (request, ctx) => {
=======
client.setRequestHandler('sampling/createMessage', async (request, extra) => {
>>>>>>> f6e8204b8621f55ee08c4f8cb8b2b85eff104842
// Handle sampling request from server
return { role: "assistant", content: {...}, model: "..." };
});

// In Server (for client→server requests like tools/call)
<<<<<<< HEAD
server.setRequestHandler(CallToolRequestSchema, async (request, ctx) => {
=======
server.setRequestHandler('tools/call', async (request, extra) => {
>>>>>>> f6e8204b8621f55ee08c4f8cb8b2b85eff104842
// Handle tool call from client
return { content: [...] };
});
```

### Request Handler Extra

The `extra` parameter in handlers (`RequestHandlerExtra`) provides:

- `signal`: AbortSignal for cancellation
- `sessionId`: Transport session identifier
- `authInfo`: Validated auth token info (if authenticated)
- `requestId`: JSON-RPC message ID
- `sendNotification(notification)`: Send related notification back
- `sendRequest(request, schema)`: Send related request (for bidirectional flows)
- `taskStore`: Task storage interface (if tasks enabled)
### Request Handler Context

The `ctx` parameter in handlers provides a structured context with grouped fields:

**Common structure (both Client and Server)**:

- `ctx.sessionId`: Transport session identifier (top-level)
- `ctx.mcpReq`: MCP protocol context
- `id`: JSON-RPC message ID
- `method`: The method being called
- `_meta`: Request metadata
- `signal`: AbortSignal for cancellation
- `send(request, schema, options?)`: Send request (for bidirectional flows)
- `ctx.http`: HTTP request context (optional, present for HTTP transports)
- `authInfo`: Validated auth token info (if authenticated)
- `ctx.task`: Task context (when tasks are enabled)
- `id`: Current task ID (updates after `store.createTask()`)
- `store`: Request-scoped task store (`RequestTaskStore`)
- `requestedTtl`: Requested TTL for the task
- `ctx.notification`: Notification context
- `send(notification)`: Send notification back

**Server-specific additions**:

- `ctx.http`: Extended with additional fields
- `req`: Raw fetch Request object (access to URL, headers, etc.)
- `closeSSE?()`: Close SSE stream for polling
- `closeStandaloneSSE?()`: Close standalone SSE stream
- `ctx.mcpReq`: Extended with server-to-client request methods
- `requestSampling(params, options?)`: Request sampling from client
- `elicitInput(params, options?)`: Request user input from client
- `ctx.notification`: Extended with logging methods
- `log(params)`: Send logging notification
- `debug(message, extraLogData?)`: Send debug log
- `info(message, extraLogData?)`: Send info log
- `warning(message, extraLogData?)`: Send warning log
- `error(message, extraLogData?)`: Send error log

### Capability Checking

Expand Down Expand Up @@ -207,7 +240,11 @@ const result = await server.createMessage({
});

// Client must have registered handler:
<<<<<<< HEAD
client.setRequestHandler(CreateMessageRequestSchema, async (request, ctx) => {
=======
client.setRequestHandler('sampling/createMessage', async (request, extra) => {
>>>>>>> f6e8204b8621f55ee08c4f8cb8b2b85eff104842
// Client-side LLM call
return { role: "assistant", content: {...} };
});
Expand All @@ -218,8 +255,13 @@ client.setRequestHandler('sampling/createMessage', async (request, extra) => {
### Request Handler Registration (Low-Level Server)

```typescript
<<<<<<< HEAD
server.setRequestHandler(SomeRequestSchema, async (request, ctx) => {
// ctx provides mcpCtx, requestCtx, task, sendNotification, sendRequest
=======
server.setRequestHandler('tools/call', async (request, extra) => {
// extra contains sessionId, authInfo, sendNotification, etc.
>>>>>>> f6e8204b8621f55ee08c4f8cb8b2b85eff104842
return {
/* result */
};
Expand All @@ -229,7 +271,7 @@ server.setRequestHandler('tools/call', async (request, extra) => {
### Tool Registration (High-Level McpServer)

```typescript
mcpServer.tool('tool-name', { param: z.string() }, async ({ param }, extra) => {
mcpServer.tool('tool-name', { param: z.string() }, async ({ param }, ctx) => {
return { content: [{ type: 'text', text: 'result' }] };
});
```
Expand Down
Loading
Loading