-
Notifications
You must be signed in to change notification settings - Fork 1.3k
MCP SDK: Draft - Context Utility + Request Context & Lifespan context #1009
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?
MCP SDK: Draft - Context Utility + Request Context & Lifespan context #1009
Conversation
Think also the examples in this case should have the |
cc @ihrpr |
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.
Than you for working on this!
I would be very careful about Python SDK to Typescript SDK quality of life features porting, as many paradigms do not translate well to Typescript. I have concern about lifespan:
- Python's async context managers don't translate well to TypeScript/JavaScript
- Existing patterns - TypeScript/Node.js already has established patterns for dependency injection and lifecycle management
- Memory management - JavaScript's garbage collection and event loop model make this pattern unnecessary and potentially problematic
The TypeScript SDK should stick to idiomatic Typescript/Javascript patterns like:
- Constructor injection for dependencies
- Module-level state management
- cleanup in disconnect handlers
I think I would also love to see in this PR how it will be used and what improvements we can get, probably adding a few code examples would be useful to have in the PR
Thanks for the comments. I can add examples in commits themselves (e.g. in the examples/ folder), they will be pretty much the same as in this PR's description. On your comments:
OK, what in the PR contradicts that? We are simply adding an utility, need some clarification/example on this point.
Agree, but let me know what parts of the PR you see as non-conformant with these points. |
This is a draft PR which aims to introduce convenience / quality of life improvements to the Typescript SDK and also bring it closer to the Python SDK, which already has implemented this, in a similar fashion.
This is a draft PR, feedback is more than welcome.
It adds a
Context
utility (with the aim to be backwards-compatible, so anyone using RequestHandlerExtra will not have breaking changes), which exposes extra convenience methods and could be extended further down the line.Motivation and Context
It aims to explore the concept of a backwards-compatible
Context
utility class being passed in callbacks on the server-side. TheContext
utility class provides convenience methods and can be further extended in the future. It provides access to methods like logging, sampling, elicitation (and others could be added) and one can call them from the callbacks without having access to theServer
orMcpServer
instances.It mimics to a large extent on how Python SDK works.
Additionally,
Context
has a composed class inside it,RequestContext
(again, similar to how PythonSDK), which holds all the metadata, and adds on top of it thelifespanContext
data.lifespanContext
is a convenience / quality of life utility as implemented in the PythonSDK, which allows to pass any data to theMcpServer
, and access it back from the callback, by doingextra.requestContext.lifespanContext
- this could be database connections or anything that the user wants to live through the whole lifespan of the server. This feature required some type generics to be added so that the.lifespanContext
can have type safety when used from the callbacks.TBD. Once any discussions have been had and if this gets accepted, documentation needs to be updated additionally in this PR to reflect the changes.
How Has This Been Tested?
Additional unit tests have been added.
Existing unit tests work with no changes.
Tests when callback is tied to RequestHandlerExtra.
Breaking Changes
No breaking changes. More tests could be added to ensure it.
The additions affect only server-side. Client-side should remain the same.
Types of changes
Checklist
Additional context
The changes follow existing concepts of the PythonSDK, applied in a similar fashion (to the extent that the libraries differ or similar in various parts of them) to Typescript.
INotes:
Context
, but currently it needs to implementRequestHandlerExtra
to keep backward compatibilityRequestHandlerExtra
data actually resides insideRequestContext
with the intent of moving it solely in there when we can afford breaking changes, leaving us with discrete data inContext
andRequestContext
.Context
Lifespan Context example
Logging Utility methods
Elicit from Context
Sampling from Context