-
Notifications
You must be signed in to change notification settings - Fork 1.6k
v2 Context API in callbacks
#1241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
v2 Context API in callbacks
#1241
Conversation
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
mattzcarey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
big fan
🦋 Changeset detectedLatest commit: a5704c5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 8 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Will now have to be a Since we can now afford breaking changes, will rework Note to self: original context interface was done pre-tasks, need a type generic version with is aware when taskStore, taskId etc. is there so that the end user does not need to check on property existence in tasks handlers TODO:
Edit: All todos done |
v2 Context API in callbacks
This PR introduces a new Context API for request handlers in the TypeScript SDK, replacing the flat
RequestHandlerExtratype with a structured, extensible context system.The new context provides:
mcpCtx,requestCtx,taskCtx)ServerContext) and client (ClientContext) handlersMotivation and Context
The previous
RequestHandlerExtrahad several design issues:1. POJO Creation Overhead
Every incoming request created a new plain JavaScript object with all properties and methods inline:
This approach has performance implications:
sendNotificationandsendRequestarrow functions are created as new function objects for every request, each capturing variables from the enclosing scope (request,relatedTaskId,taskStore,this). These closures are heap allocations that add GC pressure.The new Context API uses class instances with methods on the prototype. While both approaches allocate a new object per request, the class approach avoids per-request function allocations by referencing shared prototype methods instead.
2. Mixed Concerns: Properties Irrelevant to Client or Server
RequestHandlerExtrawas a single type used by bothClientandServer, but many properties only applied to one side:requestInfocloseSSEStreamcloseStandaloneSSEStreamauthInfotaskId,taskStore,taskRequestedTtlThis led to:
closeSSEStreamin autocomplete but it's always undefined?), making the type less useful for type checkingThe new Context API uses separate types (
ServerContext,ClientContext) with appropriate properties for each, and nested structures (requestCtx.stream) to group related optional features.3. Flat Structure Limits Extensibility
Adding new features to
RequestHandlerExtrameant:The new nested structure (
mcpCtx,requestCtx,taskCtx) provides clear namespaces for different concerns, making it easy to add features without polluting the top-level API.4. No Convenience Methods
Common operations like logging, sampling, and elicitation required manual construction:
The new Context API introduces a clean separation:
ctx.mcpCtx- MCP protocol-level context (requestId, method, sessionId, _meta)ctx.requestCtx- Transport/request-level context (signal, authInfo, and server-specific: uri, headers, stream controls)ctx.taskCtx- Task context when tasks are enabled (id, store, requestedTtl)sendNotification(),sendRequest()directly on the contextloggingNotification,requestSampling(),elicitInput()onServerContextHow Has This Been Tested?
test/integration/test/server/context.test.tsmcpCtx,requestCtx,taskCtx)Breaking Changes
This is a breaking change for code that accesses properties directly on the handler's second parameter:
extra._metactx.mcpCtx._metaextra.sessionIdctx.mcpCtx.sessionIdextra.requestIdctx.mcpCtx.requestIdextra.signalctx.requestCtx.signalextra.authInfoctx.requestCtx.authInfoextra.sendNotification(...)ctx.sendNotification(...)extra.sendRequest(...)ctx.sendRequest(...)Migration: Update property access paths as shown above. The
sendNotificationandsendRequestmethods remain at the top level of the context.Types of changes
Checklist
Additional context
Logging from ServerContext
Elicitation from ServerContext
Sampling from ServerContext
Accessing MCP Context
Task Context (when tasks are enabled)