-
Notifications
You must be signed in to change notification settings - Fork 73
feat(mcp): stateless and multi-pod server support #761
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
Open
gesh
wants to merge
7
commits into
main
Choose a base branch
from
posthog-code/mcp-stateless-session-token-py
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f8de485
feat(mcp): carry session id + client identity across stateless pods
gesh 2b05c27
feat(mcp): auto-wire the stateless mint from instrument()
gesh d6806f9
docs(mcp): lead the stateless example with the FastMCP + instrument()…
gesh f5e8d10
docs(mcp): tighten the stateless example comments
gesh c5771aa
docs(mcp): make the stateless example a runnable FastMCP server
gesh 8b161ea
chore(mcp): update public API snapshot for stateless session-token ex…
gesh c7ab522
fix(mcp): address stateless review findings
gesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """Stateless / multi-pod MCP analytics: keep one ``$session_id`` + the client | ||
| identity across pods via a session token in the ``Mcp-Session-Id`` header. | ||
|
|
||
| ``instrument()`` wires the mint into FastMCP's ``streamable_http_app()`` / | ||
| ``sse_app()`` factories, so a stateless server needs nothing extra. Run:: | ||
|
|
||
| POSTHOG_PROJECT_API_KEY=phc_xxx python examples/mcp_stateless.py | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from mcp.server.fastmcp import FastMCP | ||
|
|
||
| from posthog import Posthog | ||
| from posthog.mcp import instrument | ||
|
|
||
| posthog = Posthog( | ||
| os.environ.get("POSTHOG_PROJECT_API_KEY", "phc_xxx"), | ||
| host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"), | ||
| ) | ||
|
|
||
| server = FastMCP("my-server", stateless_http=True) | ||
|
|
||
|
|
||
| @server.tool() | ||
| def greet(name: str) -> str: | ||
| """Say hello.""" | ||
| return f"hi {name}" | ||
|
|
||
|
|
||
| instrument(server, posthog) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # instrument() already wired the session-token mint into this app, so every pod | ||
| # keeps one $session_id + the harness. (app = server.streamable_http_app() too.) | ||
| server.run(transport="streamable-http") | ||
|
|
||
|
|
||
| # No FastMCP server to wire (a custom dispatcher)? Add the middleware to your own | ||
| # ASGI app and read the recovered session per request: | ||
| # | ||
| # from posthog.mcp import PostHogMcpStatelessSessionMiddleware, get_mcp_session | ||
| # | ||
| # app.add_middleware(PostHogMcpStatelessSessionMiddleware) | ||
| # sess = get_mcp_session(request) # sess.session_id, sess.client_name, ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Seven new public names land here: the middleware, get_mcp_session, encode_session_id, decode_session_id, read_mcp_session_header, SessionTokenPayload, MCP_SESSION_HEADER. The middleware and get_mcp_session clearly need to be public for the custom dispatcher story. I'm less sure encode_session_id and read_mcp_session_header need to be public on day one versus staying internal until a real custom HTTP layer needs them. Not a blocker, just worth a second look since public surface is easy to add and hard to walk back.