python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

Context object (layered, per-request)

Open maxisbey opened this issue 5 months ago • 2 comments

The handler-facing context: per-request client identity/capabilities/version reachable from handlers, transport info under a namespace, legacy session surface quarantined. Several other issues in this milestone add fields to this object, so settling its shape early unblocks them.

What's in it

  • Layered/namespaced shape (e.g. ctx.transport.…; legacy-only verbs/state quarantined).
  • Per-request client info / capabilities / protocol version reachable from handlers (fed from _meta in the modern era, from the connection in legacy).
  • Per-request logLevel gate: no notifications/message unless the request opted in.
  • OTel and user meta= keys coexist with the envelope.
  • Typed per-transport context replaces the raw-Starlette-Request carrier; auth/headers reachable.

Conformance

  • (SDK API surface — proven via in-repo tests)
  • Together with Backwards compatibility & version negotiation, New HTTP serving path (modern + dual-era), Capabilities API + server/discover handler, subscriptions/listen: the per-request-logging and capability-enforcement checks inside server-stateless run (not skip) and pass

Dependencies

  • Depends on: none

References

maxisbey avatar Jun 17 '26 11:06 maxisbey

Shipped in v2.0.0b1: typed per-transport context, per-request client identity/capabilities/version from the envelope, the per-request logLevel gate, and OTel/user meta coexistence. This issue's conformance bar — the per-request-logging and capability-enforcement checks inside server-stateless running and passing — is met. Some context-API ergonomic refinements remain tracked as post-beta polish.

AI Disclaimer

maxisbey avatar Jul 01 '26 10:07 maxisbey

Hello @maxisbey , I can still reproduce a request-scoped notifications/message on v2.0.0b1 when the originating 2026-07-28 request does not include io.modelcontextprotocol/logLevel.

I reproduced this with the simple-streamablehttp-stateless example server on v2.0.0b1 (0da9092). The request used a valid 2026-07-28 envelope and the required Mcp-Method and Mcp-Name headers; the only relevant field omitted from _meta was io.modelcontextprotocol/logLevel:

{
  "jsonrpc": "2.0",
  "id": 1001,
  "method": "tools/call",
  "params": {
    "name": "start-notification-stream",
    "_meta": {
      "io.modelcontextprotocol/protocolVersion": "2026-07-28",
      "io.modelcontextprotocol/clientInfo": {"name": "repro", "version": "0.1.0"},
      "io.modelcontextprotocol/clientCapabilities": {}
    },
    "arguments": {"interval": 1, "count": 1, "caller": "rc-repro"}
  }
}

The request returned HTTP 200 and emitted the following request-scoped log notification on that request's response stream before the final response:

{"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","logger":"notification_stream","data":"Notification 1/1 from caller: rc-repro"}}

The reproduced example path calls ctx.session.send_log_message(..., related_request_id=ctx.request_id). ServerSession.send_log_message() forwards the log notification through the request-scoped channel, but I do not see a check for io.modelcontextprotocol/logLevel in the originating request's _meta.

Context.log() also appears to send a request-scoped notifications/message without consulting that metadata.

Since #2895 lists the per-request logLevel gate as completed, I wanted to flag that this request-scoped ServerSession.send_log_message() path still reproduces on v2.0.0b1. Is this an uncovered path in the current implementation, or am I missing another condition required for the gate to apply?

cclabadmin avatar Jul 11 '26 16:07 cclabadmin