modelcontextprotocol / modelcontextprotocol/python-sdk
Expose session, auth, and transport information on handler Context
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 24.3k
- Forks
- 4k
- Merge medio
- 1 d 1 h
- PR fusionados (30 d)
- 31
Descripción
Problem
Users consistently need access to transport-level information from inside tool, resource, and prompt handlers. Today, this data exists in the system but is not accessible through the Context object that handlers receive.
There are three categories of information users are asking for:
Session identity
Users need to identify which client session they are serving — for per-session state, logging, debugging, or multi-tenant scenarios. The session ID exists at the transport layer (e.g., Mcp-Session-Id header for streamable HTTP, UUID query param for SSE) but there is no way to read it from a handler.
- #485 — "How to get session_id in tool"
- #942 — "How to expose mcp_session_id to user?"
Auth credentials
Users completing an OAuth flow need to access the authenticated token or claims inside their handlers. The auth middleware already validates tokens and stores them in a ContextVar (auth_context_var), but Context does not expose this.
- #638 — "FastMCP Auth Context in tools"
Transport metadata (HTTP headers, client info)
Users need access to HTTP request headers, client IP, or other request-level metadata. The Starlette Request object is passed through as ServerMessageMetadata.request_context and lands on ServerRequestContext.request, but it is typed as Any and not surfaced on Context.
- #750 — "How to get HTTP request headers in tools"
- #375 — "Passing client context to tools"
Current state
The underlying data is available in the system — it just is not reachable from Context:
- Session ID: managed by
StreamableHTTPSessionManager/SseServerTransport, not exposed to handlers - Auth info: stored in
auth_context_varbyAuthContextMiddleware, not exposed to handlers - HTTP request: passed through as
request_context: AnyonServerMessageMetadata, available onServerRequestContext.requestbut not onContext
Scope
This issue is about designing and implementing access to these three categories of information on the handler Context. Key design considerations:
- Behavior varies by transport — stdio has no session ID, headers, or auth; stateless HTTP has no session ID; SSE has a different session ID mechanism than streamable HTTP
- The solution should work for both MCPServer (high-level) and low-level server handlers
- Should be consistent with the direction of #2021 (typed transport context) and #1684 (explicit context parameters)
Prior art
- PR #1608 — adds
ctx.session_idby threading throughInitializationOptions→ServerSession→RequestContext→Context
Related issues
- #2021 — Refactor handler context to be transport- and handler-type-aware (v2 architectural)
- #1684 — Refactor global contextvars into explicit parameters (v2 architectural)
- #2054 — Add dependency injection support to MCPServer (v2 architectural)
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza siguiendo el recorrido de Context, ServerRequestContext y ServerMessageMetadata.request_context; después inspecciona StreamableHTTPSessionManager, SseServerTransport y AuthContextMiddleware. Compara los enfoques de #2021, #1684 y #1608. Se considera terminado cuando los handlers pueden acceder de forma coherente a la información aplicable de sesión, autenticación y transporte en todos los transports compatibles, incluidos los casos documentados en los que los datos no están disponibles.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- api, authentication, backend
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100