-
Notifications
You must be signed in to change notification settings - Fork 1.5k
RFC: Expose initialization requests to middleware, and provide session storage #1215
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
RFC: Expose initialization requests to middleware, and provide session storage #1215
Conversation
|
@richardkmichael thanks! I went down a rabbit hole of supporting every JSONRPC call at first but it required way too much rewriting of the low-level internals to work. I really like that you cut through that by overwriting just the one method and ensuring it's in place in the overwritten .run() method. This is pretty compelling! I want to think on it a little just to make sure we aren't inadvertently taking on a broader maintenance burden than intended (though I strongly suspect your solution limits that as much as possible) |
|
Thank you for the quick feedback! I've just noticed #1160 -- but have not read it closely. Perhaps its state dictionary can be used for storage. Using the session was obviously a hack, but I needed that because there was no context at during initialization, IIRC. I certainly wouldn't want data stored in two places, so it would be worth looking at streamlining that aspect. |
|
My suggestion is lets look at #1160 for state and keep this PR focused on initializae middleware; I think 1160 may have stalled so I'll look at getting it over the line today. (update: merged 1160) |
|
Rewriting the |
|
@hopeful0 I agree. I have hesitated on rewriting low-level SDK at all because of the increased maintenance burden it places on FastMCP but I think we're at a place where the surface area is small enough and the benefit is large enough that this is worthwhile. |
|
I have been toying with this myself and this is a good solution. I want to be able to get client info and collect metrics on this information. Even though documentation says |
|
I love this proposal! As @rishid mentioned, this would allow us to collect metrics on some information across the entire MCP session; plus there are some cases where the MCP serves as a shim to other upstream services, where being able to communicate directly on server communication whether it's available to be used or not to the MCP hosts would be helpful. |
d0cf667 to
3dcf2c3
Compare
|
I've added the middleware/initialize aspects of this PR to #1546 and will close this as I think it's become stale. Appreciate the head start though @richardkmichael! |
|
Thank you! I was going ask what to do here, but I've been busy. Thanks for running with it! 👍 |
This is a proof-of-concept I hacked up with Claude. I'm not proposing it for merge as-is.
I haven't thought carefully about the consequences of these modifications. This might be touching on stateful/less aspects of MCP; not sure and perhaps not any more so than progress tokens or other remembered data on either side of the connection. If there is interest, I'll take a closer look.
I was intrigued by a middleware based solution for #1193 and #1159, but this requires modifying FastMCP.
Middleware documentation mentions all requests are exposed to middleware, but this is not true. The initialization part of the lifecycle is handled in the session establishment and not available to middleware.
This hack adds:
on_initializeIt allows a middleware like this:
I think exposing initialization to middleware would also be useful for: