diff --git a/conformance-baseline-2026-07-28.yml b/conformance-baseline-2026-07-28.yml index 70e5f7bc..8d9c6775 100644 --- a/conformance-baseline-2026-07-28.yml +++ b/conformance-baseline-2026-07-28.yml @@ -12,25 +12,8 @@ server: - tasks-request-state-removal - tasks-required-task-error - tasks-wire-fields - # server-to-client input requests during tools/call — T5, https://github.com/softwaremill/chimp/issues/243 - - input-required-result-basic-elicitation - - input-required-result-basic-list-roots - - input-required-result-basic-sampling - - input-required-result-capability-check - - input-required-result-multiple-input-requests - - input-required-result-multi-round - - input-required-result-non-tool-request - - input-required-result-request-state - - input-required-result-result-type - - input-required-result-tampered-state - # full stateless lifecycle (needs subscription serving) - - server-stateless - # x-mcp-header custom headers in the tool DSL — T13, https://github.com/softwaremill/chimp/issues/251 - - http-custom-header-server-validation # the harness records a failure in a non-primary run of these scenarios (the primary run passes) - sep-2164-resource-not-found - - input-required-result-missing-input-response - - input-required-result-ignore-extra-params client: # OAuth client flows — chimp's client has no full OAuth support yet - auth/authorization-server-migration diff --git a/core/src/main/scala/chimp/protocol/Ids.scala b/core/src/main/scala/chimp/protocol/Ids.scala index 13803dc9..77b476b1 100644 --- a/core/src/main/scala/chimp/protocol/Ids.scala +++ b/core/src/main/scala/chimp/protocol/Ids.scala @@ -2,15 +2,16 @@ package chimp.protocol import io.circe.{Codec, Decoder, Encoder, Json} -opaque type RequestId = String | Int +opaque type RequestId = String | Int | Long object RequestId: - def apply(value: String | Int): RequestId = value - def unapply(id: RequestId): Option[String | Int] = Some(id) + def apply(value: String | Int | Long): RequestId = value + def unapply(id: RequestId): Option[String | Int | Long] = Some(id) given encoder: Encoder[RequestId] = Encoder.instance: case s: String => Json.fromString(s) case i: Int => Json.fromInt(i) + case l: Long => Json.fromLong(l) given decoder: Decoder[RequestId] = Decoder.instance: c => - c.as[String].map(RequestId(_)).orElse(c.as[Int].map(RequestId(_))) + c.as[String].map(RequestId(_)).orElse(c.as[Int].map(RequestId(_))).orElse(c.as[Long].map(RequestId(_))) given codec: Codec[RequestId] = Codec.from(decoder, encoder) opaque type ProgressToken = String | Int diff --git a/core/src/main/scala/chimp/protocol/Mrtr.scala b/core/src/main/scala/chimp/protocol/Mrtr.scala index a85fcc47..11e24a4c 100644 --- a/core/src/main/scala/chimp/protocol/Mrtr.scala +++ b/core/src/main/scala/chimp/protocol/Mrtr.scala @@ -49,8 +49,8 @@ object InputResponse: * opaque `requestState` the client echoes back on its response, and the modern result envelope fields. */ final case class InputRequiredResult( - inputRequests: Map[String, InputRequest], + inputRequests: Option[Map[String, InputRequest]] = None, requestState: Option[String] = None, - resultType: ResultType = ResultType.Incomplete, + resultType: ResultType = ResultType.InputRequired, _meta: Option[Map[String, Json]] = None ) derives io.circe.Codec diff --git a/core/src/main/scala/chimp/protocol/Versioning.scala b/core/src/main/scala/chimp/protocol/Versioning.scala index 9a5cacb4..5b031a6f 100644 --- a/core/src/main/scala/chimp/protocol/Versioning.scala +++ b/core/src/main/scala/chimp/protocol/Versioning.scala @@ -18,6 +18,7 @@ object ProtocolMeta: val ProtocolVersionKey: String = "io.modelcontextprotocol/protocolVersion" val ClientInfo: String = "io.modelcontextprotocol/clientInfo" val ClientCapabilities: String = "io.modelcontextprotocol/clientCapabilities" + val LogLevel: String = "io.modelcontextprotocol/logLevel" val ServerInfo: String = "io.modelcontextprotocol/serverInfo" val SubscriptionId: String = "io.modelcontextprotocol/subscriptionId" @@ -29,19 +30,27 @@ object ProtocolMeta: data = Some(Json.obj("requested" -> requested.asJson, "supported" -> supported.asJson)) ) -/** The type of a modern (2026-07-28+) result: `complete` for a finished result, `incomplete` for one that needs another round trip (MRTR - * input-required). + /** A `MissingRequiredClientCapability` error (`-32021`) naming the capabilities the client must declare to make the request. */ + def missingClientCapabilityError(required: ClientCapabilities): JSONRPCErrorObject = + JSONRPCErrorObject( + code = JSONRPCErrorCodes.MissingRequiredClientCapability.code, + message = "Missing required client capability", + data = Some(Json.obj("requiredCapabilities" -> required.asJson.deepDropNullValues)) + ) + +/** The type of a modern (2026-07-28+) result: `complete` for a finished result, `input_required` for one that needs another round trip + * (MRTR). */ enum ResultType(val name: String): case Complete extends ResultType("complete") - case Incomplete extends ResultType("incomplete") + case InputRequired extends ResultType("input_required") object ResultType: given Encoder[ResultType] = Encoder.instance(resultType => Json.fromString(resultType.name)) given Decoder[ResultType] = Decoder.decodeString.emap: - case "complete" => Right(Complete) - case "incomplete" => Right(Incomplete) - case other => Left(s"Unknown result type: $other") + case "complete" => Right(Complete) + case "input_required" => Right(InputRequired) + case other => Left(s"Unknown result type: $other") /** Whether a cached response may be shared across authorization contexts (`Public`) or not (`Private`). */ enum CacheScope: diff --git a/core/src/test/scala/chimp/protocol/Schema20260728ConformanceSpec.scala b/core/src/test/scala/chimp/protocol/Schema20260728ConformanceSpec.scala index 59792796..121cd394 100644 --- a/core/src/test/scala/chimp/protocol/Schema20260728ConformanceSpec.scala +++ b/core/src/test/scala/chimp/protocol/Schema20260728ConformanceSpec.scala @@ -90,8 +90,9 @@ class Schema20260728ConformanceSpec extends SchemaConformance: ) validate( "InputRequiredResult", - InputRequiredResult(inputRequests = Map("name" -> request), requestState = Some("state-1")) + InputRequiredResult(inputRequests = Some(Map("name" -> request)), requestState = Some("state-1")) ) + InputRequiredResult(requestState = Some("state-1")).asJson.hcursor.get[String]("resultType") shouldBe Right("input_required") it should "produce CallToolRequestParams carrying inputResponses and requestState that match the spec schema" in: validate( diff --git a/docs/index.md b/docs/index.md index 15040596..85f1ecee 100644 --- a/docs/index.md +++ b/docs/index.md @@ -15,6 +15,7 @@ and [sttp](https://github.com/softwaremill/sttp), supporting the variety of the server/prompts server/resources server/capabilities + server/mrtr server/examples .. toctree:: diff --git a/docs/server/capabilities.md b/docs/server/capabilities.md index 092db3d1..27f6b7f7 100644 --- a/docs/server/capabilities.md +++ b/docs/server/capabilities.md @@ -1,9 +1,9 @@ # Server capabilities -Most tools just answer a request, so `serverLogic`/`handle` expose no context. A tool that needs to **push to the client while it runs** uses `streamingServerLogic`, which receives a `StreamingServerContext[F]`: +Most tools just answer a request, so `serverLogic`/`handle` expose no context. Use `serverLogicWithContext` to read the facts of the request (see [request facts](#request-facts)). A tool that needs to **push to the client while it runs** uses `streamingServerLogic`, which receives a `StreamingServerContext[F]`: - `reportProgress` — [progress](https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/progress) notifications, auto-wired to the request's progress token. -- `log` — [logging](https://modelcontextprotocol.io/specification/2025-11-25/server/utilities/logging) notifications. +- `log` — [logging](https://modelcontextprotocol.io/specification/2025-11-25/server/utilities/logging) notifications. For a modern (`2026-07-28`) request, chimp sends a message only when its level is at or above the [per-request log level](https://modelcontextprotocol.io/specification/2026-07-28/server/utilities/logging#per-request-log-level) in `_meta` (`io.modelcontextprotocol/logLevel`). If the request sets no level, chimp sends no messages. For a legacy request, chimp sends all messages; use `.withLoggingLevel` to receive `logging/setLevel`. ```{note} Pushing to the client requires an open stream, so a `streamingServerLogic` tool is registered with `addStreamingTool` on a `StreamingMcpServer`, and will not compile on the plain request/response endpoint. @@ -28,7 +28,7 @@ val work = tool("work") val server = StreamingMcpServer[Identity]().addStreamingTool(work) ``` -Server-wide capabilities are enabled by registering a handler — only what you wire up is advertised: `.withCompletion`, `.withLoggingLevel`, `.withSubscriptions`. `.withLoggingLevel` and `.withSubscriptions` serve only legacy clients: `2026-07-28` removed `logging/setLevel` and `resources/{,un}subscribe`, so a modern client gets a `404` for them (see below). +Server-wide capabilities are enabled by registering a handler — only what you wire up is advertised: `.withCompletion`, `.withLoggingLevel`, `.withSubscriptions`, `.withPublisher`. `.withLoggingLevel` and `.withSubscriptions` serve only legacy clients: `2026-07-28` removed `logging/setLevel` and `resources/{,un}subscribe`, so a modern client gets a `404` for them (see below). `.withPublisher` serves only modern clients (see [publishing list changes and resource updates](#publishing-list-changes-and-resource-updates)). ## Protocol versions @@ -36,14 +36,108 @@ chimp is **dual-era**: it speaks the legacy handshake revisions (`2025-11-25` an The server answers `server/discover` automatically with its supported versions, capabilities and identity (`serverInfo` in `_meta`), so a client can learn them in one request without connecting. A request that declares a protocol version the server does not support is rejected with an `UnsupportedProtocolVersion` error (`-32022`) listing the supported versions, per the [versioning compatibility matrix](https://modelcontextprotocol.io/specification/2026-07-28/basic/versioning#compatibility-matrix). No configuration is required for any of this. -A request is **modern** when its `_meta` or its `MCP-Protocol-Version` header declares `2026-07-28`. A legacy client that writes a legacy version into `_meta` stays on the legacy rules. chimp checks every modern request before any tool logic runs, and answers a failed check with a plain JSON error and its own HTTP status: +A request is **modern** when its `_meta` or its `MCP-Protocol-Version` header declares `2026-07-28`. A legacy client that writes a legacy version into `_meta` stays on the legacy rules. chimp checks every modern request, `server/discover` included, before any tool logic runs, and answers a failed check with a plain JSON error and its own HTTP status. A `server/discover` that declares no version (or a legacy version) gets no checks, so any client can discover the server: | Problem | JSON-RPC error | HTTP status | |---|---|---| | unsupported protocol version | `-32022` UnsupportedProtocolVersion | `400` | | `_meta` has no `protocolVersion` or no `clientCapabilities` | `-32602` InvalidParams | `400` | -| the `MCP-Protocol-Version` header and `_meta` declare different versions | `-32020` HeaderMismatch | `400` | +| the `MCP-Protocol-Version` header is missing (HTTP only), or it and `_meta` declare different versions | `-32020` HeaderMismatch | `400` | | `Mcp-Method` / `Mcp-Name` header missing or different from the body (HTTP only) | `-32020` HeaderMismatch | `400` | +| `tools/call` with an `Mcp-Param-{name}` header missing, malformed or different from its argument (HTTP only) | `-32020` HeaderMismatch | `400` | | removed method (`initialize`, `ping`, `logging/setLevel`, `resources/subscribe`, `resources/unsubscribe`) or unknown method | `-32601` MethodNotFound | `404` | +| `subscriptions/listen` on a server without a publisher, or on the sync HTTP transport | `-32601` MethodNotFound | `404` | +| `tools/call` of a tool whose required client capabilities `_meta` does not declare | `-32021` MissingRequiredClientCapability | `400` | Over HTTP, a modern request must carry the SEP-2243 standard headers: `Mcp-Method` with the body's `method`, and `Mcp-Name` with `params.name` on `tools/call` and `prompts/get`, or `params.uri` on `resources/read`. Header names are case-insensitive, values are case-sensitive, and surrounding whitespace is ignored. chimp's own client sends these headers and the `_meta` when you give it a modern protocol version — see [client transport](../client/transport.md). + +A modern `tools/call` over HTTP must also carry an `Mcp-Param-{name}` header for each argument that the tool marks with [`x-mcp-header`](tools.md#mirroring-arguments-into-http-headers). The server compares each header with its argument: + +- An argument that is present needs its header. An argument that is `null` or absent needs no header, and a header for it is a mismatch. +- Integers are compared numerically, so `42.0` in the header equals `42` in the body. Booleans are `true` or `false`, and strings are compared literally. +- `Mcp-Param-*` headers that match no marked argument are ignored. + +`Mcp-Name` and `Mcp-Param-*` values can use the Base64 sentinel form `=?base64?{value}?=`, which carries the UTF-8 bytes of the value. The server decodes a value in this form before the comparison. A sentinel with invalid Base64 (bad padding, characters outside the alphabet) or invalid UTF-8, and a value with characters that are not visible ASCII, space or tab, are a `-32020`. A value without the complete wrapper is compared literally. + +Legacy requests and stdio get none of these header checks. + +## Request facts + +Each handler can read the facts of its request from `ctx.request`, a `RequestInfo`: + +- `protocolVersion` — the protocol version of the request. +- `clientCapabilities` and `clientInfo` — what the client declares in `_meta`. +- `logLevel` — the per-request log level from `_meta`. +- `progressToken` — the progress token from `_meta`. +- `inputResponses` and `requestState` — on the retry of an input-required outcome, the answers of the client and the echoed state (see [asking the client for input](mrtr.md)). + +A modern request carries all of these in `_meta`. The server keeps no state between requests, so a legacy request gives only the protocol version (from the `MCP-Protocol-Version` header or `_meta`) and the progress token. The other facts are `None`. + +`serverLogicWithContext` gives the context to tools, prompts, resources and resource templates on every server. `streamingServerLogic` and `securedStreamingServerLogic` give it too, because `StreamingServerContext` is a `ServerContext`. + +A tool that needs a client capability declares it with `withRequiredClientCapabilities`. If a modern request does not declare all of these capabilities in `_meta`, chimp rejects it before the logic runs, with `-32021` (MissingRequiredClientCapability), HTTP `400`, and the capabilities in `data.requiredCapabilities`. chimp compares the capabilities key by key, also in nested objects. A legacy request has no per-request capabilities, so chimp does not check it. + +```scala mdoc:compile-only +import chimp.server.* +import chimp.protocol.ClientCapabilities +import io.circe.{Codec, Json} +import sttp.shared.Identity +import sttp.tapir.* + +case class AskInput(question: String) derives Codec, Schema + +val ask = tool("ask") + .withRequiredClientCapabilities(ClientCapabilities(sampling = Some(Json.obj()))) + .input[AskInput] + .serverLogicWithContext[Identity]: (in, ctx, _) => + val client = ctx.request.clientInfo.fold("unknown client")(_.name) + ToolResult.text(s"$client asked: ${in.question}") + +val server = McpServer[Identity]().addTool(ask) +``` + +## Publishing list changes and resource updates + +A server tells its clients that its tools, prompts or resources changed, or that a resource was updated, through a `NotificationPublisher`. Create one, give it to the server with `.withPublisher`, and call it from any part of your application, not only from a tool: + +- `toolsListChanged()`, `promptsListChanged()`, `resourcesListChanged()` — send the list-changed notification of that list. +- `resourceUpdated(uri)` — send `notifications/resources/updated` for one resource URI. +- `close()` — end all open subscriptions, for example when the server shuts down. + +The methods are thread-safe and return `Unit`, not an effect. In an effect system, wrap the call, for example `ZIO.succeed(publisher.toolsListChanged())`. + +```scala mdoc:compile-only +import chimp.server.* +import chimp.server.ox.OxServerHttpTransport +import io.circe.Codec +import sttp.shared.Identity +import sttp.tapir.* + +case class NoteInput(text: String) derives Codec, Schema + +val publisher = NotificationPublisher() + +val addNote = tool("add_note") + .input[NoteInput] + .handle: _ => + publisher.resourcesListChanged() + ToolResult.text("note added") + +val server = McpServer[Identity]().addTool(addNote).withPublisher(publisher).streaming +val endpoint = OxServerHttpTransport(List("mcp")).serve(server) +``` + +A `2026-07-28` client receives these notifications on a [`subscriptions/listen`](https://modelcontextprotocol.io/specification/2026-07-28/basic/patterns/subscriptions) stream: + +- With a publisher, `server/discover` advertises `listChanged` for tools, prompts and resources, and `subscribe` for resources. The capability of a list is there only when the server has that feature, for example `prompts` only when the server has prompts. The `initialize` result for legacy clients does not change: it advertises no list changes, and `subscribe` only for `.withSubscriptions`. +- The first message on the stream is `notifications/subscriptions/acknowledged`. Its filter is the part of the requested filter that the server honours. The server sends no other notification types. +- Each message on the stream carries the id of the listen request in `_meta`, under `io.modelcontextprotocol/subscriptionId`. +- `notifications/resources/updated` goes only to the streams which list the URI in `resourceSubscriptions`. +- Progress and log notifications do not go on a listen stream. They stay on the stream of their request. +- Over HTTP, a subscription ends when the client closes the stream. The server notices a closed stream at the latest when it next sends on it. +- Over stdio, all subscriptions share the one channel. The client ends one with `notifications/cancelled` that names the listen request. When the input ends, the server ends each open subscription. +- When the server ends a subscription (on `close()`, or at the end of the stdio input), it sends a successful `subscriptions/listen` result with the subscription id. Over stdio, it then sends `notifications/cancelled` for the listen request. + +```{note} +A listen stream needs a streaming transport. On the sync HTTP transport (`McpServer.endpoint`), the server answers `subscriptions/listen` with `-32601` and advertises no list changes. +``` diff --git a/docs/server/mrtr.md b/docs/server/mrtr.md new file mode 100644 index 00000000..f9020171 --- /dev/null +++ b/docs/server/mrtr.md @@ -0,0 +1,101 @@ +# Asking the client for input + +In protocol version `2026-07-28`, a server does not send requests to the client while it handles a request. Instead, a handler can end a `tools/call`, `prompts/get` or `resources/read` with an **input-required** outcome. The client gets the input that the handler asks for, and then sends the same request again with the answers. The server keeps no state between the two requests. This is the [multi round-trip requests](https://modelcontextprotocol.io/specification/2026-07-28/basic/patterns/mrtr) (MRTR) pattern. + +## Requesting input + +A tool, prompt or resource handler returns `InputRequired` instead of its result. `InputRequired` names each input request with a key that the handler chooses: + +| Input request | What the client does | Client capability that it needs | +|---|---|---| +| `InputRequired.elicitForm(message, schema)` | asks the user to fill in a form | `elicitation` (form mode) | +| `InputRequired.elicitUrl(message, url)` | asks the user to open a URL | `elicitation.url` | +| `InputRequired.sample(params)` | samples its LLM | `sampling` | +| `InputRequired.listRoots` | sends its roots | `roots` | + +On the retry, the handler reads the answers under the same keys from `ctx.request`: `elicitResult(key)`, `samplingResult(key)` and `rootsResult(key)`. A handler that never returns `InputRequired` works as before. + +```scala mdoc:compile-only +import chimp.server.* +import io.circe.{Codec, Json} +import sttp.shared.Identity +import sttp.tapir.* + +case class NoInput() derives Codec, Schema + +val nameSchema = Json.obj( + "type" -> Json.fromString("object"), + "properties" -> Json.obj("name" -> Json.obj("type" -> Json.fromString("string"))) +) + +val greet = tool("greet") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + ctx.request.elicitResult("user_name").flatMap(_.content).flatMap(_.get("name")).flatMap(_.asString) match + case Some(name) => ToolResult.text(s"Hello, $name!") + case None => InputRequired("user_name" -> InputRequired.elicitForm("What is your name?", nameSchema)) +``` + +Prompts and resources use the same `InputRequired` from their `serverLogicWithContext`. + +### Several rounds and the request state + +A handler can ask again on the retry, as many times as it needs. To remember what happened in the earlier rounds, it attaches a **request state** with `.withRequestState(state)`. The client sends the state back without a change on its retry, and the handler reads it from `ctx.request.requestState`. `InputRequired.withRequestState(state)` makes an outcome that has a state and no input requests: the client then retries at once. + +```{warning} +The request state goes through the client, so treat it as input from an attacker. If the state controls authorization, resource access or business logic, protect its integrity with a [request state signer](#protecting-the-request-state). +``` + +### Missing answers + +If the retry does not have an answer that the handler needs, return a new `InputRequired` that asks for it again. Do not fail the request. The client can omit an answer, or send it under a different key. Keys that the handler did not ask for are ignored. + +### Client capabilities + +A handler can read `ctx.request.clientCapabilities` and ask only for what the client supports. For example, it can ask for sampling when the client declares `sampling`, and for a form if not. + +## Rules that chimp enforces + +chimp applies these rules, so a handler does not have to: + +| Problem | Result | +|---|---| +| an input request that needs a capability which the client did not declare | `-32021` MissingRequiredClientCapability with `data.requiredCapabilities`, HTTP `400` | +| `inputResponses` is not an object, or one of its values is not a sampling, roots or elicitation result | `-32602` InvalidParams, HTTP `400`; the handler does not run | +| `requestState` is not a string | `-32602` InvalidParams, HTTP `400`; the handler does not run | +| `InputRequired` on a legacy (`2025-11-25` or earlier) request | `-32603` InternalError which names protocol version `2026-07-28` | + +An `InputRequired` always has at least one input request or a request state. Only `tools/call`, `prompts/get` and `resources/read` can end with it. + +On a streaming HTTP server, chimp opens the event stream when the handler sends its first message. If the handler ends with the `-32021` before it sends a message, chimp sends a plain JSON error with HTTP `400`. If the handler already sent a message, for example a progress notification, the `-32021` is the last message on the event stream. + +## Protecting the request state + +Give the server a `RequestStateSigner` to protect the integrity of all request states: + +```scala mdoc:compile-only +import chimp.server.* +import sttp.shared.Identity +import scala.concurrent.duration.* + +val secret: Array[Byte] = sys.env("MCP_REQUEST_STATE_SECRET").getBytes("UTF-8") + +val server = McpServer[Identity]() + .withRequestStateSigner(RequestStateSigner(secret, ttl = 5.minutes)) +``` + +The handler code does not change. When a handler returns a request state, chimp signs it with HMAC-SHA256. The signature also covers: + +- an expiry, after the `ttl` (10 minutes by default), +- the method, the tool or prompt name or the resource URI, and a digest of the arguments of the request, +- on a secured server, the principal: use `withRequestStateSigner(signer, principalId)`, where `principalId` gives a stable identifier of the principal. + +On the retry, chimp verifies the state before the handler runs, and gives the handler the original state. If the state was changed, has expired, or belongs to another request or principal, chimp sends `-32602` InvalidParams with HTTP `400`, and the handler does not run. + +Security notes: + +- The secret must have at least 32 bytes. Keep it in a secret store, not in the code. All instances of the server must use the same secret, or a retry can fail on a different instance. +- When you change the secret, all request states that are in progress become invalid. The clients must start these requests again. +- The signer protects the integrity of the state, not its confidentiality. The client can read the state, so do not put secrets in it. +- The signer does not make a state single-use. Within its lifetime, the client can send the same state again on the same request. If an action must occur only one time, record it on the server. + diff --git a/docs/server/prompts.md b/docs/server/prompts.md index 0e818c17..a8ba0a90 100644 --- a/docs/server/prompts.md +++ b/docs/server/prompts.md @@ -24,3 +24,5 @@ val endpoint = McpServer(prompts = List(greeting)).endpoint(List("mcp")) ``` Prompt messages reuse the `ToolContent` content types, so they can embed images (`ToolContent.Image`) and resources (`ToolContent.ResourceContent`) just like tool results. + +A prompt can also ask the client for input before it returns its messages: see [asking the client for input](mrtr.md). diff --git a/docs/server/resources.md b/docs/server/resources.md index 9333a777..41ba8783 100644 --- a/docs/server/resources.md +++ b/docs/server/resources.md @@ -8,6 +8,7 @@ - `serverLogic` — effectful read logic, with the request headers. - `handleSecured` (synchronous) or `securedServerLogic` (effectful) — logic that also receives the principal made by the server's security logic; usable only on a secured server — see [transport security](transport.md). - Register with `.addResource` / `.addResourceTemplate`. Subscriptions are wired with `.withSubscriptions`; they serve legacy clients only, as `2026-07-28` removed `resources/{,un}subscribe` (see [protocol versions in server capabilities](capabilities.md)). +- On `2026-07-28`, a client subscribes to resources with the `resourceSubscriptions` filter of `subscriptions/listen`, which replaces `resources/subscribe`. Send the updates with `NotificationPublisher.resourceUpdated(uri)`; the server sends each update only to the streams which list its URI (see [publishing list changes and resource updates](capabilities.md#publishing-list-changes-and-resource-updates)). ```scala mdoc:compile-only import chimp.server.* @@ -26,3 +27,5 @@ val endpoint = McpServer(resources = List(readme), resourceTemplates = List(item ``` Returning `Left(ResourceError(...))` (or reading an unknown URI) responds with a JSON-RPC error carrying the offending `uri`. + +A resource can also ask the client for input before it returns its contents: see [asking the client for input](mrtr.md). diff --git a/docs/server/tools.md b/docs/server/tools.md index 656d7dcd..8f8bec97 100644 --- a/docs/server/tools.md +++ b/docs/server/tools.md @@ -53,3 +53,38 @@ val adder = tool("adder") The output type carries into the logic, which returns a `ToolResult[SumOutput]`; `ToolResult.error` is accepted whatever the output type. A result with structured output but no content is also sent as serialized JSON text, as the spec recommends. + +A tool can also ask the client for input before it returns its result: see [asking the client for input](mrtr.md). + +## Mirroring arguments into HTTP headers + +A tool can mark an input property with [`x-mcp-header`](https://modelcontextprotocol.io/specification/2026-07-28/server/tools#x-mcp-header). A `2026-07-28` client on Streamable HTTP then also sends the argument in the `Mcp-Param-{name}` header, so that load balancers, proxies and gateways can route on it without reading the body. Use `withMcpHeader` with the property name, or with the chain of property names to a nested property: + +```scala mdoc:compile-only +import chimp.server.* +import io.circe.Codec +import sttp.tapir.* + +case class QueryInput(region: String, query: String) derives Codec, Schema + +val executeSql = tool("execute_sql") + .description("Executes SQL in a region") + .input[QueryInput] + .withMcpHeader("region", "Region") + .handle(in => ToolResult.text(s"Ran in ${in.region}: ${in.query}")) +``` + +The `inputSchema` of the tool then has `"x-mcp-header": "Region"` on the `region` property. `withMcpHeader` works the same for a raw JSON Schema given to `.inputJson(schema)`, and an `x-mcp-header` which is already in a raw JSON Schema is kept. + +The server checks the `Mcp-Param-*` headers against the arguments, and rejects a mismatch with `-32020` — see [protocol versions](capabilities.md#protocol-versions). + +The tool builder (`input`, `inputJson` and `withMcpHeader`) throws an `IllegalArgumentException` when an annotation breaks a constraint of the spec: + +- The header name is empty, is not an HTTP token (letters, digits and ``!#$%&'*+-.^_`|~``), or has control characters. +- Two properties of one tool use the same header name, ignoring case. +- The property type is not `string`, `integer` or `boolean`. A `number` property is not permitted. +- The property is not reachable from the schema root through `properties` keys only. For example, it is in `items`, in `anyOf`, or behind a `$ref`. A nested case class gets a `$ref` in a derived schema, so mark only the top-level fields of a derived schema, or use a raw JSON Schema. + +```{warning} +Do not mark sensitive parameters, such as passwords, tokens or personal data, with `x-mcp-header`. HTTP headers are often written to logs and are visible to every intermediary on the way to the server. +``` diff --git a/server-conformance/src/main/scala/chimp/conformance/server/Main.scala b/server-conformance/src/main/scala/chimp/conformance/server/Main.scala index 848ed73b..ee27c071 100644 --- a/server-conformance/src/main/scala/chimp/conformance/server/Main.scala +++ b/server-conformance/src/main/scala/chimp/conformance/server/Main.scala @@ -10,6 +10,7 @@ import sttp.shared.Identity import sttp.tapir.Schema import sttp.tapir.server.netty.sync.NettySyncServer +import java.security.SecureRandom import java.util.Base64 import scala.concurrent.duration.* @@ -17,6 +18,7 @@ object Main: private case class AddNumbersInput(a: Double, b: Double) derives Codec, Schema private case class NoInput() derives Codec, Schema + private case class RegionInput(region: String) derives Codec, Schema private def base64Resource(path: String): String = val stream = getClass.getResourceAsStream(path) @@ -105,6 +107,12 @@ object Main: |}""".stripMargin ).toOption.get + private val customHeaderTool = tool("test_custom_header") + .description("Mirrors its region argument into the Mcp-Param-Region header") + .input[RegionInput] + .withMcpHeader("region", "Region") + .handle(in => ToolResult.text(s"region: ${in.region}")) + private val jsonSchemaTool = tool("json_schema_2020_12_tool") .description("Advertises a JSON Schema 2020-12 input schema") .inputJson(jsonSchema2020) @@ -132,6 +140,151 @@ object Main: ctx.log(LoggingLevel.Info, Json.fromString("Tool execution completed")) ToolResult.text("logging complete") + private def stringSchema(property: String): Json = + Json.obj( + "type" -> Json.fromString("object"), + "properties" -> Json.obj(property -> Json.obj("type" -> Json.fromString("string"))), + "required" -> Json.arr(Json.fromString(property)) + ) + + private def answer(ctx: ServerContext[Identity], key: String, property: String): Option[String] = + ctx.request.elicitResult(key).flatMap(_.content).flatMap(_.get(property)).flatMap(_.asString) + + private def samplingText(ctx: ServerContext[Identity], key: String): Option[String] = + ctx.request.samplingResult(key).map(_.content).collect { case ToolContent.Text(_, text, _, _) => text } + + private def sampling(text: String, maxTokens: Int): InputRequest = + InputRequired.sample(CreateMessageParams(List(SamplingMessage(Role.User, ToolContent.Text(text = text))), maxTokens)) + + private val askName = InputRequired.elicitForm("What is your name?", stringSchema("name")) + + private val missingCapabilityTool = tool("test_missing_capability") + .description("Asks the client for sampling, so a client without that capability gets -32021") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + samplingText(ctx, "sample").fold(InputRequired("sample" -> sampling("Say hello", 10)))(text => ToolResult.text(text)) + + private val elicitationTool = tool("test_input_required_result_elicitation") + .description("Asks for the user's name with a form elicitation, then greets them") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + answer(ctx, "user_name", "name").fold(InputRequired("user_name" -> askName))(name => ToolResult.text(s"Hello, $name!")) + + private val samplingTool = tool("test_input_required_result_sampling") + .description("Asks the client's LLM a question, then returns its answer") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + samplingText(ctx, "capital_question") + .fold(InputRequired("capital_question" -> sampling("What is the capital of France?", 100)))(text => ToolResult.text(text)) + + private val listRootsTool = tool("test_input_required_result_list_roots") + .description("Asks the client for its roots, then lists them") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + ctx.request + .rootsResult("client_roots") + .fold(InputRequired("client_roots" -> InputRequired.listRoots))(result => + ToolResult.text(s"Roots: ${result.roots.map(_.uri).mkString(", ")}") + ) + + private val multipleInputsTool = tool("test_input_required_result_multiple_inputs") + .description("Asks for a form, a sampling and the roots at once") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + (answer(ctx, "user_name", "name"), samplingText(ctx, "greeting"), ctx.request.rootsResult("client_roots")) match + case (Some(name), Some(greeting), Some(roots)) => + ToolResult.text(s"$greeting, $name! Roots: ${roots.roots.map(_.uri).mkString(", ")}") + case _ => + InputRequired( + "user_name" -> askName, + "greeting" -> sampling("Generate a greeting", 50), + "client_roots" -> InputRequired.listRoots + ).withRequestState("multiple-inputs") + + private val multiRoundTool = tool("test_input_required_result_multi_round") + .description("Asks for the user's name, then for their favourite colour, in two rounds") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + val step1 = InputRequired("step1" -> InputRequired.elicitForm("Step 1: What is your name?", stringSchema("name"))) + val step2 = InputRequired("step2" -> InputRequired.elicitForm("Step 2: What is your favorite color?", stringSchema("color"))) + ctx.request.requestState match + case Some(s"round-2:$name") => + answer(ctx, "step2", "color").fold(step2.withRequestState(s"round-2:$name"))(color => ToolResult.text(s"$name likes $color")) + case Some("round-1") => + answer(ctx, "step1", "name").fold(step1.withRequestState("round-1"))(name => step2.withRequestState(s"round-2:$name")) + case _ => step1.withRequestState("round-1") + + private val capabilitiesTool = tool("test_input_required_result_capabilities") + .description("Asks only for what the client declared: sampling, else a form, else its roots") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + val capabilities = ctx.request.clientCapabilities.getOrElse(ClientCapabilities()) + if capabilities.sampling.isDefined then InputRequired("question" -> sampling("Say hello", 10)) + else if capabilities.elicitation.isDefined then InputRequired("user_name" -> askName) + else if capabilities.roots.isDefined then InputRequired("client_roots" -> InputRequired.listRoots) + else ToolResult.text("The client declared no capability to ask with") + + private val askConfirmation = + InputRequired.elicitForm( + "Please confirm", + Json.obj( + "type" -> Json.fromString("object"), + "properties" -> Json.obj("ok" -> Json.obj("type" -> Json.fromString("boolean"))), + "required" -> Json.arr(Json.fromString("ok")) + ) + ) + + private def confirmed(ctx: ServerContext[Identity]): Boolean = + ctx.request.elicitResult("confirm").flatMap(_.content).flatMap(_.get("ok")).flatMap(_.asBoolean).contains(true) + + private def confirmationTool(name: String, description: String) = tool(name) + .description(description) + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + if ctx.request.requestState.contains("awaiting-confirmation") && confirmed(ctx) then ToolResult.text("state-ok: confirmed") + else InputRequired("confirm" -> askConfirmation).withRequestState("awaiting-confirmation") + + private val requestStateTool = + confirmationTool("test_input_required_result_request_state", "Asks for a confirmation and checks the signed request state on the retry") + + private val tamperedStateTool = + confirmationTool("test_input_required_result_tampered_state", "Asks for a confirmation; a retry with a changed request state fails") + + private val streamingElicitationTool = tool("test_streaming_elicitation") + .description("Reports progress on the response stream, then asks for the user's name") + .input[NoInput] + .streamingServerLogic[Identity]: (_, ctx, _) => + ctx.reportProgress(50.0, Some(100.0)) + answer(ctx, "user_name", "name").fold(InputRequired("user_name" -> askName))(name => ToolResult.text(s"Hello, $name!")) + + private val perRequestLoggingTool = tool("test_logging_tool") + .description("Emits a log notification at each level; a modern request gets only those at or above its _meta logLevel") + .input[NoInput] + .streamingServerLogic[Identity]: (_, ctx, _) => + LoggingLevel.values.foreach(level => ctx.log(level, Json.fromString(s"$level message"))) + ToolResult.text("logging complete") + + private val publisher = NotificationPublisher() + + private val requestStateSecret: Array[Byte] = + val secret = new Array[Byte](32) + SecureRandom().nextBytes(secret) + secret + + private val triggerToolChange = tool("test_trigger_tool_change") + .description("Publishes a tools list change to the open subscriptions") + .input[NoInput] + .handle: _ => + publisher.toolsListChanged() + ToolResult.text("tools list change published") + + private val triggerPromptChange = tool("test_trigger_prompt_change") + .description("Publishes a prompts list change to the open subscriptions") + .input[NoInput] + .handle: _ => + publisher.promptsListChanged() + ToolResult.text("prompts list change published") + private val staticText = resource("test://static-text") .name("static-text") .mimeType("text/plain") @@ -179,20 +332,51 @@ object Main: ) ) + private val inputRequiredPrompt = prompt("test_input_required_result_prompt") + .description("Asks for the context to use before it builds the prompt") + .serverLogicWithContext[Identity]: (_, ctx, _) => + answer(ctx, "user_context", "context").fold( + InputRequired("user_context" -> InputRequired.elicitForm("What context should the prompt use?", stringSchema("context"))) + )(context => GetPromptResult(messages = List(PromptMessage(Role.User, ToolContent.Text(text = s"Use this context: $context"))))) + private val imagePrompt = prompt("test_prompt_with_image") .description("A prompt with an image") .handle(_ => GetPromptResult(messages = List(PromptMessage(Role.User, ToolContent.Image(data = pngData, mimeType = "image/png"))))) private val server = McpServer[Identity](name = "chimp-conformance-server", version = "0.1.0") - .addTools(addNumbers, simpleText, errorTool, imageTool, audioTool, mixedTool, embeddedResourceTool, jsonSchemaTool) + .addTools( + addNumbers, + simpleText, + errorTool, + imageTool, + audioTool, + mixedTool, + embeddedResourceTool, + jsonSchemaTool, + missingCapabilityTool, + elicitationTool, + samplingTool, + listRootsTool, + multipleInputsTool, + multiRoundTool, + capabilitiesTool, + requestStateTool, + tamperedStateTool, + triggerToolChange, + triggerPromptChange, + customHeaderTool + ) .addResources(staticText, staticBinary) .addResourceTemplate(dataTemplate) - .addPrompts(simplePrompt, argsPrompt, embeddedResourcePrompt, imagePrompt) + .addPrompts(simplePrompt, argsPrompt, embeddedResourcePrompt, imagePrompt, inputRequiredPrompt) .withCompletion((_, _, _) => Completion(values = List("alpha", "beta"))) .withLoggingLevel(_ => ()) .withSubscriptions(ResourceSubscriptions[Identity](_ => (), _ => ())) + .withPublisher(publisher) + .withRequestStateSigner(RequestStateSigner(requestStateSecret)) - private val streamingServer = server.streaming.addStreamingTools(progressTool, loggingTool) + private val streamingServer = + server.streaming.addStreamingTools(progressTool, loggingTool, perRequestLoggingTool, streamingElicitationTool) def main(args: Array[String]): Unit = val requestedPort = args @@ -202,6 +386,8 @@ object Main: val endpoint = OxServerHttpTransport(List("mcp")).serve(streamingServer) + val _ = sys.addShutdownHook(publisher.close()) + supervised: val binding = NettySyncServer().port(requestedPort).addEndpoint(endpoint).start() println(s"http://127.0.0.1:${binding.port}/mcp") diff --git a/server-streaming/server-ox/src/main/scala/chimp/server/ox/OxServerHttpTransport.scala b/server-streaming/server-ox/src/main/scala/chimp/server/ox/OxServerHttpTransport.scala index c997361d..77e9f5c5 100644 --- a/server-streaming/server-ox/src/main/scala/chimp/server/ox/OxServerHttpTransport.scala +++ b/server-streaming/server-ox/src/main/scala/chimp/server/ox/OxServerHttpTransport.scala @@ -1,19 +1,25 @@ package chimp.server.ox import chimp.protocol.JSONRPCMessage -import chimp.server.OutboundSink -import chimp.server.transport.ServerStreamingHttpTransport +import chimp.server.{ListenRequest, McpResponse, OutboundSink, SubscriptionOutput} +import chimp.server.transport.{plainJsonError, ServerStreamingHttpTransport} import io.circe.Json import io.circe.syntax.* import ox.* -import ox.channels.Channel +import org.slf4j.LoggerFactory +import ox.channels.{Channel, ChannelClosed} import ox.flow.Flow +import sttp.model.StatusCode import sttp.model.sse.ServerSentEvent import sttp.shared.Identity import sttp.tapir.StreamBodyIO import sttp.tapir.server.netty.sync.{serverSentEventsBody, OxStreams} +import scala.util.control.NonFatal + final class OxServerHttpTransport(path: List[String]) extends ServerStreamingHttpTransport[Identity, OxStreams](path): + private val log = LoggerFactory.getLogger(classOf[OxServerHttpTransport]) + val streams: OxStreams = OxStreams type EventStream = Flow[ServerSentEvent] @@ -22,13 +28,44 @@ final class OxServerHttpTransport(path: List[String]) extends ServerStreamingHtt val emptyStream: EventStream = Flow.empty - def eventStream(handle: OutboundSink[Identity] => Option[Json]): Flow[ServerSentEvent] = + def eventStream(handle: OutboundSink[Identity] => McpResponse): Either[(StatusCode, Json), Flow[ServerSentEvent]] = + val outbound = Channel.buffered[Outbound](64) + val sink = new OutboundSink[Identity]: + def send(message: JSONRPCMessage): Unit = outbound.sendOrClosed(Outbound.Message(message.asJson.deepDropNullValues)).discard + val worker = oxThreadFactory.newThread: () => + try outbound.sendOrClosed(Outbound.Final(handle(sink))).discard + catch case NonFatal(e) => log.warn("Failed to handle the JSON-RPC message", e) + finally outbound.doneOrClosed().discard + worker.start() + + def stream(first: Outbound | ChannelClosed): Flow[ServerSentEvent] = + Flow.usingEmit: emit => + def emitBody(item: Outbound | ChannelClosed): Unit = item match + case Outbound.Message(json) => emit(ServerSentEvent(data = Some(json.noSpaces))) + case Outbound.Final(response) => response.body.foreach(json => emit(ServerSentEvent(data = Some(json.noSpaces)))) + case _: ChannelClosed => () + try + emitBody(first) + outbound.foreach(emitBody) + finally + outbound.doneOrClosed().discard + worker.interrupt() + + outbound.receiveOrClosed() match + case first @ Outbound.Final(response) => plainJsonError(response).toLeft(stream(first)) + case first => Right(stream(first)) + + private enum Outbound: + case Message(json: Json) + case Final(response: McpResponse) + + private[server] def subscriptionStream(listen: ListenRequest): Flow[ServerSentEvent] = Flow.usingEmit: emit => - supervised: - val outbound = Channel.buffered[Json](64) - val sink = new OutboundSink[Identity]: - def send(message: JSONRPCMessage): Unit = outbound.send(message.asJson.deepDropNullValues) - forkDiscard: - try handle(sink).foreach(outbound.send) - finally outbound.done() - outbound.foreach(json => emit(ServerSentEvent(data = Some(json.noSpaces)))) + val outbound = Channel.unlimited[Json] + val subscription = listen.open(new SubscriptionOutput: + def send(message: Json): Unit = + val _ = outbound.sendOrClosed(message) + def close(): Unit = + val _ = outbound.doneOrClosed()) + try outbound.foreach(json => emit(ServerSentEvent(data = Some(json.noSpaces)))) + finally subscription.cancel() diff --git a/server-streaming/server-ox/src/main/scala/chimp/server/ox/OxServerStdioTransport.scala b/server-streaming/server-ox/src/main/scala/chimp/server/ox/OxServerStdioTransport.scala index b52e692c..5142983b 100644 --- a/server-streaming/server-ox/src/main/scala/chimp/server/ox/OxServerStdioTransport.scala +++ b/server-streaming/server-ox/src/main/scala/chimp/server/ox/OxServerStdioTransport.scala @@ -1,8 +1,16 @@ package chimp.server.ox -import chimp.protocol.{JSONRPCMessage, ProgressToken} +import chimp.protocol.JSONRPCMessage import chimp.server.transport.ServerStreamingStdioTransport -import chimp.server.{McpHandler, OutboundSink, SinkStreamingServerContext, StreamingMcpServer, StreamingServerContext} +import chimp.server.{ + McpHandler, + OutboundSink, + RequestInfo, + SinkStreamingServerContext, + StdioSubscriptions, + StreamingMcpServer, + StreamingServerContext +} import chimp.transport.{StdioFraming, StdioLineReader} import io.circe.syntax.* import io.circe.{parser, Json} @@ -24,7 +32,7 @@ final class OxServerStdioTransport( def serve(server: StreamingMcpServer[Identity]): Unit = given MonadError[Identity] = IdentityMonad - val handler = new McpHandler[Identity, StreamingServerContext[Identity]](server) + val handler = new McpHandler[Identity, StreamingServerContext[Identity]](server, canListen = true) val reader = StdioLineReader(in, maxLineLength) val writer = BufferedWriter(OutputStreamWriter(out, StandardCharsets.UTF_8)) @@ -39,15 +47,20 @@ final class OxServerStdioTransport( val sink = new OutboundSink[Identity]: def send(message: JSONRPCMessage): Unit = outbound.send(message.asJson.deepDropNullValues) - val makeContext: Option[ProgressToken] => StreamingServerContext[Identity] = - token => SinkStreamingServerContext(sink, token) - - reader.lines - .filter(_.nonEmpty) - .foreach: line => - parser.parse(line) match - case Right(json) => handler.handleJsonRpc(json, Nil, makeContext).body.foreach(outbound.send) - case Left(error) => log.warn(s"Failed to parse JSON-RPC line: ${error.getMessage}; raw: $line") + val makeContext: RequestInfo => StreamingServerContext[Identity] = + info => SinkStreamingServerContext(sink, info) + + val subscriptions = StdioSubscriptions(handler, json => { val _ = outbound.sendOrClosed(json) }) + + try + reader.lines + .filter(_.nonEmpty) + .foreach: line => + parser.parse(line) match + case Right(json) => + if !subscriptions.accept(json) then handler.handleJsonRpc(json, Nil, makeContext).body.foreach(outbound.send) + case Left(error) => log.warn(s"Failed to parse JSON-RPC line: ${error.getMessage}; raw: $line") + finally subscriptions.endAll() outbound.done() writerFork.join() diff --git a/server-streaming/server-ox/src/test/scala/chimp/server/ox/OxMcpServerHttpSpec.scala b/server-streaming/server-ox/src/test/scala/chimp/server/ox/OxMcpServerHttpSpec.scala index d958c82d..6eeeea24 100644 --- a/server-streaming/server-ox/src/test/scala/chimp/server/ox/OxMcpServerHttpSpec.scala +++ b/server-streaming/server-ox/src/test/scala/chimp/server/ox/OxMcpServerHttpSpec.scala @@ -8,7 +8,10 @@ import chimp.server.transport.SecuredServerStreamingHttpTransport import chimp.server.{ tool, McpServer, + McpServerHeaderHttpTests, + McpServerInputRequiredHttpTests, McpServerStreamingTests, + McpServerSubscriptionHttpTests, McpServerTests, SecuredMcpServer, SecuredMcpServerStreamingTests, @@ -31,15 +34,20 @@ import sttp.tapir.Schema import sttp.tapir.server.netty.sync.{NettySyncServer, OxStreams} import sttp.tapir.server.ServerEndpoint +import java.net.URI import scala.concurrent.Future class OxMcpServerHttpSpec extends McpServerTests[Identity] with McpServerStreamingTests[Identity] + with McpServerSubscriptionHttpTests[Identity] + with McpServerInputRequiredHttpTests[Identity] + with McpServerHeaderHttpTests[Identity] with SecuredMcpServerTests[Identity] with SecuredMcpServerStreamingTests[Identity] with SyncToFuture: private val clientInfo = Implementation("chimp-server-test", "0.0.1") + override protected val clientProtocolVersion: ProtocolVersion = ProtocolVersion.Latest override protected def withServer(server: McpServer[Identity])(test: McpClient[Identity] => Identity[Assertion]): Future[Assertion] = withStreamingServer(server.streaming)(test) @@ -50,10 +58,18 @@ class OxMcpServerHttpSpec withHttpServer(OxServerHttpTransport(List("mcp")).serve(server)): (port, backend, ox) => given Ox = ox val transport = - OxClientHttpTransport(backend, uri"http://localhost:$port/mcp", ProtocolVersion.Latest, ClientTransport.defaultTimeout) - try test(McpClient.bidirectional(transport, clientInfo, protocolVersion = ProtocolVersion.Latest)) + OxClientHttpTransport(backend, uri"http://localhost:$port/mcp", clientProtocolVersion, ClientTransport.defaultTimeout) + try test(McpClient.bidirectional(transport, clientInfo, protocolVersion = clientProtocolVersion)) finally transport.close() + override protected def withStreamingHttpEndpoint(server: StreamingMcpServer[Identity])(test: URI => Assertion): Future[Assertion] = + withHttpServer(OxServerHttpTransport(List("mcp")).serve(server)): (port, _, _) => + test(URI.create(s"http://localhost:$port/mcp")) + + override protected def withHttpEndpoint(server: McpServer[Identity])(test: URI => Assertion): Future[Assertion] = + withHttpServer(server.endpoint(List("mcp"))): (port, _, _) => + test(URI.create(s"http://localhost:$port/mcp")) + override protected def withSecuredServer( server: SecuredMcpServer[Identity, String, String, User], token: String @@ -77,11 +93,11 @@ class OxMcpServerHttpSpec val transport = OxClientHttpTransport( backend, uri"http://localhost:$port/mcp", - ProtocolVersion.Latest, + clientProtocolVersion, ClientTransport.defaultTimeout, headers = List(Header.authorization("Bearer", token)) ) - try test(McpClient.bidirectional(transport, clientInfo, protocolVersion = ProtocolVersion.Latest)) + try test(McpClient.bidirectional(transport, clientInfo, protocolVersion = clientProtocolVersion)) finally transport.close() // 2026-07-28 requests over raw HTTP: chimp's client does not write the modern `_meta` or the SEP-2243 headers yet diff --git a/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoOutbound.scala b/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoOutbound.scala index e3189bb7..738f0aea 100644 --- a/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoOutbound.scala +++ b/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoOutbound.scala @@ -1,6 +1,5 @@ package chimp.server.pekko -import io.circe.Json import org.apache.pekko.stream.QueueOfferResult import org.apache.pekko.stream.scaladsl.SourceQueueWithComplete import org.slf4j.LoggerFactory @@ -13,8 +12,8 @@ private[pekko] object PekkoOutbound: val defaultBufferSize: Int = 64 val defaultMaxConcurrentSends: Int = 64 - def offer(queue: SourceQueueWithComplete[Json], json: Json)(using ExecutionContext): Future[Unit] = - queue.offer(json).flatMap { + def offer[A](queue: SourceQueueWithComplete[A], message: A)(using ExecutionContext): Future[Unit] = + queue.offer(message).flatMap { case QueueOfferResult.Enqueued => Future.unit case QueueOfferResult.Dropped => diff --git a/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoServerHttpTransport.scala b/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoServerHttpTransport.scala index 99f42d20..fdd15226 100644 --- a/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoServerHttpTransport.scala +++ b/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoServerHttpTransport.scala @@ -1,15 +1,16 @@ package chimp.server.pekko import chimp.protocol.JSONRPCMessage -import chimp.server.OutboundSink -import chimp.server.transport.ServerStreamingHttpTransport +import chimp.server.{ListenRequest, McpResponse, OutboundSink, SubscriptionOutput} +import chimp.server.transport.{plainJsonError, ServerStreamingHttpTransport} import io.circe.Json import io.circe.syntax.* import org.apache.pekko.NotUsed -import org.apache.pekko.stream.scaladsl.Source +import org.apache.pekko.stream.scaladsl.{Sink, Source} import org.apache.pekko.stream.{Materializer, OverflowStrategy} import org.slf4j.LoggerFactory import sttp.capabilities.pekko.PekkoStreams +import sttp.model.StatusCode import sttp.model.sse.ServerSentEvent import sttp.tapir.server.pekkohttp.PekkoServerSentEvents import sttp.tapir.{streamTextBody, CodecFormat, StreamBodyIO} @@ -39,21 +40,49 @@ final class PekkoServerHttpTransport( val emptyStream: EventStream = Source.empty[ServerSentEvent] - def eventStream(handle: OutboundSink[Future] => Future[Option[Json]]): Future[EventStream] = - Future.successful: - Source - .queue[Json](outboundBufferSize, OverflowStrategy.backpressure, maxConcurrentSends) - .mapMaterializedValue: queue => - val sink = new OutboundSink[Future]: - def send(message: JSONRPCMessage): Future[Unit] = PekkoOutbound.offer(queue, message.asJson.deepDropNullValues) - Try(handle(sink)) - .fold(Future.failed, identity) - .onComplete: - case Success(response) => - val lastMessage = response.fold(Future.unit)(json => PekkoOutbound.offer(queue, json)) - lastMessage.onComplete(_ => queue.complete()) - case Failure(t) => - log.warn("Failed to handle the JSON-RPC message", t) - queue.complete() - NotUsed - .map(json => ServerSentEvent(data = Some(json.noSpaces))) + def eventStream(handle: OutboundSink[Future] => Future[McpResponse]): Future[Either[(StatusCode, Json), EventStream]] = + val (queue, outbound) = + Source.queue[Outbound](outboundBufferSize, OverflowStrategy.backpressure, maxConcurrentSends).preMaterialize() + val sink = new OutboundSink[Future]: + def send(message: JSONRPCMessage): Future[Unit] = PekkoOutbound.offer(queue, Outbound.Message(message.asJson.deepDropNullValues)) + Try(handle(sink)) + .fold(Future.failed, identity) + .onComplete: + case Success(response) => PekkoOutbound.offer(queue, Outbound.Final(response)).onComplete(_ => queue.complete()) + case Failure(t) => + log.warn("Failed to handle the JSON-RPC message", t) + queue.complete() + outbound + .prefixAndTail(1) + .runWith(Sink.head) + .map: (first, rest) => + val stream = (Source(first) ++ rest) + .mapConcat: + case Outbound.Message(json) => List(json) + case Outbound.Final(response) => response.body.toList + .map(json => ServerSentEvent(data = Some(json.noSpaces))) + first.headOption match + case Some(Outbound.Final(response)) => + plainJsonError(response) match + case Some(error) => + val _ = rest.runWith(Sink.ignore) + Left(error) + case None => Right(stream) + case _ => Right(stream) + + private[server] def subscriptionStream(listen: ListenRequest): EventStream = + Source + .queue[Json](outboundBufferSize, OverflowStrategy.backpressure, maxConcurrentSends) + .mapMaterializedValue: queue => + listen.open(new SubscriptionOutput: + def send(message: Json): Unit = + val _ = PekkoOutbound.offer(queue, message) + def close(): Unit = queue.complete()) + .watchTermination(): (subscription, done) => + done.onComplete(_ => subscription.cancel()) + NotUsed + .map(json => ServerSentEvent(data = Some(json.noSpaces))) + + private enum Outbound: + case Message(json: Json) + case Final(response: McpResponse) diff --git a/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoServerStdioTransport.scala b/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoServerStdioTransport.scala index ccc840f1..734bb927 100644 --- a/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoServerStdioTransport.scala +++ b/server-streaming/server-pekko/src/main/scala/chimp/server/pekko/PekkoServerStdioTransport.scala @@ -1,8 +1,16 @@ package chimp.server.pekko -import chimp.protocol.{JSONRPCMessage, ProgressToken} +import chimp.protocol.JSONRPCMessage import chimp.server.transport.ServerStreamingStdioTransport -import chimp.server.{McpHandler, OutboundSink, SinkStreamingServerContext, StreamingMcpServer, StreamingServerContext} +import chimp.server.{ + McpHandler, + OutboundSink, + RequestInfo, + SinkStreamingServerContext, + StdioSubscriptions, + StreamingMcpServer, + StreamingServerContext +} import chimp.transport.{McpLineTooLongException, StdioFraming} import io.circe.syntax.* import io.circe.{parser, Json} @@ -28,7 +36,7 @@ final class PekkoServerStdioTransport( private given MonadError[Future] = FutureMonad() def serve(server: StreamingMcpServer[Future]): Future[Unit] = - val handler = new McpHandler[Future, StreamingServerContext[Future]](server) + val handler = new McpHandler[Future, StreamingServerContext[Future]](server, canListen = true) val (outbound, writing) = Source .queue[Json](PekkoOutbound.defaultBufferSize, OverflowStrategy.backpressure, PekkoOutbound.defaultMaxConcurrentSends) @@ -39,8 +47,10 @@ final class PekkoServerStdioTransport( val sink = new OutboundSink[Future]: def send(message: JSONRPCMessage): Future[Unit] = PekkoOutbound.offer(outbound, message.asJson.deepDropNullValues) - val makeContext: Option[ProgressToken] => StreamingServerContext[Future] = - token => SinkStreamingServerContext(sink, token) + val makeContext: RequestInfo => StreamingServerContext[Future] = + info => SinkStreamingServerContext(sink, info) + + val subscriptions = StdioSubscriptions(handler, json => { val _ = PekkoOutbound.offer(outbound, json) }) val reading = StreamConverters .fromInputStream(() => in) @@ -48,24 +58,28 @@ final class PekkoServerStdioTransport( .mapError { case _: Framing.FramingException => McpLineTooLongException(maxLineLength) } .map(_.utf8String.trim) .filter(_.nonEmpty) - .mapAsync(1)(line => handleLine(handler, makeContext, outbound, line)) + .mapAsync(1)(line => handleLine(handler, subscriptions, makeContext, outbound, line)) .runWith(Sink.ignore) reading.transformWith: result => + subscriptions.endAll() outbound.complete() writing.transform(_ => result.map(_ => ())) private def handleLine( handler: McpHandler[Future, StreamingServerContext[Future]], - makeContext: Option[ProgressToken] => StreamingServerContext[Future], + subscriptions: StdioSubscriptions, + makeContext: RequestInfo => StreamingServerContext[Future], outbound: SourceQueueWithComplete[Json], line: String ): Future[Unit] = parser.parse(line) match case Right(json) => - handler - .handleJsonRpc(json, Nil, makeContext) - .flatMap(response => response.body.fold(Future.unit)(body => PekkoOutbound.offer(outbound, body))) + if subscriptions.accept(json) then Future.unit + else + handler + .handleJsonRpc(json, Nil, makeContext) + .flatMap(response => response.body.fold(Future.unit)(body => PekkoOutbound.offer(outbound, body))) case Left(error) => log.warn(s"Failed to parse JSON-RPC line: ${error.getMessage}; raw: $line") Future.unit diff --git a/server-streaming/server-pekko/src/test/scala/chimp/server/pekko/PekkoMcpServerHttpSpec.scala b/server-streaming/server-pekko/src/test/scala/chimp/server/pekko/PekkoMcpServerHttpSpec.scala index f3c09448..141674e5 100644 --- a/server-streaming/server-pekko/src/test/scala/chimp/server/pekko/PekkoMcpServerHttpSpec.scala +++ b/server-streaming/server-pekko/src/test/scala/chimp/server/pekko/PekkoMcpServerHttpSpec.scala @@ -2,11 +2,14 @@ package chimp.server.pekko import chimp.client.transport.pekko.PekkoClientHttpTransport import chimp.client.{BidirectionalMcpClient, McpClient} -import chimp.protocol.Implementation +import chimp.protocol.{Implementation, ProtocolVersion} import chimp.server.transport.SecuredServerStreamingHttpTransport import chimp.server.{ McpServer, + McpServerHeaderHttpTests, + McpServerInputRequiredHttpTests, McpServerStreamingTests, + McpServerSubscriptionHttpTests, McpServerTests, SecuredMcpServer, SecuredMcpServerStreamingTests, @@ -24,16 +27,21 @@ import sttp.model.Uri.UriContext import sttp.tapir.server.pekkohttp.PekkoHttpServerInterpreter import sttp.tapir.server.ServerEndpoint +import java.net.URI import scala.concurrent.duration.DurationInt -import scala.concurrent.{ExecutionContext, Future} +import scala.concurrent.{blocking, ExecutionContext, Future} class PekkoMcpServerHttpSpec extends McpServerTests[Future] with McpServerStreamingTests[Future] + with McpServerSubscriptionHttpTests[Future] + with McpServerInputRequiredHttpTests[Future] + with McpServerHeaderHttpTests[Future] with SecuredMcpServerTests[Future] with SecuredMcpServerStreamingTests[Future] with PekkoToFuture: private val clientInfo = Implementation("chimp-server-test", "0.0.1") + override protected val clientProtocolVersion: ProtocolVersion = ProtocolVersion.LatestLegacy override protected def withServer(server: McpServer[Future])(test: McpClient[Future] => Future[Assertion]): Future[Assertion] = withStreamingServer(server.streaming)(test) @@ -44,6 +52,21 @@ class PekkoMcpServerHttpSpec withHttpServer(PekkoServerHttpTransport(List("mcp")).serve(server), Nil): transport => McpClient.bidirectional(transport, clientInfo).flatMap(test) + override protected def withStreamingHttpEndpoint(server: StreamingMcpServer[Future])(test: URI => Assertion): Future[Assertion] = + withEndpointUri(PekkoServerHttpTransport(List("mcp")).serve(server))(test) + + override protected def withHttpEndpoint(server: McpServer[Future])(test: URI => Assertion): Future[Assertion] = + withEndpointUri(server.endpoint(List("mcp")))(test) + + private def withEndpointUri(endpoint: ServerEndpoint[PekkoStreams & WebSockets, Future])(test: URI => Assertion): Future[Assertion] = + given ExecutionContext = actorSystem.dispatcher + Http() + .newServerAt("localhost", 0) + .bind(PekkoHttpServerInterpreter().toRoute(endpoint)) + .flatMap: binding => + Future(blocking(test(URI.create(s"http://localhost:${binding.localAddress.getPort}/mcp")))) + .transformWith(result => binding.terminate(5.seconds).transform(_ => result)) + override protected def withSecuredServer( server: SecuredMcpServer[Future, String, String, User], token: String diff --git a/server-streaming/server-zio/src/main/scala/chimp/server/zio/ZioServerHttpTransport.scala b/server-streaming/server-zio/src/main/scala/chimp/server/zio/ZioServerHttpTransport.scala index 9b2c4d6e..27766bb2 100644 --- a/server-streaming/server-zio/src/main/scala/chimp/server/zio/ZioServerHttpTransport.scala +++ b/server-streaming/server-zio/src/main/scala/chimp/server/zio/ZioServerHttpTransport.scala @@ -1,16 +1,17 @@ package chimp.server.zio import chimp.protocol.JSONRPCMessage -import chimp.server.OutboundSink -import chimp.server.transport.ServerStreamingHttpTransport +import chimp.server.{ListenRequest, McpResponse, OutboundSink, SubscriptionOutput} +import chimp.server.transport.{plainJsonError, ServerStreamingHttpTransport} import io.circe.Json import io.circe.syntax.* import sttp.capabilities.zio.ZioStreams +import sttp.model.StatusCode import sttp.model.sse.ServerSentEvent import sttp.tapir.* import sttp.tapir.ztapir.ZioServerSentEvents import zio.stream.{Stream, ZStream} -import zio.{Queue, Task, ZIO} +import zio.{Queue, Task, Unsafe, ZIO} import java.nio.charset.StandardCharsets @@ -25,23 +26,47 @@ final class ZioServerHttpTransport(path: List[String]) extends ServerStreamingHt val emptyStream: EventStream = ZStream.empty - def eventStream(handle: OutboundSink[Task] => Task[Option[Json]]): Task[EventStream] = - ZIO.succeed { - ZStream.unwrapScoped { - for - queue <- Queue.unbounded[Outbound] - sink = new OutboundSink[Task]: - def send(message: JSONRPCMessage): Task[Unit] = - queue.offer(Outbound.Message(message.asJson)).unit - _ <- handle(sink) - .flatMap(response => ZIO.foreachDiscard(response)(json => queue.offer(Outbound.Message(json)))) - .ensuring(queue.offer(Outbound.Close)) - .catchAllCause(_ => ZIO.unit) - .forkScoped - yield ZStream.fromQueue(queue).collectWhile { case Outbound.Message(json) => ServerSentEvent(data = Some(json.noSpaces)) } - } + def eventStream(handle: OutboundSink[Task] => Task[McpResponse]): Task[Either[(StatusCode, Json), EventStream]] = + for + queue <- Queue.unbounded[Outbound] + sink = new OutboundSink[Task]: + def send(message: JSONRPCMessage): Task[Unit] = + queue.offer(Outbound.Message(message.asJson)).unit + worker <- handle(sink) + .flatMap(response => queue.offer(Outbound.Final(response))) + .catchAllCause(cause => ZIO.logWarningCause("Failed to handle the JSON-RPC message", cause)) + .ensuring(queue.offer(Outbound.Close)) + .forkDaemon + first <- queue.take + yield + val stream = (ZStream.succeed(first) ++ ZStream.fromQueue(queue)) + .collectWhile: + case Outbound.Message(json) => Some(json) + case Outbound.Final(response) => response.body + .collectSome + .map(json => ServerSentEvent(data = Some(json.noSpaces))) + .ensuring(worker.interrupt) + first match + case Outbound.Final(response) => plainJsonError(response).toLeft(stream) + case _ => Right(stream) + + private[server] def subscriptionStream(listen: ListenRequest): EventStream = + ZStream.unwrapScoped { + for + queue <- ZIO.acquireRelease(Queue.unbounded[Outbound])(_.shutdown) + runtime <- ZIO.runtime[Any] + output = new SubscriptionOutput: + def send(message: Json): Unit = offer(Outbound.Message(message)) + def close(): Unit = offer(Outbound.Close) + private def offer(outbound: Outbound): Unit = + Unsafe.unsafe { implicit unsafe => + val _ = runtime.unsafe.run(queue.offer(outbound)) + } + _ <- ZIO.acquireRelease(ZIO.succeed(listen.open(output)))(subscription => ZIO.succeed(subscription.cancel())) + yield ZStream.fromQueue(queue).collectWhile { case Outbound.Message(json) => ServerSentEvent(data = Some(json.noSpaces)) } } private enum Outbound: case Message(json: Json) + case Final(response: McpResponse) case Close diff --git a/server-streaming/server-zio/src/main/scala/chimp/server/zio/ZioServerStdioTransport.scala b/server-streaming/server-zio/src/main/scala/chimp/server/zio/ZioServerStdioTransport.scala index dc3710ba..12aadfd6 100644 --- a/server-streaming/server-zio/src/main/scala/chimp/server/zio/ZioServerStdioTransport.scala +++ b/server-streaming/server-zio/src/main/scala/chimp/server/zio/ZioServerStdioTransport.scala @@ -1,7 +1,15 @@ package chimp.server.zio -import chimp.protocol.{JSONRPCMessage, ProgressToken} -import chimp.server.{McpHandler, OutboundSink, SinkStreamingServerContext, StreamingMcpServer, StreamingServerContext} +import chimp.protocol.JSONRPCMessage +import chimp.server.{ + McpHandler, + OutboundSink, + RequestInfo, + SinkStreamingServerContext, + StdioSubscriptions, + StreamingMcpServer, + StreamingServerContext +} import chimp.server.transport.ServerStreamingStdioTransport import chimp.transport.{StdioFraming, StdioLineReader} import io.circe.syntax.* @@ -23,21 +31,25 @@ final class ZioServerStdioTransport( private given MonadError[Task] = new RIOMonadError[Any] def serve(server: StreamingMcpServer[Task]): Task[Unit] = - val handler = new McpHandler[Task, StreamingServerContext[Task]](server) + val handler = new McpHandler[Task, StreamingServerContext[Task]](server, canListen = true) val reader = StdioLineReader(in, maxLineLength) val writer = BufferedWriter(OutputStreamWriter(out, StandardCharsets.UTF_8)) - def writeLine(json: Json): Task[Unit] = - ZIO.attemptBlocking: + def write(json: Json): Unit = + writer.synchronized: writer.write(json.noSpaces) writer.newLine() writer.flush() + def writeLine(json: Json): Task[Unit] = ZIO.attemptBlocking(write(json)) + + val subscriptions = StdioSubscriptions(handler, write) + val sink = new OutboundSink[Task]: def send(message: JSONRPCMessage): Task[Unit] = writeLine(message.asJson.deepDropNullValues) - val makeContext: Option[ProgressToken] => StreamingServerContext[Task] = - token => SinkStreamingServerContext(sink, token) + val makeContext: RequestInfo => StreamingServerContext[Task] = + info => SinkStreamingServerContext(sink, info) def loop: Task[Unit] = ZIO @@ -50,9 +62,13 @@ final class ZioServerStdioTransport( else parser.parse(line) match case Right(json) => - handler.handleJsonRpc(json, Nil, makeContext).flatMap(response => ZIO.foreachDiscard(response.body)(writeLine)) + ZIO + .attemptBlocking(subscriptions.accept(json)) + .flatMap: accepted => + if accepted then ZIO.unit + else handler.handleJsonRpc(json, Nil, makeContext).flatMap(response => ZIO.foreachDiscard(response.body)(writeLine)) case Left(error) => ZIO.succeed(log.warn(s"Failed to parse JSON-RPC line: ${error.getMessage}; raw: $line")) handled *> loop - loop + loop.ensuring(ZIO.attemptBlocking(subscriptions.endAll()).ignore) diff --git a/server-streaming/server-zio/src/test/scala/chimp/server/zio/ZioMcpServerHttpSpec.scala b/server-streaming/server-zio/src/test/scala/chimp/server/zio/ZioMcpServerHttpSpec.scala index a500fa28..d369e38c 100644 --- a/server-streaming/server-zio/src/test/scala/chimp/server/zio/ZioMcpServerHttpSpec.scala +++ b/server-streaming/server-zio/src/test/scala/chimp/server/zio/ZioMcpServerHttpSpec.scala @@ -7,7 +7,10 @@ import chimp.protocol.{Implementation, ProtocolVersion} import chimp.server.transport.SecuredServerStreamingHttpTransport import chimp.server.{ McpServer, + McpServerHeaderHttpTests, + McpServerInputRequiredHttpTests, McpServerStreamingTests, + McpServerSubscriptionHttpTests, McpServerTests, SecuredMcpServer, SecuredMcpServerStreamingTests, @@ -26,15 +29,20 @@ import sttp.tapir.server.ServerEndpoint import zio.http.Server import zio.{Scope, Task, ZIO} +import java.net.URI import scala.concurrent.Future class ZioMcpServerHttpSpec extends McpServerTests[Task] with McpServerStreamingTests[Task] + with McpServerSubscriptionHttpTests[Task] + with McpServerInputRequiredHttpTests[Task] + with McpServerHeaderHttpTests[Task] with SecuredMcpServerTests[Task] with SecuredMcpServerStreamingTests[Task] with ZioToFuture: private val clientInfo = Implementation("chimp-server-test", "0.0.1") + override protected val clientProtocolVersion: ProtocolVersion = ProtocolVersion.Latest override protected def withServer(server: McpServer[Task])(test: McpClient[Task] => Task[Assertion]): Future[Assertion] = withStreamingServer(server.streaming)(test) @@ -43,14 +51,28 @@ class ZioMcpServerHttpSpec server: StreamingMcpServer[Task] )(test: BidirectionalMcpClient[Task] => Task[Assertion]): Future[Assertion] = withHttpServer(ZioServerHttpTransport(List("mcp")).serve(server), Nil): transport => - McpClient.bidirectional(transport, clientInfo, protocolVersion = ProtocolVersion.Latest).flatMap(test) + McpClient.bidirectional(transport, clientInfo, protocolVersion = clientProtocolVersion).flatMap(test) + + override protected def withStreamingHttpEndpoint(server: StreamingMcpServer[Task])(test: URI => Assertion): Future[Assertion] = + withEndpointUri(ZioServerHttpTransport(List("mcp")).serve(server))(test) + + override protected def withHttpEndpoint(server: McpServer[Task])(test: URI => Assertion): Future[Assertion] = + withEndpointUri(server.endpoint(List("mcp")))(test) + + private def withEndpointUri(endpoint: ServerEndpoint[ZioStreams & WebSockets, Task])(test: URI => Assertion): Future[Assertion] = + toFuture: + val routes = ZioHttpInterpreter().toHttp(endpoint) + (for + port <- Server.install(routes) + result <- ZIO.attemptBlocking(test(URI.create(s"http://localhost:$port/mcp"))) + yield result).provide(Server.defaultWithPort(0)) override protected def withSecuredServer( server: SecuredMcpServer[Task, String, String, User], token: String )(test: McpClient[Task] => Task[Assertion]): Future[Assertion] = withHttpServer(server.endpoint(List("mcp")), List(Header.authorization("Bearer", token))): transport => - McpClient(transport, clientInfo, ProtocolVersion.Latest).flatMap(test) + McpClient(transport, clientInfo, clientProtocolVersion).flatMap(test) override protected def withSecuredStreamingServer( server: SecuredStreamingMcpServer[Task, String, String, User], @@ -58,7 +80,7 @@ class ZioMcpServerHttpSpec )(test: BidirectionalMcpClient[Task] => Task[Assertion]): Future[Assertion] = val endpoint = SecuredServerStreamingHttpTransport(List("mcp"), ZioServerHttpTransport(List("mcp"))).serve(server) withHttpServer(endpoint, List(Header.authorization("Bearer", token))): transport => - McpClient.bidirectional(transport, clientInfo, protocolVersion = ProtocolVersion.Latest).flatMap(test) + McpClient.bidirectional(transport, clientInfo, protocolVersion = clientProtocolVersion).flatMap(test) private def withHttpServer( endpoint: ServerEndpoint[ZioStreams & WebSockets, Task], @@ -74,7 +96,7 @@ class ZioMcpServerHttpSpec .scoped( backend, uri"http://localhost:$port/mcp", - ProtocolVersion.Latest, + clientProtocolVersion, ClientTransport.defaultTimeout, headers = headers ) diff --git a/server/src/main/scala/chimp/server/InputRequired.scala b/server/src/main/scala/chimp/server/InputRequired.scala new file mode 100644 index 00000000..d5b0c9f6 --- /dev/null +++ b/server/src/main/scala/chimp/server/InputRequired.scala @@ -0,0 +1,68 @@ +package chimp.server + +import chimp.protocol.* +import io.circe.Json + +/** The outcome of a tool, prompt or resource handler which needs input from the client before it can finish (multi round-trip requests, + * 2026-07-28). The client answers each request under its key, echoes the request state, and retries the same request; the handler reads + * the answers from [[RequestInfo.inputResponses]] and the state from [[RequestInfo.requestState]]. It always carries at least one input + * request or a request state. + */ +final case class InputRequired private (requests: Map[String, InputRequest], requestState: Option[String]): + /** Adds an input request under `key`; a request already under `key` is replaced. */ + def and(key: String, request: InputRequest): InputRequired = copy(requests = requests + (key -> request)) + + /** Sets the opaque state which the client echoes on its retry. */ + def withRequestState(state: String): InputRequired = copy(requestState = Some(state)) + + private[server] def mapRequestState(f: String => String): InputRequired = copy(requestState = requestState.map(f)) + +object InputRequired: + def apply(first: (String, InputRequest), rest: (String, InputRequest)*): InputRequired = + new InputRequired((first +: rest).toMap, None) + + /** An outcome without input requests: the client retries at once, with `state`. */ + def withRequestState(state: String): InputRequired = new InputRequired(Map.empty, Some(state)) + + /** Asks the user to fill in a form described by a JSON Schema; the client must declare the `elicitation` (form) capability. */ + def elicitForm(message: String, requestedSchema: Json): InputRequest = + InputRequest.Elicit(ElicitRequest(params = ElicitParams.Form(message, requestedSchema))) + + /** Asks the user to complete an interaction at `url`, out of band; the client must declare the `elicitation.url` capability. */ + def elicitUrl(message: String, url: String): InputRequest = + InputRequest.Elicit(ElicitRequest(params = ElicitParams.Url(message, url))) + + /** Asks the client to sample its LLM; the client must declare the `sampling` capability. */ + def sample(params: CreateMessageParams): InputRequest = InputRequest.Sampling(CreateMessageRequest(params = params)) + + /** Asks the client for its roots; the client must declare the `roots` capability. */ + val listRoots: InputRequest = InputRequest.Roots(ListRootsRequest()) + + /** The client capabilities which `request` needs. */ + private def capabilitiesFor(request: InputRequest): ClientCapabilities = request match + case InputRequest.Sampling(_) => ClientCapabilities(sampling = Some(Json.obj())) + case InputRequest.Roots(_) => ClientCapabilities(roots = Some(ClientRootsCapability())) + case InputRequest.Elicit(ElicitRequest(_, _: ElicitParams.Form)) => + ClientCapabilities(elicitation = Some(Json.obj("form" -> Json.obj()))) + case InputRequest.Elicit(ElicitRequest(_, _: ElicitParams.Url)) => ClientCapabilities(elicitation = Some(Json.obj("url" -> Json.obj()))) + + /** Whether `declared` covers what `request` needs. An empty `elicitation` object means form mode only, as in 2025-11-25. */ + private[server] def isDeclared(request: InputRequest, declared: ClientCapabilities): Boolean = + def elicitationHas(mode: String, emptyMeans: Boolean): Boolean = + declared.elicitation.flatMap(_.asObject).exists(modes => modes.contains(mode) || (modes.isEmpty && emptyMeans)) + request match + case InputRequest.Sampling(_) => declared.sampling.isDefined + case InputRequest.Roots(_) => declared.roots.isDefined + case InputRequest.Elicit(ElicitRequest(_, _: ElicitParams.Form)) => elicitationHas("form", emptyMeans = true) + case InputRequest.Elicit(ElicitRequest(_, _: ElicitParams.Url)) => elicitationHas("url", emptyMeans = false) + + /** The client capabilities which `requests` need together, as `requiredCapabilities` of a `-32021` error names them. */ + private[server] def requiredCapabilities(requests: Iterable[InputRequest]): ClientCapabilities = + requests + .map(capabilitiesFor) + .foldLeft(ClientCapabilities()): (all, one) => + ClientCapabilities( + roots = all.roots.orElse(one.roots), + sampling = all.sampling.orElse(one.sampling), + elicitation = (all.elicitation ++ one.elicitation).reduceOption(_.deepMerge(_)) + ) diff --git a/server/src/main/scala/chimp/server/McpHandler.scala b/server/src/main/scala/chimp/server/McpHandler.scala index 38b066dc..63d7a7b1 100644 --- a/server/src/main/scala/chimp/server/McpHandler.scala +++ b/server/src/main/scala/chimp/server/McpHandler.scala @@ -4,11 +4,9 @@ import chimp.protocol.* import io.circe.* import io.circe.syntax.* import org.slf4j.LoggerFactory -import sttp.apispec.circe.* import sttp.model.{Header, StatusCode} import sttp.monad.MonadError import sttp.monad.syntax.* -import sttp.tapir.docs.apispec.schema.TapirSchemaToJsonSchema enum McpResponse: case JsonResponse(json: Json) @@ -30,8 +28,13 @@ enum McpResponse: case JsonError(status, json) => JsonError(status, json.deepDropNullValues) case EmptyAcceptResponse => this -private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerDef[F, C]): +/** @param canListen + * Whether the transport can hold a `subscriptions/listen` stream open. Without it the server neither serves `subscriptions/listen` nor + * advertises the capabilities which need it. + */ +private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerDef[F, C], canListen: Boolean = false): private val logger = LoggerFactory.getLogger(classOf[McpHandler[?, ?]]) + private val listenPublisher = server.publisher.filter(_ => canListen) private val toolsByName = server.tools.map(tool => tool.name -> tool).toMap private val promptsByName = server.prompts.map(prompt => prompt.definition.name -> prompt).toMap private val resourcesByUri = server.resources.map(resource => resource.definition.uri -> resource).toMap @@ -39,17 +42,13 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD private val hasPrompts = server.prompts.nonEmpty private val toolDefinitions = server.tools.map(toolToDefinition) - private def toJsonSchema(toolSchema: ToolSchema): Json = toolSchema match - case ToolSchema.Derived(schema) => - val base = TapirSchemaToJsonSchema(schema, markOptionsAsNullable = false) - (if server.showJsonSchemaMetadata then base else base.copy($schema = None)).asJson - case ToolSchema.Raw(json) => json + private def toJsonSchema(toolSchema: ToolSchema): Json = McpHeaders.render(toolSchema, server.showJsonSchemaMetadata) private def toolToDefinition(tool: ServerTool[?, ?, F, C]): ToolDefinition = ToolDefinition( name = tool.name, description = tool.description, - inputSchema = toJsonSchema(tool.inputSchema), + inputSchema = McpHeaders.annotated(toJsonSchema(tool.inputSchema), tool.mcpHeaders).getOrElse(toJsonSchema(tool.inputSchema)), outputSchema = tool.outputSchema.map(toJsonSchema), annotations = tool.annotations .map(annotation => @@ -63,15 +62,15 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD ) ) - def handleJsonRpc(request: Json, headers: Seq[Header], makeContext: Option[ProgressToken] => C)(using MonadError[F]): F[McpResponse] = + def handleJsonRpc(request: Json, headers: Seq[Header], makeContext: RequestInfo => C)(using MonadError[F]): F[McpResponse] = doHandleJsonRpc(request, headers, makeContext).map: response => logger.debug(s"Request: $request, response: ${response.statusCode}, body: ${response.body.getOrElse(Json.Null)}") response.withNullsDroppedDeep def handleJsonRpc(request: Json, headers: Seq[Header])(using m: MonadError[F], ev: ServerContext[F] <:< C): F[McpResponse] = - handleJsonRpc(request, headers, _ => ev(ServerContext.noop[F])) + handleJsonRpc(request, headers, info => ev(ServerContext[F](info))) - private def doHandleJsonRpc(request: Json, headers: Seq[Header], makeContext: Option[ProgressToken] => C)(using + private def doHandleJsonRpc(request: Json, headers: Seq[Header], makeContext: RequestInfo => C)(using MonadError[F] ): F[McpResponse] = request.as[JSONRPCMessage] match @@ -80,7 +79,10 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD case Right(JSONRPCMessage.Request(_, method, params: Option[Json], id)) => requestRejection(method, params, headers, id) match case Some(rejection) => rejection.unit - case None => dispatch(method, params, id, headers, makeContext, isModernRequest(method, params, headers)) + case None => + val isModern = isModernRequest(params, headers) + val info = requestInfo(params, headers, isModern) + dispatch(method, params, id, headers, info, makeContext, isModern) case Right(notification: JSONRPCMessage.Notification) => logger.debug(s"Received notification: ${notification.method}") McpResponse.EmptyAcceptResponse.unit @@ -93,13 +95,16 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD params: Option[Json], id: RequestId, headers: Seq[Header], - makeContext: Option[ProgressToken] => C, + info: RequestInfo, + makeContext: RequestInfo => C, isModern: Boolean )(using MonadError[F]): F[McpResponse] = def wrap(cacheHints: Option[CacheHints])(message: JSONRPCMessage): JSONRPCMessage = message match case JSONRPCMessage.Response(jsonrpc, rid, result) if isModern => JSONRPCMessage.Response(jsonrpc, rid, modernResult(result, cacheHints)) case other => other + def reply(cacheHints: Option[CacheHints])(reply: Reply): McpResponse = + reply.fold(identity, message => jsonResponse(wrap(cacheHints)(message))) val cacheHints = Some(CacheHints.Default) method match case "initialize" => @@ -111,7 +116,7 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD case "tools/list" => jsonResponse(wrap(cacheHints)(JSONRPCMessage.Response(id = id, result = ListToolsResponse(toolDefinitions).asJson))).unit case "tools/call" => - handleToolsCall(params, id, headers, makeContext).map(wrap(None)).map(jsonResponse) + handleToolsCall(params, id, headers, info, makeContext, isModern).map(reply(None)) case "resources/list" if hasResources => jsonResponse( wrap(cacheHints)(JSONRPCMessage.Response(id = id, result = ListResourcesResult(server.resources.map(_.definition)).asJson)) @@ -123,7 +128,7 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD ) ).unit case "resources/read" if hasResources => - handleResourcesRead(params, id, headers, makeContext).map(wrap(cacheHints)).map(jsonResponse) + handleResourcesRead(params, id, headers, info, makeContext, isModern).map(reply(cacheHints)) case "resources/subscribe" if server.subscriptions.isDefined => handleSubscribe(params, id, subscribe = true).map(jsonResponse) case "resources/unsubscribe" if server.subscriptions.isDefined => @@ -133,18 +138,23 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD wrap(cacheHints)(JSONRPCMessage.Response(id = id, result = ListPromptsResult(server.prompts.map(_.definition)).asJson)) ).unit case "prompts/get" if hasPrompts => - handlePromptsGet(params, id, headers, makeContext).map(wrap(None)).map(jsonResponse) + handlePromptsGet(params, id, headers, info, makeContext, isModern).map(reply(None)) case "completion/complete" if server.completion.isDefined => handleComplete(params, id).map(wrap(None)).map(jsonResponse) case "logging/setLevel" if server.loggingLevel.isDefined => handleSetLoggingLevel(params, id).map(jsonResponse) + case "subscriptions/listen" if listenPublisher.isDefined => + jsonResponse(protocolError(id, JSONRPCErrorCodes.InvalidParams.code, "Invalid or missing params")).unit case other => jsonResponse(protocolError(id, JSONRPCErrorCodes.MethodNotFound.code, s"Unknown method: $other")).unit + /** Adds the modern envelope fields to a result. An input-required result already names its type and is never cached. */ private def modernResult(result: Json, cacheHints: Option[CacheHints]): Json = val meta = result.hcursor.downField("_meta").focus.getOrElse(Json.obj()).deepMerge(serverInfoMeta) - val enriched = result.deepMerge(Json.obj("resultType" -> ResultType.Complete.asJson, "_meta" -> meta)) - cacheHints.fold(enriched)(hints => enriched.deepMerge(hints.asJson)) + if result.hcursor.get[ResultType]("resultType").contains(ResultType.InputRequired) then result.deepMerge(Json.obj("_meta" -> meta)) + else + val enriched = result.deepMerge(Json.obj("resultType" -> ResultType.Complete.asJson, "_meta" -> meta)) + cacheHints.fold(enriched)(hints => enriched.deepMerge(hints.asJson)) private def protocolError(id: RequestId, code: Int, message: String, data: Option[Json] = None): JSONRPCMessage.Error = logger.debug(s"Protocol error (id=$id, code=$code): $message") @@ -152,9 +162,37 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD private def jsonResponse(message: JSONRPCMessage): McpResponse = McpResponse.JsonResponse(message.asJson) - // request methods a 2026-07-28 client may call (server/discover is handled separately as the era-agnostic bootstrap) + /** The reply of a handler which may end with [[InputRequired]]: a JSON-RPC message, or a response with its own HTTP status. */ + private type Reply = Either[McpResponse, JSONRPCMessage] + + /** Applies the server-enforced input-required rules: only a modern request can get it, and only for the input requests which the client + * declared the capabilities for (otherwise a `-32021` with HTTP 400). + */ + private def inputRequiredReply(id: RequestId, outcome: InputRequired, info: RequestInfo, isModern: Boolean): Reply = + if !isModern then + Right( + protocolError( + id, + JSONRPCErrorCodes.InternalError.code, + s"The handler asked for client input, which needs protocol version ${ProtocolVersion.V2026_07_28.name} or later" + ) + ) + else + val declared = info.clientCapabilities.getOrElse(ClientCapabilities()) + val undeclared = outcome.requests.values.filterNot(InputRequired.isDeclared(_, declared)) + if undeclared.nonEmpty then + val required = InputRequired.requiredCapabilities(undeclared) + logger.debug(s"Input required rejected (id=$id): missing client capabilities ${required.asJson.deepDropNullValues.noSpaces}") + val envelope: JSONRPCMessage = JSONRPCMessage.Error(id = id, error = ProtocolMeta.missingClientCapabilityError(required)) + Left(McpResponse.JsonError(StatusCode.BadRequest, envelope.asJson)) + else + val result = InputRequiredResult(Option.when(outcome.requests.nonEmpty)(outcome.requests), outcome.requestState) + Right(JSONRPCMessage.Response(id = id, result = result.asJson)) + + // request methods a 2026-07-28 client may call private val modernMethods: Set[String] = Set( + "server/discover", "tools/list", "tools/call", "resources/list", @@ -163,7 +201,7 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD "prompts/list", "prompts/get", "completion/complete" - ) + ) ++ listenPublisher.map(_ => "subscriptions/listen") private def jsonError(id: RequestId, status: StatusCode, code: Int, message: String, data: Option[Json] = None): McpResponse = logger.debug(s"Modern request rejected (id=$id, http=${status.code}, code=$code): $message") @@ -172,13 +210,32 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD /** Whether the request follows the modern (2026-07-28) rules: it declares a modern version in `_meta` or via the `MCP-Protocol-Version` * header. A known legacy version in `_meta` keeps the legacy rules; an unknown one is treated as modern, so it gets the modern `-32022` - * with HTTP 400. `server/discover` is the era-agnostic bootstrap and is never treated as modern here. + * with HTTP 400. A `server/discover` that declares no version stays legacy, so a probing client can still discover the server. */ - private def isModernRequest(method: String, params: Option[Json], headers: Seq[Header]): Boolean = + private def isModernRequest(params: Option[Json], headers: Seq[Header]): Boolean = val metaVersion = params.flatMap(_.hcursor.downField("_meta").downField(ProtocolMeta.ProtocolVersionKey).as[String].toOption) val headerVersion = headers.collectFirst { case h if h.name.equalsIgnoreCase(ProtocolMeta.ProtocolVersionHeader) => h.value.trim } val metaIsModern = metaVersion.exists(version => ProtocolVersion.from(version).forall(_.isModern)) - method != "server/discover" && (metaIsModern || headerVersion.flatMap(ProtocolVersion.from).exists(_.isModern)) + metaIsModern || headerVersion.flatMap(ProtocolVersion.from).exists(_.isModern) + + /** The facts a handler can read about the request; see [[RequestInfo]]. */ + private def requestInfo(params: Option[Json], headers: Seq[Header], isModern: Boolean): RequestInfo = + val meta = params.flatMap(_.hcursor.downField("_meta").focus).map(_.hcursor) + def fromMeta[A: Decoder](key: String): Option[A] = meta.flatMap(_.get[A](key).toOption) + val headerVersion = headers.collectFirst { case h if h.name.equalsIgnoreCase(ProtocolMeta.ProtocolVersionHeader) => h.value.trim } + val progressToken = fromMeta[ProgressToken]("progressToken") + val protocolVersion = fromMeta[String](ProtocolMeta.ProtocolVersionKey).orElse(headerVersion).flatMap(ProtocolVersion.from) + if isModern then + RequestInfo( + protocolVersion = protocolVersion, + clientCapabilities = fromMeta[ClientCapabilities](ProtocolMeta.ClientCapabilities), + clientInfo = fromMeta[Implementation](ProtocolMeta.ClientInfo), + logLevel = fromMeta[LoggingLevel](ProtocolMeta.LogLevel), + progressToken = progressToken, + inputResponses = params.flatMap(_.hcursor.get[Map[String, InputResponse]]("inputResponses").toOption).getOrElse(Map.empty), + requestState = params.flatMap(_.hcursor.get[String]("requestState").toOption) + ) + else RequestInfo(protocolVersion = protocolVersion, progressToken = progressToken) /** The rejection to send for an invalid request, or `None` if it may be dispatched. Runs no tool logic, so a transport can call it before * deciding how to respond (a modern validation error is a plain-JSON HTTP error, not an SSE stream). @@ -187,7 +244,7 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD val meta = params.flatMap(_.hcursor.downField("_meta").focus) val metaVersion = meta.flatMap(_.hcursor.get[String](ProtocolMeta.ProtocolVersionKey).toOption) val headerVersion = headers.collectFirst { case h if h.name.equalsIgnoreCase(ProtocolMeta.ProtocolVersionHeader) => h.value.trim } - if isModernRequest(method, params, headers) then modernRejection(method, params, meta, metaVersion, headerVersion, headers, id) + if isModernRequest(params, headers) then modernRejection(method, params, meta, metaVersion, headerVersion, headers, id) else metaVersion match case Some(version) if ProtocolVersion.from(version).isEmpty => @@ -215,12 +272,15 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD id: RequestId ): Option[McpResponse] = val declared = metaVersion.orElse(headerVersion) - if declared.exists(version => ProtocolVersion.from(version).isEmpty) then + val overHttp = headers.exists(_.name.equalsIgnoreCase("Host")) + if overHttp && headerVersion.isEmpty then + Some(jsonError(id, StatusCode.BadRequest, JSONRPCErrorCodes.HeaderMismatch.code, "Missing MCP-Protocol-Version header")) + else if headerVersion.isDefined && metaVersion.isDefined && headerVersion != metaVersion then + Some(jsonError(id, StatusCode.BadRequest, JSONRPCErrorCodes.HeaderMismatch.code, "MCP-Protocol-Version header does not match _meta")) + else if declared.exists(version => ProtocolVersion.from(version).isEmpty) then val envelope: JSONRPCMessage = JSONRPCMessage.Error(id = id, error = ProtocolMeta.unsupportedVersionError(declared.get, ProtocolVersion.supported.map(_.name))) Some(McpResponse.JsonError(StatusCode.BadRequest, envelope.asJson)) - else if headerVersion.isDefined && metaVersion.isDefined && headerVersion != metaVersion then - Some(jsonError(id, StatusCode.BadRequest, JSONRPCErrorCodes.HeaderMismatch.code, "MCP-Protocol-Version header does not match _meta")) else if metaVersion.isEmpty then Some( jsonError(id, StatusCode.BadRequest, JSONRPCErrorCodes.InvalidParams.code, s"Missing ${ProtocolMeta.ProtocolVersionKey} in _meta") @@ -231,44 +291,119 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD ) else if !modernMethods.contains(method) then Some(jsonError(id, StatusCode.NotFound, JSONRPCErrorCodes.MethodNotFound.code, s"Method not found: $method")) - else if headers.exists(_.name.equalsIgnoreCase("Host")) then + else // SEP-2243 standard request headers only apply over HTTP (identified by the Host header); stdio carries no such headers - headerValidationError(method, params, headers, id) - else None + val headerError = Option.when(overHttp)(headerValidationError(method, params, headers, id)) + headerError.flatten.orElse(missingCapabilityError(method, params, meta, id)).orElse(inputResponsesError(method, params, id)) + + // the requests which can end with an input-required result and so carry `inputResponses` and `requestState` on a retry + private val inputRequiredMethods: Set[String] = Set("tools/call", "prompts/get", "resources/read") + + /** A `-32602` with HTTP 400 when a retry's `inputResponses` is not an object of input responses, or its `requestState` not a string. */ + private def inputResponsesError(method: String, params: Option[Json], id: RequestId): Option[McpResponse] = + def invalid(message: String) = Some(jsonError(id, StatusCode.BadRequest, JSONRPCErrorCodes.InvalidParams.code, message)) + val cursor = params.filter(_ => inputRequiredMethods.contains(method)).map(_.hcursor) + val responses = cursor.flatMap(_.downField("inputResponses").focus).map(_.as[Map[String, InputResponse]]) + val state = cursor.flatMap(_.downField("requestState").focus) + responses match + case Some(Left(failure)) => invalid(s"Invalid inputResponses: ${failure.getMessage}") + case _ if state.exists(json => !json.isString) => invalid("Invalid requestState: expected a string") + case _ => None + + /** A `-32021` with HTTP 400 when `tools/call` names a tool whose required client capabilities the request's `_meta` does not declare. */ + private def missingCapabilityError(method: String, params: Option[Json], meta: Option[Json], id: RequestId): Option[McpResponse] = + val declared = meta.flatMap(_.hcursor.downField(ProtocolMeta.ClientCapabilities).focus).getOrElse(Json.obj()) + val required = Option + .when(method == "tools/call")(params.flatMap(_.hcursor.get[String]("name").toOption)) + .flatten + .flatMap(toolsByName.get) + .flatMap(_.requiredClientCapabilities) + required + .filterNot(capabilities => declares(declared, capabilities.asJson.deepDropNullValues)) + .map: capabilities => + val envelope: JSONRPCMessage = JSONRPCMessage.Error(id = id, error = ProtocolMeta.missingClientCapabilityError(capabilities)) + logger.debug(s"Modern request rejected (id=$id): missing client capabilities ${capabilities.asJson.noSpaces}") + McpResponse.JsonError(StatusCode.BadRequest, envelope.asJson) + + /** Whether `declared` has every key of `required`, recursively for nested objects. */ + private def declares(declared: Json, required: Json): Boolean = + required.asObject.forall(_.toList.forall { (key, value) => + declared.asObject.flatMap(_(key)).exists(declaredValue => value.asObject.forall(_ => declares(declaredValue, value))) + }) // SEP-2243: the body field that the `Mcp-Name` header must mirror, per method private val nameFields: Map[String, String] = Map("tools/call" -> "name", "prompts/get" -> "name", "resources/read" -> "uri") - /** SEP-2243 standard request headers: `Mcp-Method` must equal the body method, and `Mcp-Name` must equal the method's name-shaped body - * field ([[nameFields]]); values are case-sensitive, surrounding whitespace is ignored. A missing or mismatched header is a `-32020` - * HeaderMismatch with HTTP 400. + private val headerParams: Map[String, List[McpHeader]] = + toolDefinitions.map(definition => definition.name -> McpHeaders.params(definition.inputSchema)).toMap + + /** SEP-2243 request headers: `Mcp-Method` must equal the body method, `Mcp-Name` the method's name-shaped body field ([[nameFields]]), + * and on `tools/call` each `Mcp-Param-{name}` the argument its tool marks with `x-mcp-header`. Values are case-sensitive, surrounding + * whitespace is ignored, and `Mcp-Name` and `Mcp-Param-*` values are compared after decoding the Base64 sentinel form. A missing, + * malformed or mismatched header is a `-32020` HeaderMismatch with HTTP 400. */ private def headerValidationError(method: String, params: Option[Json], headers: Seq[Header], id: RequestId): Option[McpResponse] = def header(name: String): Option[String] = headers.collectFirst { case h if h.name.equalsIgnoreCase(name) => h.value.trim } - def mismatch(message: String) = Some(jsonError(id, StatusCode.BadRequest, JSONRPCErrorCodes.HeaderMismatch.code, message)) val nameField = nameFields.get(method) val bodyName = nameField.flatMap(field => params.flatMap(_.hcursor.get[String](field).toOption)) - if !header("Mcp-Method").contains(method) then mismatch("Mcp-Method header missing or does not match the request method") - else if bodyName.exists(name => !header("Mcp-Name").contains(name)) then - mismatch(s"Mcp-Name header missing or does not match params.${nameField.getOrElse("name")}") - else None + val nameError = bodyName.flatMap: name => + header("Mcp-Name").map(McpHeaders.decode) match + case None => Some("Mcp-Name header missing") + case Some(Left(problem)) => Some(s"Mcp-Name header $problem") + case Some(Right(value)) if value != name => Some(s"Mcp-Name header does not match params.${nameField.getOrElse("name")}") + case Some(Right(_)) => None + val toolHeaders = Option.when(method == "tools/call")(bodyName.flatMap(headerParams.get)).flatten.getOrElse(Nil) + val arguments = params.flatMap(_.hcursor.downField("arguments").focus) + val message = + if !header("Mcp-Method").contains(method) then Some("Mcp-Method header missing or does not match the request method") + else nameError.orElse(McpHeaders.mismatch(toolHeaders, arguments, header)) + message.map(jsonError(id, StatusCode.BadRequest, JSONRPCErrorCodes.HeaderMismatch.code, _)) private val serverImplementation: Implementation = Implementation(server.name, server.version) private val serverInfoJson: Json = serverImplementation.asJson private val serverInfoMeta: Json = Json.obj(ProtocolMeta.ServerInfo -> serverInfoJson) - private val serverCapabilities: ServerCapabilities = ServerCapabilities( - logging = Option.when(server.loggingLevel.isDefined)(Json.obj()), - completions = Option.when(server.completion.isDefined)(Json.obj()), - prompts = Option.when(hasPrompts)(ServerPromptsCapability(listChanged = Some(false))), - resources = - Option.when(hasResources)(ServerResourcesCapability(subscribe = Some(server.subscriptions.isDefined), listChanged = Some(false))), - tools = Option.when(server.tools.nonEmpty)(ServerToolsCapability(listChanged = Some(false))) - ) + /** A legacy client gets no list changes (there is no stream for them) and subscribes with `resources/subscribe`. A modern client gets + * them on a `subscriptions/listen` stream, which also replaces `resources/subscribe`. + */ + private def serverCapabilities(modern: Boolean): ServerCapabilities = + val listChanged = Some(modern && listenPublisher.isDefined) + ServerCapabilities( + logging = Option.when(server.loggingLevel.isDefined)(Json.obj()), + completions = Option.when(server.completion.isDefined)(Json.obj()), + prompts = Option.when(hasPrompts)(ServerPromptsCapability(listChanged = listChanged)), + resources = Option.when(hasResources)( + ServerResourcesCapability( + subscribe = if modern then listChanged else Some(server.subscriptions.isDefined), + listChanged = listChanged + ) + ), + tools = Option.when(server.tools.nonEmpty)(ServerToolsCapability(listChanged = listChanged)) + ) + + /** The `subscriptions/listen` request which this server serves on a stream, with the subset of its filter that the server honours; `None` + * for any other request, and for a request which [[validateRequest]] rejects. + */ + def listenRequest(request: Json, headers: Seq[Header]): Option[ListenRequest] = + for + publisher <- listenPublisher + case JSONRPCMessage.Request(_, "subscriptions/listen", params, id) <- request.as[JSONRPCMessage].toOption + if isModernRequest(params, headers) && requestRejection("subscriptions/listen", params, headers, id).isEmpty + listen <- params.flatMap(_.as[SubscriptionsListenParams].toOption) + yield ListenRequest(id, honoured(listen.notifications), publisher) + + private def honoured(requested: SubscriptionFilter): SubscriptionFilter = + def flag(value: Option[Boolean], supported: Boolean) = Option.when(supported && value.contains(true))(true) + SubscriptionFilter( + promptsListChanged = flag(requested.promptsListChanged, hasPrompts), + resourceSubscriptions = requested.resourceSubscriptions.filter(_ => hasResources), + resourcesListChanged = flag(requested.resourcesListChanged, hasResources), + toolsListChanged = flag(requested.toolsListChanged, server.tools.nonEmpty) + ) private val discoverResult: DiscoverResult = DiscoverResult( supportedVersions = ProtocolVersion.supported.map(_.name), - capabilities = serverCapabilities, + capabilities = serverCapabilities(modern = true), ttlMs = CacheHints.Default.ttlMs, cacheScope = CacheHints.Default.cacheScope, instructions = server.instructions, @@ -280,7 +415,7 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD val negotiated = requested.map(ProtocolVersion.negotiate).getOrElse(ProtocolVersion.LatestLegacy) val result = InitializeResult( protocolVersion = negotiated.name, - capabilities = serverCapabilities, + capabilities = serverCapabilities(modern = false), serverInfo = serverImplementation, instructions = server.instructions ) @@ -289,33 +424,64 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD private def handleDiscover(id: RequestId): JSONRPCMessage.Response = JSONRPCMessage.Response(id = id, result = discoverResult.asJson) - private def handleToolsCall(params: Option[Json], id: RequestId, headers: Seq[Header], makeContext: Option[ProgressToken] => C)(using - MonadError[F] - ): F[JSONRPCMessage] = + private def handleToolsCall( + params: Option[Json], + id: RequestId, + headers: Seq[Header], + info: RequestInfo, + makeContext: RequestInfo => C, + isModern: Boolean + )(using MonadError[F]): F[Reply] = val name = params.flatMap(_.hcursor.downField("name").as[String].toOption) val arguments = params.flatMap(_.hcursor.downField("arguments").focus).getOrElse(Json.obj()) - val progressToken = params.flatMap(_.hcursor.downField("_meta").downField("progressToken").as[ProgressToken].toOption) name match case Some(name) => toolsByName.get(name) match case Some(tool) => tool.inputDecoder.decodeJson(arguments) match case Right(input) => - val context = makeContext(progressToken) - tool - .logic(input, context, headers) - .map: result => - toolCallResponse(id, result) + withContext(id, "tools/call", name, arguments, info, makeContext): (context, sign) => + tool + .logic(input, context, headers) + .map: + case outcome: InputRequired => inputRequiredReply(id, sign(outcome), context.request, isModern) + case result: ToolResult[?] => Right(toolCallResponse(id, result)) case Left(decodingError) => val snippet = arguments.noSpaces.take(200) - protocolError( - id, - JSONRPCErrorCodes.InvalidParams.code, - s"Invalid arguments: ${decodingError.getMessage}. Input: $snippet" - ).unit - case None => protocolError(id, JSONRPCErrorCodes.MethodNotFound.code, s"Unknown tool: $name").unit + failed( + protocolError( + id, + JSONRPCErrorCodes.InvalidParams.code, + s"Invalid arguments: ${decodingError.getMessage}. Input: $snippet" + ) + ) + case None => failed(protocolError(id, JSONRPCErrorCodes.MethodNotFound.code, s"Unknown tool: $name")) case None => - protocolError(id, JSONRPCErrorCodes.InvalidParams.code, "Missing tool name").unit + failed(protocolError(id, JSONRPCErrorCodes.InvalidParams.code, "Missing tool name")) + + private def failed(error: JSONRPCMessage)(using MonadError[F]): F[Reply] = (Right(error): Reply).unit + + /** Runs `run` with the handler's context, and with how to sign the request state of its outcome. With a [[RequestStateSigner]], the + * request state of a retry is verified first, and the handler sees the original state; a state which fails verification is a `-32602` + * with HTTP 400, and the handler does not run. + */ + private def withContext(id: RequestId, method: String, target: String, arguments: Json, info: RequestInfo, makeContext: RequestInfo => C)( + run: (C, InputRequired => InputRequired) => F[Reply] + )(using MonadError[F]): F[Reply] = + server.requestStateSigner match + case None => run(makeContext(info), identity) + case Some(signer) => + val context = makeContext(info) + val owner = server.requestStateOwner(context) + val request = Json.arr(method.asJson, target.asJson, RequestStateSigner.digest(arguments).asJson, owner.asJson) + val sign: InputRequired => InputRequired = _.mapRequestState(signer.sign(_, request)) + info.requestState.map(signer.verify(_, request)) match + case None => run(context, sign) + case Some(Right(state)) => run(makeContext(info.copy(requestState = Some(state))), sign) + case Some(Left(problem)) => + (Left( + jsonError(id, StatusCode.BadRequest, JSONRPCErrorCodes.InvalidParams.code, s"Invalid requestState: $problem") + ): Reply).unit private def toolCallResponse(id: RequestId, result: ToolResult[?]): JSONRPCMessage = // for backwards compatibility, structured output is serialized into a text block, unless the tool returned content of its own @@ -335,24 +501,34 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD params: Option[Json], id: RequestId, headers: Seq[Header], - makeContext: Option[ProgressToken] => C - )(using MonadError[F]): F[JSONRPCMessage] = - decodeParams[ReadResourceParams](params, id): params => - val context = makeContext(None) - resourcesByUri.get(params.uri) match - case Some(resource) => resource.read(context, headers).map(resourceReadResponse(id, params.uri)) - case None => - server.resourceTemplates.iterator - .map(template => (template, template.matcher.matchUri(params.uri))) - .collectFirst { case (template, Some(vars)) => template.read(vars, params.uri, context, headers) } match - case Some(result) => result.map(resourceReadResponse(id, params.uri)) - case None => - protocolError( - id, - JSONRPCErrorCodes.ResourceNotFound.code, - s"Resource not found: ${params.uri}", - Some(Json.obj("uri" -> Json.fromString(params.uri))) - ).unit + info: RequestInfo, + makeContext: RequestInfo => C, + isModern: Boolean + )(using MonadError[F]): F[Reply] = + decodeReplyParams[ReadResourceParams](params, id): params => + withContext(id, "resources/read", params.uri, Json.obj(), info, makeContext): (context, sign) => + val respond = resourceReadReply(id, params.uri, context.request, isModern, sign) + resourcesByUri.get(params.uri) match + case Some(resource) => resource.read(context, headers).map(respond) + case None => + server.resourceTemplates.iterator + .map(template => (template, template.matcher.matchUri(params.uri))) + .collectFirst { case (template, Some(vars)) => template.read(vars, params.uri, context, headers) } match + case Some(result) => result.map(respond) + case None => + failed( + protocolError( + id, + JSONRPCErrorCodes.ResourceNotFound.code, + s"Resource not found: ${params.uri}", + Some(Json.obj("uri" -> Json.fromString(params.uri))) + ) + ) + + private def decodeReplyParams[P: Decoder](params: Option[Json], id: RequestId)(f: P => F[Reply])(using MonadError[F]): F[Reply] = + params.flatMap(_.as[P].toOption) match + case Some(params) => f(params) + case None => failed(protocolError(id, JSONRPCErrorCodes.InvalidParams.code, "Invalid or missing params")) private def decodeParams[P: Decoder](params: Option[Json], id: RequestId)(f: P => F[JSONRPCMessage])(using MonadError[F] @@ -361,15 +537,20 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD case Some(params) => f(params) case None => protocolError(id, JSONRPCErrorCodes.InvalidParams.code, "Invalid or missing params").unit - private def resourceReadResponse(id: RequestId, uri: String)(result: Either[ResourceError, List[ResourceContents]]): JSONRPCMessage = - result match - case Right(contents) => JSONRPCMessage.Response(id = id, result = ReadResourceResult(contents).asJson) - case Left(error) => - protocolError( - id, - JSONRPCErrorCodes.ResourceNotFound.code, - error.message, - error.uri.orElse(Some(uri)).map(uri => Json.obj("uri" -> Json.fromString(uri))) + private def resourceReadReply(id: RequestId, uri: String, info: RequestInfo, isModern: Boolean, sign: InputRequired => InputRequired)( + outcome: Either[ResourceError, List[ResourceContents]] | InputRequired + ): Reply = + outcome match + case required: InputRequired => inputRequiredReply(id, sign(required), info, isModern) + case Right(contents) => Right(JSONRPCMessage.Response(id = id, result = ReadResourceResult(contents).asJson)) + case Left(error) => + Right( + protocolError( + id, + JSONRPCErrorCodes.ResourceNotFound.code, + error.message, + error.uri.orElse(Some(uri)).map(uri => Json.obj("uri" -> Json.fromString(uri))) + ) ) private def handleSubscribe(params: Option[Json], id: RequestId, subscribe: Boolean)(using MonadError[F]): F[JSONRPCMessage] = @@ -383,15 +564,21 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD params: Option[Json], id: RequestId, headers: Seq[Header], - makeContext: Option[ProgressToken] => C - )(using MonadError[F]): F[JSONRPCMessage] = - decodeParams[GetPromptParams](params, id): params => + info: RequestInfo, + makeContext: RequestInfo => C, + isModern: Boolean + )(using MonadError[F]): F[Reply] = + decodeReplyParams[GetPromptParams](params, id): params => promptsByName.get(params.name) match case Some(prompt) => - prompt - .logic(params.arguments.getOrElse(Map.empty), makeContext(None), headers) - .map(promptResult => JSONRPCMessage.Response(id = id, result = promptResult.asJson)) - case None => protocolError(id, JSONRPCErrorCodes.InvalidParams.code, s"Unknown prompt: ${params.name}").unit + val arguments = params.arguments.getOrElse(Map.empty) + withContext(id, "prompts/get", params.name, arguments.asJson, info, makeContext): (context, sign) => + prompt + .logic(arguments, context, headers) + .map: + case outcome: InputRequired => inputRequiredReply(id, sign(outcome), context.request, isModern) + case result: GetPromptResult => Right(JSONRPCMessage.Response(id = id, result = result.asJson)) + case None => failed(protocolError(id, JSONRPCErrorCodes.InvalidParams.code, s"Unknown prompt: ${params.name}")) private def handleComplete(params: Option[Json], id: RequestId)(using MonadError[F]): F[JSONRPCMessage] = val handler = server.completion.get diff --git a/server/src/main/scala/chimp/server/McpHeaders.scala b/server/src/main/scala/chimp/server/McpHeaders.scala new file mode 100644 index 00000000..a762ff0e --- /dev/null +++ b/server/src/main/scala/chimp/server/McpHeaders.scala @@ -0,0 +1,169 @@ +package chimp.server + +import io.circe.syntax.* +import io.circe.{Json, JsonObject} +import sttp.apispec.circe.* +import sttp.tapir.docs.apispec.schema.TapirSchemaToJsonSchema + +import java.nio.ByteBuffer +import java.nio.charset.{CodingErrorAction, StandardCharsets} +import java.util.{Base64, Locale} +import scala.util.Try + +/** A tool input property mirrored into the `Mcp-Param-{name}` HTTP header. + * + * @param path + * The chain of `properties` keys from the input schema root to the property. + * @param name + * The `x-mcp-header` value: the name part of the header. + */ +case class McpHeader(path: List[String], name: String) + +/** The `x-mcp-header` schema extension and the Base64 sentinel form of header values. */ +private[server] object McpHeaders: + val Key = "x-mcp-header" + val ParamPrefix = "Mcp-Param-" + + private val SentinelPrefix = "=?base64?" + private val SentinelSuffix = "?=" + private val Base64Alphabet = "^[A-Za-z0-9+/]*={0,2}$".r + private val PrimitiveTypes = Set("string", "integer", "boolean") + private val TokenSymbols = "!#$%&'*+-.^_`|~".toSet + private val SchemaMaps = Set("properties", "patternProperties", "$defs", "definitions", "dependentSchemas") + private val NonSchemaValues = Set("default", "examples", "example", "const", "enum", Key) + + def render(schema: ToolSchema, showMetadata: Boolean): Json = schema match + case ToolSchema.Derived(schema) => + val base = TapirSchemaToJsonSchema(schema, markOptionsAsNullable = false) + (if showMetadata then base else base.copy($schema = None)).asJson + case ToolSchema.Raw(json) => json + + /** The input schema with every header in `headers` added as an `x-mcp-header` annotation. */ + def annotated(schema: Json, headers: List[McpHeader]): Either[String, Json] = + headers.foldLeft[Either[String, Json]](Right(schema))((acc, header) => acc.flatMap(annotate(_, header))) + + private def annotate(schema: Json, header: McpHeader): Either[String, Json] = + def at(json: Json, path: List[String]): Option[Json] = path match + case Nil => json.asObject.map(node => Json.fromJsonObject(node.add(Key, header.name.asJson))) + case key :: rest => + for + node <- json.asObject + properties <- node("properties").flatMap(_.asObject) + child <- properties(key) + updated <- at(child, rest) + yield Json.fromJsonObject(node.add("properties", Json.fromJsonObject(properties.add(key, updated)))) + Option + .when(header.path.nonEmpty)(at(schema, header.path)) + .flatten + .toRight(s"no input property at ${header.path.mkString(".")} reachable through properties keys only") + + /** The violations of the `x-mcp-header` constraints in the input schema. */ + def problems(schema: Json): List[String] = + val found = annotations(schema) + val names = found.flatMap((_, node) => node(Key).flatMap(_.asString)) + val duplicates = names + .groupBy(_.toLowerCase(Locale.ROOT)) + .values + .collect { case same if same.size > 1 => s"x-mcp-header '${same.head}' is used more than once (names are case-insensitive)" } + found.flatMap(problem) ++ duplicates + + /** The headers which the input schema declares, from annotations on properties reachable through `properties` keys only. */ + def params(schema: Json): List[McpHeader] = + for + (path, node) <- annotations(schema) + name <- node(Key).flatMap(_.asString) + propertyPath <- propertyPath(path) + yield McpHeader(propertyPath, name) + + private def problem(path: List[String], node: JsonObject): Option[String] = + val location = path.mkString("#/", "/", "") + def invalid(reason: String) = Some(s"x-mcp-header at $location $reason") + node(Key).flatMap(_.asString) match + case None => invalid("must be a string") + case Some(name) if name.isEmpty => invalid("must not be empty") + case Some(name) if name.exists(Character.isISOControl) => invalid("must not contain control characters") + case Some(name) if !name.forall(isTokenChar) => invalid(s"must be an HTTP token, got '$name'") + case Some(_) if propertyPath(path).isEmpty => invalid("must be on a property reachable through properties keys only") + case Some(_) if !node("type").exists(isPrimitiveType) => invalid("must be on a string, integer or boolean property") + case Some(_) => None + + private def isTokenChar(c: Char): Boolean = (c < 128 && c.isLetterOrDigit) || TokenSymbols.contains(c) + + private def isPrimitiveType(json: Json): Boolean = + json.asString.exists(PrimitiveTypes.contains) || + json.asArray + .flatMap(_.flatMap(_.asString).filterNot(_ == "null").toList match + case single :: Nil => Some(PrimitiveTypes.contains(single)) + case _ => None) + .contains(true) + + private def propertyPath(path: List[String]): Option[List[String]] = + val steps = path.grouped(2).toList + Option.when(path.nonEmpty && path.length % 2 == 0 && steps.forall(_.head == "properties"))(steps.map(_(1))) + + /** Every schema node carrying `x-mcp-header`, with its path from the root as JSON keys (array indices as strings). */ + private def annotations(schema: Json): List[(List[String], JsonObject)] = + def walk(json: Json, path: List[String]): List[(List[String], JsonObject)] = + json.asObject.toList.flatMap: node => + val own = node(Key).map(_ => (path, node)).toList + val nested = node.toList.flatMap: + case (key, _) if NonSchemaValues.contains(key) => Nil + case (key, value) if SchemaMaps.contains(key) => + value.asObject.toList.flatMap(_.toList.flatMap((name, sub) => walk(sub, path ++ List(key, name)))) + case (key, value) => + value.asArray match + case Some(items) => items.toList.zipWithIndex.flatMap((item, i) => walk(item, path ++ List(key, i.toString))) + case None => walk(value, path :+ key) + own ++ nested + walk(schema, Nil) + + /** Decodes a header value which may use the Base64 sentinel form `=?base64?...?=`; a value without the full wrapper is literal. */ + def decode(value: String): Either[String, String] = + val wrapped = + value.length >= SentinelPrefix.length + SentinelSuffix.length && value.startsWith(SentinelPrefix) && value.endsWith(SentinelSuffix) + if value.exists(c => (c < ' ' && c != '\t') || c > '~') then Left("contains invalid characters") + else if !wrapped then Right(value) + else + val encoded = value.substring(SentinelPrefix.length, value.length - SentinelSuffix.length) + val bytes = Option + .when(encoded.length % 4 == 0 && Base64Alphabet.matches(encoded))(Try(Base64.getDecoder.decode(encoded)).toOption) + .flatten + bytes.toRight("is not valid Base64").flatMap(utf8) + + private def utf8(bytes: Array[Byte]): Either[String, String] = + val decoder = + StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT) + Try(decoder.decode(ByteBuffer.wrap(bytes)).toString).toOption.toRight("is not valid UTF-8 after Base64 decoding") + + /** Whether a decoded header value equals the argument: numbers compare numerically, booleans as `true`/`false`, strings literally. */ + def matches(header: String, argument: Json): Boolean = + argument.fold( + jsonNull = false, + jsonBoolean = _.toString == header, + jsonNumber = number => Try(BigDecimal(header)).toOption.exists(value => number.toBigDecimal.exists(_.compare(value) == 0)), + jsonString = _ == header, + jsonArray = _ => false, + jsonObject = _ => false + ) + + /** The `-32020` message for the first `Mcp-Param-*` header of `params` which does not mirror its argument. A null or absent argument + * expects no header. + */ + def mismatch(params: List[McpHeader], arguments: Option[Json], header: String => Option[String]): Option[String] = + params.iterator + .map: param => + val name = ParamPrefix + param.name + val argument = param.path.foldLeft(arguments)((json, key) => json.flatMap(_.asObject).flatMap(_(key))).filterNot(_.isNull) + (argument, header(name).map(decode)) match + case (None, None) => None + case (Some(_), None) => Some(s"$name header missing for an argument present in the body") + case (_, Some(Left(problem))) => Some(s"$name header $problem") + case (None, Some(Right(_))) => Some(s"$name header present, but the argument is absent from the body") + case (Some(value), Some(Right(h))) => Option.unless(matches(h, value))(s"$name header does not match the argument") + .collectFirst { case Some(message) => message } + + /** Throws [[IllegalArgumentException]] when the tool's input schema breaks the `x-mcp-header` constraints. */ + def validate(tool: String, schema: ToolSchema, headers: List[McpHeader]): Unit = + val annotated = McpHeaders.annotated(render(schema, showMetadata = false), headers) + val found = annotated.fold(List(_), problems) + require(found.isEmpty, s"Tool $tool has an invalid x-mcp-header: ${found.mkString("; ")}") diff --git a/server/src/main/scala/chimp/server/McpServer.scala b/server/src/main/scala/chimp/server/McpServer.scala index c994f3d6..c09f58d4 100644 --- a/server/src/main/scala/chimp/server/McpServer.scala +++ b/server/src/main/scala/chimp/server/McpServer.scala @@ -31,6 +31,11 @@ sealed trait McpServerDef[F[_], C <: ServerContext[F]]: def completion: Option[CompletionHandler[F]] def loggingLevel: Option[SetLoggingLevelHandler[F]] def subscriptions: Option[ResourceSubscriptions[F]] + def publisher: Option[NotificationPublisher] + def requestStateSigner: Option[RequestStateSigner] + + /** Who a request state belongs to, besides its request: the principal on a secured server. */ + private[server] def requestStateOwner(context: C): Option[String] = None case class McpServer[F[_]]( name: String = "Chimp MCP server", @@ -44,7 +49,9 @@ case class McpServer[F[_]]( resourceTemplates: List[ServerResourceTemplate[F, ServerContext[F]]] = Nil, completion: Option[CompletionHandler[F]] = None, loggingLevel: Option[SetLoggingLevelHandler[F]] = None, - subscriptions: Option[ResourceSubscriptions[F]] = None + subscriptions: Option[ResourceSubscriptions[F]] = None, + publisher: Option[NotificationPublisher] = None, + requestStateSigner: Option[RequestStateSigner] = None ) extends McpServerDef[F, ServerContext[F]]: def name(value: String): McpServer[F] = copy(name = value) @@ -94,6 +101,18 @@ case class McpServer[F[_]]( def withSubscriptions(handler: ResourceSubscriptions[F]): McpServer[F] = copy(subscriptions = Some(handler)) + /** Sends the list changes and resource updates that `publisher` publishes to the clients on a `subscriptions/listen` stream. Needs a + * streaming transport; a server on the sync HTTP transport answers `subscriptions/listen` with `-32601`. + */ + def withPublisher(publisher: NotificationPublisher): McpServer[F] = + copy(publisher = Some(publisher)) + + /** Signs the request state of each [[InputRequired]] outcome, and verifies it on the retry before the handler runs; see + * [[RequestStateSigner]]. + */ + def withRequestStateSigner(signer: RequestStateSigner): McpServer[F] = + copy(requestStateSigner = Some(signer)) + def endpoint(path: List[String]): ServerEndpoint[Any, F] = ServerHttpTransport(path).serve(this) /** Adds the security input, the error output which describes a rejection, and the logic which validates the security input and makes the @@ -123,7 +142,9 @@ case class McpServer[F[_]]( resourceTemplates, completion, loggingLevel, - subscriptions + subscriptions, + publisher, + requestStateSigner ) case class StreamingMcpServer[F[_]]( @@ -138,7 +159,9 @@ case class StreamingMcpServer[F[_]]( resourceTemplates: List[ServerResourceTemplate[F, StreamingServerContext[F]]] = Nil, completion: Option[CompletionHandler[F]] = None, loggingLevel: Option[SetLoggingLevelHandler[F]] = None, - subscriptions: Option[ResourceSubscriptions[F]] = None + subscriptions: Option[ResourceSubscriptions[F]] = None, + publisher: Option[NotificationPublisher] = None, + requestStateSigner: Option[RequestStateSigner] = None ) extends McpServerDef[F, StreamingServerContext[F]]: def name(value: String): StreamingMcpServer[F] = copy(name = value) @@ -194,6 +217,18 @@ case class StreamingMcpServer[F[_]]( def withSubscriptions(handler: ResourceSubscriptions[F]): StreamingMcpServer[F] = copy(subscriptions = Some(handler)) + /** Sends the list changes and resource updates that `publisher` publishes to the clients on a `subscriptions/listen` stream. Needs a + * streaming transport; a server on the sync HTTP transport answers `subscriptions/listen` with `-32601`. + */ + def withPublisher(publisher: NotificationPublisher): StreamingMcpServer[F] = + copy(publisher = Some(publisher)) + + /** Signs the request state of each [[InputRequired]] outcome, and verifies it on the retry before the handler runs; see + * [[RequestStateSigner]]. + */ + def withRequestStateSigner(signer: RequestStateSigner): StreamingMcpServer[F] = + copy(requestStateSigner = Some(signer)) + /** An [[McpServer]] with security logic, which runs before the server handles an MCP message. The result of the security logic, the * principal, is given to the logic of the tools, prompts, and resources which are added to this server. Tools, prompts, and resources of * the initial server, which do not need the principal, are kept. @@ -213,7 +248,8 @@ case class SecuredMcpServer[F[_], S, E, P]( securedTools: List[ServerTool[?, ?, F, SecuredServerContext[F, P]]] = Nil, securedPrompts: List[ServerPrompt[F, SecuredServerContext[F, P]]] = Nil, securedResources: List[ServerResource[F, SecuredServerContext[F, P]]] = Nil, - securedResourceTemplates: List[ServerResourceTemplate[F, SecuredServerContext[F, P]]] = Nil + securedResourceTemplates: List[ServerResourceTemplate[F, SecuredServerContext[F, P]]] = Nil, + requestStatePrincipal: Option[P => String] = None ) extends McpServerDef[F, SecuredServerContext[F, P]]: def name: String = server.name def version: String = server.version @@ -223,6 +259,11 @@ case class SecuredMcpServer[F[_], S, E, P]( def completion: Option[CompletionHandler[F]] = server.completion def loggingLevel: Option[SetLoggingLevelHandler[F]] = server.loggingLevel def subscriptions: Option[ResourceSubscriptions[F]] = server.subscriptions + def publisher: Option[NotificationPublisher] = server.publisher + def requestStateSigner: Option[RequestStateSigner] = server.requestStateSigner + + private[server] override def requestStateOwner(context: SecuredServerContext[F, P]): Option[String] = + requestStatePrincipal.map(_(context.principal)) def tools: List[ServerTool[?, ?, F, SecuredServerContext[F, P]]] = mergeBy(server.tools, securedTools)(_.name) @@ -288,6 +329,18 @@ case class SecuredMcpServer[F[_], S, E, P]( def withSubscriptions(handler: ResourceSubscriptions[F]): SecuredMcpServer[F, S, E, P] = copy(server = server.withSubscriptions(handler)) + /** Sends the list changes and resource updates that `publisher` publishes to the clients on a `subscriptions/listen` stream. Needs a + * streaming transport; a server on the sync HTTP transport answers `subscriptions/listen` with `-32601`. + */ + def withPublisher(publisher: NotificationPublisher): SecuredMcpServer[F, S, E, P] = + copy(server = server.withPublisher(publisher)) + + /** Signs the request state of each [[InputRequired]] outcome, and verifies it on the retry before the handler runs; see + * [[RequestStateSigner]]. A state also belongs to the principal which `principalId` identifies, and fails for any other principal. + */ + def withRequestStateSigner(signer: RequestStateSigner, principalId: P => String): SecuredMcpServer[F, S, E, P] = + copy(server = server.withRequestStateSigner(signer), requestStatePrincipal = Some(principalId)) + def endpoint(path: List[String]): ServerEndpoint[Any, F] = SecuredServerHttpTransport[F, S, E, P](path).serve(this) def streaming: SecuredStreamingMcpServer[F, S, E, P] = SecuredStreamingMcpServer(this) @@ -310,6 +363,11 @@ case class SecuredStreamingMcpServer[F[_], S, E, P]( def completion: Option[CompletionHandler[F]] = server.completion def loggingLevel: Option[SetLoggingLevelHandler[F]] = server.loggingLevel def subscriptions: Option[ResourceSubscriptions[F]] = server.subscriptions + def publisher: Option[NotificationPublisher] = server.publisher + def requestStateSigner: Option[RequestStateSigner] = server.requestStateSigner + + private[server] override def requestStateOwner(context: SecuredStreamingServerContext[F, P]): Option[String] = + server.requestStateOwner(context) def securityInput: EndpointInput[S] = server.securityInput def errorOutput: EndpointOutput[E] = server.errorOutput @@ -375,3 +433,15 @@ case class SecuredStreamingMcpServer[F[_], S, E, P]( def withSubscriptions(handler: ResourceSubscriptions[F]): SecuredStreamingMcpServer[F, S, E, P] = copy(server = server.withSubscriptions(handler)) + + /** Sends the list changes and resource updates that `publisher` publishes to the clients on a `subscriptions/listen` stream. Needs a + * streaming transport; a server on the sync HTTP transport answers `subscriptions/listen` with `-32601`. + */ + def withPublisher(publisher: NotificationPublisher): SecuredStreamingMcpServer[F, S, E, P] = + copy(server = server.withPublisher(publisher)) + + /** Signs the request state of each [[InputRequired]] outcome, and verifies it on the retry before the handler runs; see + * [[RequestStateSigner]]. A state also belongs to the principal which `principalId` identifies, and fails for any other principal. + */ + def withRequestStateSigner(signer: RequestStateSigner, principalId: P => String): SecuredStreamingMcpServer[F, S, E, P] = + copy(server = server.withRequestStateSigner(signer, principalId)) diff --git a/server/src/main/scala/chimp/server/NotificationPublisher.scala b/server/src/main/scala/chimp/server/NotificationPublisher.scala new file mode 100644 index 00000000..9b18d23f --- /dev/null +++ b/server/src/main/scala/chimp/server/NotificationPublisher.scala @@ -0,0 +1,166 @@ +package chimp.server + +import chimp.protocol.* +import io.circe.Json +import io.circe.syntax.* + +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.{AtomicBoolean, AtomicLong} +import scala.jdk.CollectionConverters.* + +/** Publishes list changes and resource updates to the clients which hold a `subscriptions/listen` stream (2026-07-28) open. Give it to a + * server with `withPublisher`. A streaming server then advertises `listChanged` for its tools, prompts and resources, and `subscribe` for + * its resources. + * + * The methods are thread-safe and can be called from any part of the application. Each one returns after it has put the notification on + * every open subscription which asked for it. + */ +final class NotificationPublisher: + private val subscriptions = ConcurrentHashMap[Long, Subscription]() + private val nextKey = AtomicLong() + private val closed = AtomicBoolean(false) + + /** Sends `notifications/tools/list_changed`. */ + def toolsListChanged(): Unit = publish(ServerEvent.ToolsListChanged) + + /** Sends `notifications/prompts/list_changed`. */ + def promptsListChanged(): Unit = publish(ServerEvent.PromptsListChanged) + + /** Sends `notifications/resources/list_changed`. */ + def resourcesListChanged(): Unit = publish(ServerEvent.ResourcesListChanged) + + /** Sends `notifications/resources/updated` to the subscriptions which listed `uri`. */ + def resourceUpdated(uri: String): Unit = publish(ServerEvent.ResourceUpdated(uri)) + + /** Ends each open subscription with a successful `subscriptions/listen` result, for example when the server shuts down. A subscription + * opened after this ends at once. + */ + def close(): Unit = + closed.set(true) + subscriptions.values.asScala.toList.foreach(_.end()) + + private def publish(event: ServerEvent): Unit = subscriptions.values.asScala.foreach(_.deliver(event)) + + private[server] def openSubscriptions: Int = subscriptions.size + + private[server] def open(id: RequestId, filter: SubscriptionFilter, output: SubscriptionOutput): Subscription = + val key = nextKey.getAndIncrement() + val subscription = Subscription(id, filter, output, () => { val _ = subscriptions.remove(key) }) + val _ = subscriptions.put(key, subscription) + subscription.acknowledge() + if closed.get then subscription.end() + subscription + +/** Where the messages of one subscription go. Each transport supplies its own. */ +private[server] trait SubscriptionOutput: + def send(message: Json): Unit + + /** Called after the server sent the final `subscriptions/listen` result. */ + def close(): Unit + +private[server] enum ServerEvent: + case ToolsListChanged, PromptsListChanged, ResourcesListChanged + case ResourceUpdated(uri: String) + + def accepted(filter: SubscriptionFilter): Boolean = this match + case ToolsListChanged => filter.toolsListChanged.contains(true) + case PromptsListChanged => filter.promptsListChanged.contains(true) + case ResourcesListChanged => filter.resourcesListChanged.contains(true) + case ResourceUpdated(uri) => filter.resourceSubscriptions.exists(_.contains(uri)) + + def notification(meta: Json): JSONRPCMessage.Notification = this match + case ToolsListChanged => JSONRPCMessage.Notification(method = "notifications/tools/list_changed", params = Some(meta)) + case PromptsListChanged => JSONRPCMessage.Notification(method = "notifications/prompts/list_changed", params = Some(meta)) + case ResourcesListChanged => JSONRPCMessage.Notification(method = "notifications/resources/list_changed", params = Some(meta)) + case ResourceUpdated(uri) => + JSONRPCMessage.Notification(method = "notifications/resources/updated", params = Some(meta.deepMerge(Json.obj("uri" -> uri.asJson)))) + +/** One `subscriptions/listen` stream. Every message on it carries the id of the listen request in `_meta`. */ +private[server] final class Subscription( + val id: RequestId, + filter: SubscriptionFilter, + output: SubscriptionOutput, + unregister: () => Unit +): + private val active = AtomicBoolean(true) + private val acknowledged = AtomicBoolean(false) + private val meta: Map[String, Json] = Map(ProtocolMeta.SubscriptionId -> id.asJson) + private val metaParams: Json = Json.obj("_meta" -> meta.asJson) + + def isActive: Boolean = active.get + + def acknowledge(): Unit = synchronized: + if active.get then + send( + JSONRPCMessage.Notification( + method = "notifications/subscriptions/acknowledged", + params = Some(SubscriptionsAcknowledgedParams(filter, Some(meta)).asJson) + ) + ) + acknowledged.set(true) + + /** Holds the lock of [[acknowledge]], so an event published while the acknowledgement is on its way waits for it, instead of being + * dropped. + */ + def deliver(event: ServerEvent): Unit = synchronized: + if acknowledged.get && active.get && event.accepted(filter) then send(event.notification(metaParams)) + + /** Ends the subscription because the client ended it; sends nothing. */ + def cancel(): Unit = + if active.compareAndSet(true, false) then unregister() + + /** Ends the subscription on the server's initiative, with a successful `subscriptions/listen` result. */ + def end(): Unit = synchronized: + if active.compareAndSet(true, false) then + unregister() + send(JSONRPCMessage.Response(id = id, result = SubscriptionsListenResult(_meta = Some(meta)).asJson)) + output.close() + + private def send(message: JSONRPCMessage): Unit = output.send(message.asJson.deepDropNullValues) + +/** A `subscriptions/listen` request which the server accepted, with the subset of the filter it honours. */ +private[server] final case class ListenRequest(id: RequestId, filter: SubscriptionFilter, publisher: NotificationPublisher): + def open(output: SubscriptionOutput): Subscription = publisher.open(id, filter, output) + +/** The subscriptions of one stdio connection. They share its single output and are told apart by the subscription id. */ +private[server] final class StdioSubscriptions(handler: McpHandler[?, ?], write: Json => Unit): + private val open = ConcurrentHashMap[RequestId, Subscription]() + + /** Opens a subscription if `message` is a `subscriptions/listen` request which the server accepts, and then gives `true`. Otherwise ends + * the subscription which a `notifications/cancelled` names, and gives `false`: the server handles the message as usual. + */ + def accept(message: Json): Boolean = + handler.listenRequest(message, Nil) match + case Some(request) => + listen(request) + true + case None => + cancel(message) + false + + def listen(request: ListenRequest): Unit = + val output = new SubscriptionOutput: + def send(message: Json): Unit = write(message) + def close(): Unit = + val _ = open.values.removeIf(!_.isActive) + write( + (JSONRPCMessage.Notification( + method = "notifications/cancelled", + params = Some(CancelledParams(request.id).asJson) + ): JSONRPCMessage).asJson.deepDropNullValues + ) + val subscription = request.open(output) + if subscription.isActive then + val _ = open.put(request.id, subscription) + + /** Ends the subscription which a `notifications/cancelled` from the client names; ignores any other message. */ + def cancel(message: Json): Unit = + message.as[JSONRPCMessage] match + case Right(JSONRPCMessage.Notification(_, "notifications/cancelled", Some(params))) => + params.as[CancelledParams].toOption.flatMap(cancelled => Option(open.remove(cancelled.requestId))).foreach(_.cancel()) + case _ => () + + /** Ends each open subscription of the connection, for example when its input ends. */ + def endAll(): Unit = + open.values.asScala.toList.foreach(_.end()) + open.clear() diff --git a/server/src/main/scala/chimp/server/Prompt.scala b/server/src/main/scala/chimp/server/Prompt.scala index fa3264ca..7eda74f5 100644 --- a/server/src/main/scala/chimp/server/Prompt.scala +++ b/server/src/main/scala/chimp/server/Prompt.scala @@ -7,7 +7,9 @@ import sttp.shared.Identity /** Starts defining a prompt with the given name. */ def prompt(name: String): PartialPrompt = PartialPrompt(name) -/** A prompt being defined, before its logic is attached. */ +/** A prompt being defined, before its logic is attached. The logic ends with a [[GetPromptResult]], or with [[InputRequired]] to ask the + * client for input first. + */ case class PartialPrompt( name: String, title: Option[String] = None, @@ -34,19 +36,25 @@ case class PartialPrompt( /** Attaches effectful logic, with access to the request headers, producing the prompt's messages. */ def serverLogic[F[_]]( - logic: (Map[String, String], Seq[Header]) => F[GetPromptResult] + logic: (Map[String, String], Seq[Header]) => F[GetPromptResult | InputRequired] ): ServerPrompt[F, ServerContext[F]] = ServerPrompt(definition, (args, _, headers) => logic(args, headers)) + /** Attaches effectful logic, with access to the [[ServerContext]], which gives the facts of the request. */ + def serverLogicWithContext[F[_]]( + logic: (Map[String, String], ServerContext[F], Seq[Header]) => F[GetPromptResult | InputRequired] + ): ServerPrompt[F, ServerContext[F]] = + ServerPrompt(definition, logic) + /** Attaches effectful logic, with access to the principal; usable only on a [[SecuredMcpServer]]. */ def securedServerLogic[F[_], P]( - logic: (Map[String, String], P, Seq[Header]) => F[GetPromptResult] + logic: (Map[String, String], P, Seq[Header]) => F[GetPromptResult | InputRequired] ): ServerPrompt[F, SecuredServerContext[F, P]] = ServerPrompt(definition, (args, context, headers) => logic(args, context.principal, headers)) /** Attaches synchronous logic that also receives the request headers. */ def handleWithHeaders( - logic: (Map[String, String], Seq[Header]) => GetPromptResult + logic: (Map[String, String], Seq[Header]) => GetPromptResult | InputRequired ): ServerPrompt[Identity, ServerContext[Identity]] = serverLogic[Identity](logic) @@ -56,7 +64,7 @@ case class PartialPrompt( /** Attaches synchronous logic over the supplied argument values and the principal; usable only on a [[SecuredMcpServer]]. */ def handleSecured[P]( - logic: (Map[String, String], P) => GetPromptResult + logic: (Map[String, String], P) => GetPromptResult | InputRequired ): ServerPrompt[Identity, SecuredServerContext[Identity, P]] = securedServerLogic[Identity, P]((args, principal, _) => logic(args, principal)) @@ -68,5 +76,5 @@ end PartialPrompt /** A fully-defined prompt: its metadata plus the logic producing its messages. */ case class ServerPrompt[F[_], -C <: ServerContext[F]]( definition: Prompt, - logic: (Map[String, String], C, Seq[Header]) => F[GetPromptResult] + logic: (Map[String, String], C, Seq[Header]) => F[GetPromptResult | InputRequired] ) diff --git a/server/src/main/scala/chimp/server/RequestStateSigner.scala b/server/src/main/scala/chimp/server/RequestStateSigner.scala new file mode 100644 index 00000000..597a7a0f --- /dev/null +++ b/server/src/main/scala/chimp/server/RequestStateSigner.scala @@ -0,0 +1,69 @@ +package chimp.server + +import io.circe.syntax.* +import io.circe.{parser, Json} + +import java.nio.charset.StandardCharsets.UTF_8 +import java.security.MessageDigest +import java.time.Clock +import java.util.Base64 +import javax.crypto.Mac +import javax.crypto.spec.SecretKeySpec +import scala.concurrent.duration.* +import scala.util.Try + +/** Protects the integrity of the request state of [[InputRequired]] outcomes. A server with a signer signs each request state with + * HMAC-SHA256, together with an expiry and the identity of the request (its method, its tool or prompt name or resource URI, a digest of + * its arguments, and on a secured server the principal). On a retry, the server verifies the state before the handler runs, and gives the + * handler the original state. A state which was changed, has expired, or belongs to another request or principal fails with `-32602`. + * + * The client can read the state, so do not put secrets in it. Within its lifetime, a state can be used again on the same request. + */ +final class RequestStateSigner private (secret: Array[Byte], ttl: FiniteDuration, clock: Clock): + private val encoder = Base64.getUrlEncoder.withoutPadding() + private val decoder = Base64.getUrlDecoder + + private def mac(data: String): Array[Byte] = + val hmac = Mac.getInstance("HmacSHA256") + hmac.init(SecretKeySpec(secret, "HmacSHA256")) + hmac.doFinal(data.getBytes(UTF_8)) + + private[server] def sign(state: String, request: Json): String = + val expiresAt = clock.millis() + ttl.toMillis + val body = + encoder.encodeToString(Json.obj("state" -> state.asJson, "exp" -> expiresAt.asJson, "req" -> request).noSpaces.getBytes(UTF_8)) + s"$body.${encoder.encodeToString(mac(body))}" + + /** The original state, or why `signed` fails verification for `request`. */ + private[server] def verify(signed: String, request: Json): Either[String, String] = + signed.split('.') match + case Array(body, signature) if Try(decoder.decode(signature)).toOption.exists(MessageDigest.isEqual(_, mac(body))) => + val payload = Try(String(decoder.decode(body), UTF_8)).toOption.flatMap(parser.parse(_).toOption).map(_.hcursor) + val fields = + for + c <- payload + state <- c.get[String]("state").toOption + expiresAt <- c.get[Long]("exp").toOption + issuedFor <- c.get[Json]("req").toOption + yield (state, expiresAt, issuedFor) + fields match + case Some((_, expiresAt, _)) if expiresAt <= clock.millis() => Left("the request state has expired") + case Some((_, _, issuedFor)) if issuedFor != request => Left("the request state belongs to another request") + case Some((state, _, _)) => Right(state) + case None => Left("the request state is malformed") + case _ => Left("the request state failed the integrity check") + +object RequestStateSigner: + /** @param secret + * The HMAC key, at least 32 bytes. Keep it secret, and use the same one on all instances of the server. + * @param ttl + * How long a request state stays valid. + */ + def apply(secret: Array[Byte], ttl: FiniteDuration = 10.minutes, clock: Clock = Clock.systemUTC()): RequestStateSigner = + require(secret.length >= 32, s"The request state secret must have at least 32 bytes, got ${secret.length}") + new RequestStateSigner(secret.clone(), ttl, clock) + + /** A digest of a request's salient parameters, so a state can be bound to them without carrying them. */ + private[server] def digest(json: Json): String = + val bytes = json.printWith(io.circe.Printer.noSpacesSortKeys).getBytes(UTF_8) + Base64.getUrlEncoder.withoutPadding().encodeToString(MessageDigest.getInstance("SHA-256").digest(bytes)) diff --git a/server/src/main/scala/chimp/server/Resource.scala b/server/src/main/scala/chimp/server/Resource.scala index ad645a61..e44725c8 100644 --- a/server/src/main/scala/chimp/server/Resource.scala +++ b/server/src/main/scala/chimp/server/Resource.scala @@ -16,7 +16,9 @@ def resource(uri: String): PartialResource = PartialResource(uri) /** Starts defining a resource template whose URI carries `{variable}` placeholders. */ def resourceTemplate(uriTemplate: String): PartialResourceTemplate = PartialResourceTemplate(uriTemplate) -/** A resource being defined, before its read logic is attached. */ +/** A resource being defined, before its read logic is attached. The logic ends with the contents (or an error), or with [[InputRequired]] + * to ask the client for input first. + */ case class PartialResource( uri: String, name: Option[String] = None, @@ -50,29 +52,37 @@ case class PartialResource( /** Attaches effectful logic, with access to the request headers, producing the resource's contents (or an error). */ def serverLogic[F[_]]( - logic: Seq[Header] => F[Either[ResourceError, List[ResourceContents]]] + logic: Seq[Header] => F[Either[ResourceError, List[ResourceContents]] | InputRequired] ): ServerResource[F, ServerContext[F]] = ServerResource(definition, (_, headers) => logic(headers)) + /** Attaches effectful logic, with access to the [[ServerContext]], which gives the facts of the request. */ + def serverLogicWithContext[F[_]]( + logic: (ServerContext[F], Seq[Header]) => F[Either[ResourceError, List[ResourceContents]] | InputRequired] + ): ServerResource[F, ServerContext[F]] = + ServerResource(definition, logic) + /** Attaches effectful logic, with access to the principal; usable only on a [[SecuredMcpServer]]. */ def securedServerLogic[F[_], P]( - logic: (P, Seq[Header]) => F[Either[ResourceError, List[ResourceContents]]] + logic: (P, Seq[Header]) => F[Either[ResourceError, List[ResourceContents]] | InputRequired] ): ServerResource[F, SecuredServerContext[F, P]] = ServerResource(definition, (context, headers) => logic(context.principal, headers)) /** Attaches synchronous logic that also receives the request headers. */ def handleWithHeaders( - logic: Seq[Header] => Either[ResourceError, List[ResourceContents]] + logic: Seq[Header] => Either[ResourceError, List[ResourceContents]] | InputRequired ): ServerResource[Identity, ServerContext[Identity]] = serverLogic[Identity](logic) /** Attaches synchronous logic producing the resource's contents (or an error). */ - def handle(logic: () => Either[ResourceError, List[ResourceContents]]): ServerResource[Identity, ServerContext[Identity]] = + def handle( + logic: () => Either[ResourceError, List[ResourceContents]] | InputRequired + ): ServerResource[Identity, ServerContext[Identity]] = handleWithHeaders(_ => logic()) /** Attaches synchronous logic over the principal; usable only on a [[SecuredMcpServer]]. */ def handleSecured[P]( - logic: P => Either[ResourceError, List[ResourceContents]] + logic: P => Either[ResourceError, List[ResourceContents]] | InputRequired ): ServerResource[Identity, SecuredServerContext[Identity, P]] = securedServerLogic[Identity, P]((principal, _) => logic(principal)) @@ -83,7 +93,7 @@ end PartialResource /** A fully-defined resource: its metadata plus the logic reading its contents. */ case class ServerResource[F[_], -C <: ServerContext[F]]( definition: Resource, - read: (C, Seq[Header]) => F[Either[ResourceError, List[ResourceContents]]] + read: (C, Seq[Header]) => F[Either[ResourceError, List[ResourceContents]] | InputRequired] ) /** A resource template being defined, before its read logic is attached. */ @@ -116,13 +126,21 @@ case class PartialResourceTemplate( /** Attaches effectful logic reading a matched URI; receives the extracted variables, the full URI, and the request headers. */ def serverLogic[F[_]]( - logic: (Map[String, String], String, Seq[Header]) => F[Either[ResourceError, List[ResourceContents]]] + logic: (Map[String, String], String, Seq[Header]) => F[Either[ResourceError, List[ResourceContents]] | InputRequired] ): ServerResourceTemplate[F, ServerContext[F]] = ServerResourceTemplate(definition, UriTemplate.compile(uriTemplate), (vars, uri, _, headers) => logic(vars, uri, headers)) + /** Attaches effectful logic reading a matched URI, with access to the [[ServerContext]], which gives the facts of the request. */ + def serverLogicWithContext[F[_]]( + logic: (Map[String, String], String, ServerContext[F], Seq[Header]) => F[ + Either[ResourceError, List[ResourceContents]] | InputRequired + ] + ): ServerResourceTemplate[F, ServerContext[F]] = + ServerResourceTemplate(definition, UriTemplate.compile(uriTemplate), logic) + /** Attaches effectful logic, with access to the principal; usable only on a [[SecuredMcpServer]]. */ def securedServerLogic[F[_], P]( - logic: (Map[String, String], String, P, Seq[Header]) => F[Either[ResourceError, List[ResourceContents]]] + logic: (Map[String, String], String, P, Seq[Header]) => F[Either[ResourceError, List[ResourceContents]] | InputRequired] ): ServerResourceTemplate[F, SecuredServerContext[F, P]] = ServerResourceTemplate( definition, @@ -132,19 +150,19 @@ case class PartialResourceTemplate( /** Attaches synchronous logic that also receives the request headers. */ def handleWithHeaders( - logic: (Map[String, String], String, Seq[Header]) => Either[ResourceError, List[ResourceContents]] + logic: (Map[String, String], String, Seq[Header]) => Either[ResourceError, List[ResourceContents]] | InputRequired ): ServerResourceTemplate[Identity, ServerContext[Identity]] = serverLogic[Identity](logic) /** Attaches synchronous logic receiving the extracted variables and the full URI. */ def handle( - logic: (Map[String, String], String) => Either[ResourceError, List[ResourceContents]] + logic: (Map[String, String], String) => Either[ResourceError, List[ResourceContents]] | InputRequired ): ServerResourceTemplate[Identity, ServerContext[Identity]] = handleWithHeaders((vars, uri, _) => logic(vars, uri)) /** Attaches synchronous logic over the extracted variables, the full URI, and the principal; usable only on a [[SecuredMcpServer]]. */ def handleSecured[P]( - logic: (Map[String, String], String, P) => Either[ResourceError, List[ResourceContents]] + logic: (Map[String, String], String, P) => Either[ResourceError, List[ResourceContents]] | InputRequired ): ServerResourceTemplate[Identity, SecuredServerContext[Identity, P]] = securedServerLogic[Identity, P]((vars, uri, principal, _) => logic(vars, uri, principal)) @@ -157,7 +175,7 @@ end PartialResourceTemplate case class ServerResourceTemplate[F[_], -C <: ServerContext[F]]( definition: ResourceTemplate, matcher: UriTemplate, - read: (Map[String, String], String, C, Seq[Header]) => F[Either[ResourceError, List[ResourceContents]]] + read: (Map[String, String], String, C, Seq[Header]) => F[Either[ResourceError, List[ResourceContents]] | InputRequired] ) /** A compiled URI template that matches concrete URIs and extracts their `{variable}` values. */ diff --git a/server/src/main/scala/chimp/server/ServerContext.scala b/server/src/main/scala/chimp/server/ServerContext.scala index ebcfa603..06bcd3e8 100644 --- a/server/src/main/scala/chimp/server/ServerContext.scala +++ b/server/src/main/scala/chimp/server/ServerContext.scala @@ -5,28 +5,65 @@ import io.circe.Json import io.circe.syntax.* import sttp.monad.MonadError -trait ServerContext[F[_]] +/** What a request tells about itself. A modern (2026-07-28+) request carries all of it in `_meta`, and a retry of an [[InputRequired]] + * outcome also carries the client's answers (`inputResponses`) and the echoed `requestState`. A legacy request gives only the protocol + * version (from the `MCP-Protocol-Version` header or `_meta`) and the progress token; the server keeps no state from `initialize`. + */ +final case class RequestInfo( + protocolVersion: Option[ProtocolVersion] = None, + clientCapabilities: Option[ClientCapabilities] = None, + clientInfo: Option[Implementation] = None, + logLevel: Option[LoggingLevel] = None, + progressToken: Option[ProgressToken] = None, + inputResponses: Map[String, InputResponse] = Map.empty, + requestState: Option[String] = None +): + /** The client's answer to the form or URL elicitation asked under `key`, if the retry carries one. */ + def elicitResult(key: String): Option[ElicitResult] = inputResponses.get(key).collect { case InputResponse.Elicit(result) => result } + + /** The client's answer to the sampling request asked under `key`, if the retry carries one. */ + def samplingResult(key: String): Option[CreateMessageResult] = + inputResponses.get(key).collect { case InputResponse.Sampling(result) => result } + + /** The client's answer to the roots request asked under `key`, if the retry carries one. */ + def rootsResult(key: String): Option[ListRootsResult] = inputResponses.get(key).collect { case InputResponse.Roots(result) => result } + + /** A modern request gets `notifications/message` only at or above its `logLevel`, and none without one. A legacy request gets all. */ + private[server] def allowsLog(level: LoggingLevel): Boolean = + if protocolVersion.exists(_.isModern) then logLevel.exists(minimum => level.ordinal >= minimum.ordinal) else true + +object RequestInfo: + val empty: RequestInfo = RequestInfo() + +trait ServerContext[F[_]]: + def request: RequestInfo object ServerContext: - def noop[F[_]]: ServerContext[F] = new ServerContext[F] {} + def noop[F[_]]: ServerContext[F] = apply(RequestInfo.empty) + + private[server] def apply[F[_]](info: RequestInfo): ServerContext[F] = new ServerContext[F]: + def request: RequestInfo = info /** A context which also gives the principal that the security logic of a [[SecuredMcpServer]] made from the request. */ trait SecuredServerContext[F[_], +P] extends ServerContext[F]: def principal: P object SecuredServerContext: - def apply[F[_], P](value: P): SecuredServerContext[F, P] = new SecuredServerContext[F, P]: + def apply[F[_], P](value: P, info: RequestInfo = RequestInfo.empty): SecuredServerContext[F, P] = new SecuredServerContext[F, P]: def principal: P = value + def request: RequestInfo = info trait StreamingServerContext[F[_]] extends ServerContext[F]: def reportProgress(progress: Double, total: Option[Double] = None, message: Option[String] = None): F[Unit] + + /** Sends `notifications/message`. For a modern request it sends only at or above the request's `logLevel`, and nothing without one. */ def log(level: LoggingLevel, data: Json, logger: Option[String] = None): F[Unit] -private[server] final class SinkStreamingServerContext[F[_]](sink: OutboundSink[F], progressToken: Option[ProgressToken])(using +private[server] final class SinkStreamingServerContext[F[_]](sink: OutboundSink[F], val request: RequestInfo)(using m: MonadError[F] ) extends StreamingServerContext[F]: def reportProgress(progress: Double, total: Option[Double] = None, message: Option[String] = None): F[Unit] = - progressToken match + request.progressToken match case Some(token) => sink.send( JSONRPCMessage.Notification( @@ -37,9 +74,11 @@ private[server] final class SinkStreamingServerContext[F[_]](sink: OutboundSink[ case None => m.unit(()) def log(level: LoggingLevel, data: Json, logger: Option[String] = None): F[Unit] = - sink.send( - JSONRPCMessage.Notification(method = "notifications/message", params = Some(LoggingMessageParams(level, data, logger).asJson)) - ) + if request.allowsLog(level) then + sink.send( + JSONRPCMessage.Notification(method = "notifications/message", params = Some(LoggingMessageParams(level, data, logger).asJson)) + ) + else m.unit(()) /** A context which gives both the principal that the security logic of a [[SecuredMcpServer]] made from the request, and the streaming * capabilities of a [[StreamingServerContext]]. @@ -48,11 +87,11 @@ trait SecuredStreamingServerContext[F[_], +P] extends SecuredServerContext[F, P] private[server] final class SinkSecuredStreamingServerContext[F[_], P]( sink: OutboundSink[F], - progressToken: Option[ProgressToken], + val request: RequestInfo, val principal: P )(using MonadError[F]) extends SecuredStreamingServerContext[F, P]: - private val delegate = SinkStreamingServerContext[F](sink, progressToken) + private val delegate = SinkStreamingServerContext[F](sink, request) def reportProgress(progress: Double, total: Option[Double] = None, message: Option[String] = None): F[Unit] = delegate.reportProgress(progress, total, message) def log(level: LoggingLevel, data: Json, logger: Option[String] = None): F[Unit] = delegate.log(level, data, logger) diff --git a/server/src/main/scala/chimp/server/Tool.scala b/server/src/main/scala/chimp/server/Tool.scala index e6aecd3b..f211c514 100644 --- a/server/src/main/scala/chimp/server/Tool.scala +++ b/server/src/main/scala/chimp/server/Tool.scala @@ -1,6 +1,6 @@ package chimp.server -import chimp.protocol.{ResourceContents, ToolContent} +import chimp.protocol.{ClientCapabilities, ResourceContents, ToolContent} import io.circe.syntax.* import io.circe.{Decoder, Encoder, Json} import sttp.model.Header @@ -66,7 +66,8 @@ def tool(name: String): PartialTool = case class PartialTool( name: String, description: Option[String] = None, - annotations: Option[ToolAnnotations] = None + annotations: Option[ToolAnnotations] = None, + requiredClientCapabilities: Option[ClientCapabilities] = None ): def description(desc: String): PartialTool = copy(description = Some(desc)) @@ -74,80 +75,119 @@ case class PartialTool( def withAnnotations(ann: ToolAnnotations): PartialTool = copy(annotations = Some(ann)) + /** Declares the client capabilities a call needs. A modern (2026-07-28+) request whose `_meta` does not declare them fails with `-32021` + * (MissingRequiredClientCapability) before the logic runs. + */ + def withRequiredClientCapabilities(capabilities: ClientCapabilities): PartialTool = + copy(requiredClientCapabilities = Some(capabilities)) + /** Fixes the input type, deriving its JSON Schema and decoder from the given instances. */ def input[I: Schema: Decoder]: Tool[I, NoStructuredOutput] = - Tool[I, NoStructuredOutput](name, description, ToolSchema.Derived(summon[Schema[I]]), summon[Decoder[I]], None, annotations) + McpHeaders.validate(name, ToolSchema.Derived(summon[Schema[I]]), Nil) + Tool[I, NoStructuredOutput]( + name, + description, + ToolSchema.Derived(summon[Schema[I]]), + summon[Decoder[I]], + None, + annotations, + requiredClientCapabilities + ) /** Fixes the input as raw JSON, validated against the given JSON Schema. */ def inputJson(schema: Json): Tool[Json, NoStructuredOutput] = - Tool[Json, NoStructuredOutput](name, description, ToolSchema.Raw(schema), summon[Decoder[Json]], None, annotations) + McpHeaders.validate(name, ToolSchema.Raw(schema), Nil) + Tool[Json, NoStructuredOutput]( + name, + description, + ToolSchema.Raw(schema), + summon[Decoder[Json]], + None, + annotations, + requiredClientCapabilities + ) -/** A tool with a known input type `I` and structured output type `O`, ready to be given its handling logic. */ +/** A tool with a known input type `I` and structured output type `O`, ready to be given its handling logic. The logic ends with a + * [[ToolResult]], or with [[InputRequired]] to ask the client for input first. + */ case class Tool[I, O]( name: String, description: Option[String], inputSchema: ToolSchema, inputDecoder: Decoder[I], outputSchema: Option[ToolSchema], - annotations: Option[ToolAnnotations] + annotations: Option[ToolAnnotations], + requiredClientCapabilities: Option[ClientCapabilities] = None, + mcpHeaders: List[McpHeader] = Nil ): + /** Marks the top-level input property `property` with `x-mcp-header`, so that a client mirrors its value into the `Mcp-Param-{header}` + * HTTP header and the server checks that the header matches the argument. Throws [[IllegalArgumentException]] when the annotation breaks + * the spec constraints. + */ + def withMcpHeader(property: String, header: String): Tool[I, O] = withMcpHeader(List(property), header) + + /** Marks the input property at `path`, a chain of `properties` keys from the schema root, with `x-mcp-header`, as the single-property + * overload does. + */ + def withMcpHeader(path: List[String], header: String): Tool[I, O] = + val headers = mcpHeaders :+ McpHeader(path, header) + McpHeaders.validate(name, inputSchema, headers) + copy(mcpHeaders = headers) + /** Fixes the structured output type, deriving its JSON Schema from the given instance. */ def output[O2: Schema]: Tool[I, O2] = - Tool[I, O2](name, description, inputSchema, inputDecoder, Some(ToolSchema.Derived(summon[Schema[O2]])), annotations) + copy(outputSchema = Some(ToolSchema.Derived(summon[Schema[O2]]))) /** Fixes the structured output as raw JSON, described by the given JSON Schema. */ def outputJson(schema: Json): Tool[I, Json] = - Tool[I, Json](name, description, inputSchema, inputDecoder, Some(ToolSchema.Raw(schema)), annotations) + copy(outputSchema = Some(ToolSchema.Raw(schema))) /** Attaches effectful logic, with access to the request headers. */ - def serverLogic[F[_]](logic: (I, Seq[Header]) => F[ToolResult[O]]): ServerTool[I, O, F, ServerContext[F]] = - ServerTool(name, description, inputSchema, inputDecoder, outputSchema, annotations, (input, _, headers) => logic(input, headers)) + def serverLogic[F[_]](logic: (I, Seq[Header]) => F[ToolResult[O] | InputRequired]): ServerTool[I, O, F, ServerContext[F]] = + serverTool((input, _, headers) => logic(input, headers)) + + /** Attaches effectful logic, with access to the [[ServerContext]], which gives the facts of the request. */ + def serverLogicWithContext[F[_]]( + logic: (I, ServerContext[F], Seq[Header]) => F[ToolResult[O] | InputRequired] + ): ServerTool[I, O, F, ServerContext[F]] = + serverTool(logic) /** Attaches effectful logic, with access to the principal; usable only on a [[SecuredMcpServer]]. */ - def securedServerLogic[F[_], P](logic: (I, P, Seq[Header]) => F[ToolResult[O]]): ServerTool[I, O, F, SecuredServerContext[F, P]] = - ServerTool( - name, - description, - inputSchema, - inputDecoder, - outputSchema, - annotations, - (input, context, headers) => logic(input, context.principal, headers) - ) + def securedServerLogic[F[_], P]( + logic: (I, P, Seq[Header]) => F[ToolResult[O] | InputRequired] + ): ServerTool[I, O, F, SecuredServerContext[F, P]] = + serverTool((input, context, headers) => logic(input, context.principal, headers)) /** Attaches effectful logic with access to the [[StreamingServerContext]]; usable only on a streaming server. */ def streamingServerLogic[F[_]]( - logic: (I, StreamingServerContext[F], Seq[Header]) => F[ToolResult[O]] + logic: (I, StreamingServerContext[F], Seq[Header]) => F[ToolResult[O] | InputRequired] ): ServerTool[I, O, F, StreamingServerContext[F]] = - ServerTool(name, description, inputSchema, inputDecoder, outputSchema, annotations, logic) + serverTool(logic) /** Attaches effectful logic with access to the principal and the [[StreamingServerContext]]; usable only on a secured streaming server. */ def securedStreamingServerLogic[F[_], P]( - logic: (I, P, StreamingServerContext[F], Seq[Header]) => F[ToolResult[O]] + logic: (I, P, StreamingServerContext[F], Seq[Header]) => F[ToolResult[O] | InputRequired] ): ServerTool[I, O, F, SecuredStreamingServerContext[F, P]] = - ServerTool( - name, - description, - inputSchema, - inputDecoder, - outputSchema, - annotations, - (input, context, headers) => logic(input, context.principal, context, headers) - ) + serverTool((input, context, headers) => logic(input, context.principal, context, headers)) /** Attaches synchronous logic that also receives the request headers. */ - def handleWithHeaders(logic: (I, Seq[Header]) => ToolResult[O]): ServerTool[I, O, Identity, ServerContext[Identity]] = - ServerTool(name, description, inputSchema, inputDecoder, outputSchema, annotations, (i, _, headers) => logic(i, headers)) + def handleWithHeaders(logic: (I, Seq[Header]) => ToolResult[O] | InputRequired): ServerTool[I, O, Identity, ServerContext[Identity]] = + serverTool((i, _, headers) => logic(i, headers)) /** Attaches synchronous logic over just the decoded input. */ - def handle(logic: I => ToolResult[O]): ServerTool[I, O, Identity, ServerContext[Identity]] = + def handle(logic: I => ToolResult[O] | InputRequired): ServerTool[I, O, Identity, ServerContext[Identity]] = handleWithHeaders((i, _) => logic(i)) /** Attaches synchronous logic over the decoded input and the principal; usable only on a [[SecuredMcpServer]]. */ - def handleSecured[P](logic: (I, P) => ToolResult[O]): ServerTool[I, O, Identity, SecuredServerContext[Identity, P]] = + def handleSecured[P](logic: (I, P) => ToolResult[O] | InputRequired): ServerTool[I, O, Identity, SecuredServerContext[Identity, P]] = securedServerLogic[Identity, P]((i, principal, _) => logic(i, principal)) + private def serverTool[F[_], C <: ServerContext[F]]( + logic: (I, C, Seq[Header]) => F[ToolResult[O] | InputRequired] + ): ServerTool[I, O, F, C] = + ServerTool(name, description, inputSchema, inputDecoder, outputSchema, annotations, logic, requiredClientCapabilities, mcpHeaders) + /** A fully-defined tool: its metadata plus the logic handling a call, in effect `F` with context `C`. */ case class ServerTool[I, O, F[_], -C <: ServerContext[F]]( name: String, @@ -156,5 +196,7 @@ case class ServerTool[I, O, F[_], -C <: ServerContext[F]]( inputDecoder: Decoder[I], outputSchema: Option[ToolSchema], annotations: Option[ToolAnnotations], - logic: (I, C, Seq[Header]) => F[ToolResult[O]] + logic: (I, C, Seq[Header]) => F[ToolResult[O] | InputRequired], + requiredClientCapabilities: Option[ClientCapabilities] = None, + mcpHeaders: List[McpHeader] = Nil ) diff --git a/server/src/main/scala/chimp/server/transport/JsonRpcHttpResponses.scala b/server/src/main/scala/chimp/server/transport/JsonRpcHttpResponses.scala index 929f3ad2..9fa7ae6c 100644 --- a/server/src/main/scala/chimp/server/transport/JsonRpcHttpResponses.scala +++ b/server/src/main/scala/chimp/server/transport/JsonRpcHttpResponses.scala @@ -1,6 +1,6 @@ package chimp.server.transport -import chimp.server.{McpResponse, OriginCheck, OutboundSink} +import chimp.server.{ListenRequest, McpResponse, OriginCheck, OutboundSink} import io.circe.Json import sttp.model.{Header, HeaderNames, StatusCode} import sttp.monad.MonadError @@ -26,16 +26,29 @@ private[transport] type JsonRejection = (StatusCode, Json) private[transport] val jsonRejectionOutput: EndpointOutput[JsonRejection] = statusCode.and(jsonBody[Json]) /** Checks the origin, then validates the request with `rejection` (runs no tool logic). A rejection - for example a modern validation error - - * is returned on the left, to be sent as a plain JSON error with its own HTTP status; otherwise `handle` runs inside a 200 event stream. + * is returned on the left, to be sent as a plain JSON error with its own HTTP status. An accepted `subscriptions/listen` request gets a + * 200 subscription stream, which stays open until one side ends it; any other request runs `handle`, see + * [[ServerStreamingHttpTransport.eventStream]]. */ private[transport] def respondWithEventStream[F[_], Caps]( originCheck: OriginCheck, headers: Seq[Header], backend: ServerStreamingHttpTransport[F, Caps], - rejection: => Option[McpResponse] -)(handle: OutboundSink[F] => F[Option[Json]])(using m: MonadError[F]): F[Either[JsonRejection, (StatusCode, backend.EventStream)]] = + rejection: => Option[McpResponse], + listen: => Option[ListenRequest] +)(handle: OutboundSink[F] => F[McpResponse])(using m: MonadError[F]): F[Either[JsonRejection, (StatusCode, backend.EventStream)]] = if !originAllowed(originCheck, headers) then m.unit(Right((StatusCode.Forbidden, backend.emptyStream))) else rejection match case Some(response) => m.unit(Left((response.statusCode, response.body.getOrElse(Json.Null)))) - case None => backend.eventStream(handle).map(events => Right((StatusCode.Ok, events))) + case None => + listen match + case Some(request) => m.unit(Right((StatusCode.Ok, backend.subscriptionStream(request)))) + case None => backend.eventStream(handle).map(_.map(events => (StatusCode.Ok, events))) + +/** The plain JSON error to send instead of an event stream, when the handler ends with an error which has its own HTTP status (for example + * a `-32021`) before it sends any message. + */ +private[server] def plainJsonError(response: McpResponse): Option[(StatusCode, Json)] = response match + case McpResponse.JsonError(status, json) => Some((status, json)) + case _ => None diff --git a/server/src/main/scala/chimp/server/transport/SecuredServerHttpTransport.scala b/server/src/main/scala/chimp/server/transport/SecuredServerHttpTransport.scala index ebc452f9..a317b178 100644 --- a/server/src/main/scala/chimp/server/transport/SecuredServerHttpTransport.scala +++ b/server/src/main/scala/chimp/server/transport/SecuredServerHttpTransport.scala @@ -35,7 +35,7 @@ final case class SecuredServerHttpTransport[F[_], S, E, P](path: List[String]): val (headers, json) = input given MonadError[F] = me respondToJsonRpc(server.originCheck, headers) { - handler.handleJsonRpc(json, headers, _ => SecuredServerContext[F, P](principal)) + handler.handleJsonRpc(json, headers, info => SecuredServerContext[F, P](principal, info)) }.map(Right(_)) } ) diff --git a/server/src/main/scala/chimp/server/transport/SecuredServerStreamingHttpTransport.scala b/server/src/main/scala/chimp/server/transport/SecuredServerStreamingHttpTransport.scala index 5ba0d740..641c466a 100644 --- a/server/src/main/scala/chimp/server/transport/SecuredServerStreamingHttpTransport.scala +++ b/server/src/main/scala/chimp/server/transport/SecuredServerStreamingHttpTransport.scala @@ -1,6 +1,5 @@ package chimp.server.transport -import chimp.protocol.ProgressToken import chimp.server.* import io.circe.Json import sttp.model.Header @@ -28,7 +27,7 @@ final case class SecuredServerStreamingHttpTransport[F[_], Caps, S, E, P]( backend: ServerStreamingHttpTransport[F, Caps] ): def serve(server: SecuredStreamingMcpServer[F, S, E, P]): ServerEndpoint[Caps, F] = - val handler = new McpHandler[F, SecuredStreamingServerContext[F, P]](server) + val handler = new McpHandler[F, SecuredStreamingServerContext[F, P]](server, canListen = true) val mcpEndpoint = endpoint.post .securityIn(server.securityInput) .in(path.foldLeft(emptyInput)((inputSoFar, pathComponent) => inputSoFar / pathComponent)) @@ -45,10 +44,16 @@ final case class SecuredServerStreamingHttpTransport[F[_], Caps, S, E, P]( me => { (principal: P) => (input: (Seq[Header], Json)) => val (headers, json) = input given MonadError[F] = me - respondWithEventStream(server.originCheck, headers, backend, handler.validateRequest(json, headers)) { sink => - val makeContext: Option[ProgressToken] => SecuredStreamingServerContext[F, P] = - token => SinkSecuredStreamingServerContext(sink, token, principal) - handler.handleJsonRpc(json, headers, makeContext).map(_.body) + respondWithEventStream( + server.originCheck, + headers, + backend, + handler.validateRequest(json, headers), + handler.listenRequest(json, headers) + ) { sink => + val makeContext: RequestInfo => SecuredStreamingServerContext[F, P] = + info => SinkSecuredStreamingServerContext(sink, info, principal) + handler.handleJsonRpc(json, headers, makeContext) }.map(_.left.map(Right(_))) } ) diff --git a/server/src/main/scala/chimp/server/transport/ServerStdioTransport.scala b/server/src/main/scala/chimp/server/transport/ServerStdioTransport.scala index 619b91d2..87176c51 100644 --- a/server/src/main/scala/chimp/server/transport/ServerStdioTransport.scala +++ b/server/src/main/scala/chimp/server/transport/ServerStdioTransport.scala @@ -1,6 +1,6 @@ package chimp.server.transport -import chimp.protocol.{JSONRPCMessage, ProgressToken} +import chimp.protocol.JSONRPCMessage import chimp.server.* import chimp.transport.{StdioFraming, StdioLineReader} import io.circe.parser @@ -36,7 +36,7 @@ final class ServerStdioTransport( def serve(server: StreamingMcpServer[Identity]): Unit = given MonadError[Identity] = IdentityMonad - val handler = new McpHandler[Identity, StreamingServerContext[Identity]](server) + val handler = new McpHandler[Identity, StreamingServerContext[Identity]](server, canListen = true) val reader = StdioLineReader(in, maxLineLength) val writer = BufferedWriter(OutputStreamWriter(out, StandardCharsets.UTF_8)) @@ -49,12 +49,17 @@ final class ServerStdioTransport( val sink = new OutboundSink[Identity]: def send(message: JSONRPCMessage): Identity[Unit] = writeLine(message.asJson.deepDropNullValues) - val makeContext: Option[ProgressToken] => StreamingServerContext[Identity] = - token => SinkStreamingServerContext(sink, token) + val makeContext: RequestInfo => StreamingServerContext[Identity] = + info => SinkStreamingServerContext(sink, info) - reader.lines - .filter(_.nonEmpty) - .foreach: line => - parser.parse(line) match - case Right(json) => handler.handleJsonRpc(json, Nil, makeContext).body.foreach(writeLine) - case Left(error) => log.warn(s"Failed to parse JSON-RPC line: ${error.getMessage}; raw: $line") + val subscriptions = StdioSubscriptions(handler, writeLine) + + try + reader.lines + .filter(_.nonEmpty) + .foreach: line => + parser.parse(line) match + case Right(json) => + if !subscriptions.accept(json) then handler.handleJsonRpc(json, Nil, makeContext).body.foreach(writeLine) + case Left(error) => log.warn(s"Failed to parse JSON-RPC line: ${error.getMessage}; raw: $line") + finally subscriptions.endAll() diff --git a/server/src/main/scala/chimp/server/transport/ServerStreamingHttpTransport.scala b/server/src/main/scala/chimp/server/transport/ServerStreamingHttpTransport.scala index d6529d44..ef10ea48 100644 --- a/server/src/main/scala/chimp/server/transport/ServerStreamingHttpTransport.scala +++ b/server/src/main/scala/chimp/server/transport/ServerStreamingHttpTransport.scala @@ -1,12 +1,10 @@ package chimp.server.transport -import chimp.protocol.ProgressToken import chimp.server.* import io.circe.Json import sttp.capabilities.Streams -import sttp.model.Header +import sttp.model.{Header, StatusCode} import sttp.monad.MonadError -import sttp.monad.syntax.* import sttp.tapir.* import sttp.tapir.json.circe.* import sttp.tapir.server.ServerEndpoint @@ -27,10 +25,20 @@ abstract class ServerStreamingHttpTransport[F[_], Caps](path: List[String]) exte type EventStream def sseBody: StreamBodyIO[streams.BinaryStream, EventStream, Caps] def emptyStream: EventStream - def eventStream(handle: OutboundSink[F] => F[Option[Json]]): F[EventStream] + + /** Runs `handle`, which sends its messages to the sink and ends with the response. If it ends with an error which has its own HTTP status + * (see [[plainJsonError]]) before it sends any message, that error is returned on the left, to be sent as plain JSON. Otherwise the + * messages and then the response's body go on a 200 event stream. + */ + def eventStream(handle: OutboundSink[F] => F[McpResponse]): F[Either[(StatusCode, Json), EventStream]] + + /** The stream of an accepted `subscriptions/listen` request. It opens the subscription when the stream starts, and cancels it when the + * client closes the stream. The server ends it by closing the subscription's output. + */ + private[server] def subscriptionStream(listen: ListenRequest): EventStream final def serve(server: StreamingMcpServer[F]): ServerEndpoint[Caps, F] = - val handler = new McpHandler[F, StreamingServerContext[F]](server) + val handler = new McpHandler[F, StreamingServerContext[F]](server, canListen = true) val endpoint = sttp.tapir.endpoint.post .in(path.foldLeft(emptyInput)((inputSoFar, pathComponent) => inputSoFar / pathComponent)) .in(extractFromRequest(_.headers)) @@ -44,10 +52,16 @@ abstract class ServerStreamingHttpTransport[F[_], Caps](path: List[String]) exte me => { (input: (Seq[Header], Json)) => val (headers, json) = input given MonadError[F] = me - respondWithEventStream(server.originCheck, headers, this, handler.validateRequest(json, headers)) { sink => - val makeContext: Option[ProgressToken] => StreamingServerContext[F] = - token => SinkStreamingServerContext(sink, token) - handler.handleJsonRpc(json, headers, makeContext).map(_.body) + respondWithEventStream( + server.originCheck, + headers, + this, + handler.validateRequest(json, headers), + handler.listenRequest(json, headers) + ) { sink => + val makeContext: RequestInfo => StreamingServerContext[F] = + info => SinkStreamingServerContext(sink, info) + handler.handleJsonRpc(json, headers, makeContext) } } ) diff --git a/server/src/test/scala/chimp/server/McpHandlerInputRequiredSpec.scala b/server/src/test/scala/chimp/server/McpHandlerInputRequiredSpec.scala new file mode 100644 index 00000000..d47afba8 --- /dev/null +++ b/server/src/test/scala/chimp/server/McpHandlerInputRequiredSpec.scala @@ -0,0 +1,226 @@ +package chimp.server + +import chimp.protocol.* +import io.circe.* +import io.circe.syntax.* +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.model.StatusCode +import sttp.monad.{IdentityMonad, MonadError} +import sttp.shared.Identity +import sttp.tapir.Schema + +class McpHandlerInputRequiredSpec extends AnyFlatSpec with Matchers: + import JSONRPCMessage.* + + given MonadError[Identity] = IdentityMonad + + case class NoInput() derives Schema, Codec + + private val nameSchema = Json.obj("type" -> "object".asJson, "properties" -> Json.obj("name" -> Json.obj("type" -> "string".asJson))) + + private def name(ctx: ServerContext[Identity], key: String): Option[String] = + ctx.request.elicitResult(key).flatMap(_.content).flatMap(_.get("name")).flatMap(_.asString) + + private val greetTool = tool("greet") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + name(ctx, "user_name") match + case Some(name) => ToolResult.text(s"Hello, $name!") + case None => InputRequired("user_name" -> InputRequired.elicitForm("What is your name?", nameSchema)) + + private val twoRoundsTool = tool("two_rounds") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + (ctx.request.requestState, name(ctx, "first"), name(ctx, "second")) match + case (Some("round-2"), _, Some(second)) => ToolResult.text(s"done: $second") + case (Some("round-1"), Some(_), _) => + InputRequired("second" -> InputRequired.elicitForm("Second?", nameSchema)).withRequestState("round-2") + case _ => InputRequired("first" -> InputRequired.elicitForm("First?", nameSchema)).withRequestState("round-1") + + private val samplingParams = CreateMessageParams(List(SamplingMessage(Role.User, ToolContent.Text(text = "Hi?"))), maxTokens = 10) + + private val askAllTool = tool("ask_all") + .input[NoInput] + .handle: _ => + InputRequired( + "form" -> InputRequired.elicitForm("Name?", nameSchema), + "url" -> InputRequired.elicitUrl("Sign in", "https://example.com/login"), + "sample" -> InputRequired.sample(samplingParams), + "roots" -> InputRequired.listRoots + ) + + private val adaptiveTool = tool("adaptive") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + if ctx.request.clientCapabilities.exists(_.sampling.isDefined) then InputRequired("q" -> InputRequired.sample(samplingParams)) + else InputRequired("q" -> InputRequired.elicitForm("Name?", nameSchema)) + + private val stateOnlyTool = tool("state_only") + .input[NoInput] + .serverLogicWithContext[Identity]: (_, ctx, _) => + ctx.request.requestState.fold(InputRequired.withRequestState("again"))(state => ToolResult.text(s"state $state")) + + private val greetPrompt = prompt("greet") + .serverLogicWithContext[Identity]: (_, ctx, _) => + name(ctx, "user_name") match + case Some(name) => GetPromptResult(messages = List(PromptMessage(Role.User, ToolContent.Text(text = s"Hello, $name")))) + case None => InputRequired("user_name" -> InputRequired.elicitForm("Name?", nameSchema)) + + private val rootsResource = resource("test://roots") + .serverLogicWithContext[Identity]: (ctx, _) => + ctx.request.rootsResult("roots") match + case Some(roots) => Right(List(ResourceContents.Text("test://roots", roots.roots.map(_.uri).mkString(",")))) + case None => InputRequired("roots" -> InputRequired.listRoots) + + private val rootsTemplate = resourceTemplate("test://roots/{id}") + .serverLogicWithContext[Identity]: (_, uri, ctx, _) => + ctx.request.rootsResult("roots") match + case Some(roots) => Right(List(ResourceContents.Text(uri, roots.roots.map(_.uri).mkString(",")))) + case None => InputRequired("roots" -> InputRequired.listRoots) + + private val handler = McpHandler( + McpServer[Identity]() + .addTools(greetTool, twoRoundsTool, askAllTool, adaptiveTool, stateOnlyTool) + .addPrompt(greetPrompt) + .addResource(rootsResource) + .addResourceTemplate(rootsTemplate) + ) + + private val allCapabilities = + Json.obj("sampling" -> Json.obj(), "roots" -> Json.obj(), "elicitation" -> Json.obj("form" -> Json.obj(), "url" -> Json.obj())) + + private def call(method: String, params: Json, capabilities: Json = allCapabilities, modern: Boolean = true): McpResponse = + val meta = + if modern then Json.obj(ProtocolMeta.ProtocolVersionKey -> "2026-07-28".asJson, ProtocolMeta.ClientCapabilities -> capabilities) + else Json.obj() + val request: JSONRPCMessage = Request(method = method, params = Some(params.deepMerge(Json.obj("_meta" -> meta))), id = RequestId("r")) + handler.handleJsonRpc(request.asJson, Seq.empty) + + private def callTool(tool: String, extra: (String, Json)*): McpResponse = + call("tools/call", Json.obj(("name" -> tool.asJson) +: ("arguments" -> Json.obj()) +: extra*)) + + private def bodyOf(response: McpResponse): Json = response.body.getOrElse(fail("Expected a body")) + + private def resultOf(response: McpResponse): Json = + bodyOf(response).as[JSONRPCMessage].getOrElse(fail("decode")) match + case Response(_, _, result) => result + case other => fail(s"Expected Response, got $other") + + private def errorOf(response: McpResponse): JSONRPCErrorObject = + bodyOf(response).as[JSONRPCMessage].getOrElse(fail("decode")) match + case Error(_, _, error) => error + case other => fail(s"Expected Error, got $other") + + private def nameAnswer(value: String): Json = ElicitResult(ElicitAction.Accept, Some(Map("name" -> value.asJson))).asJson + + private def textOf(result: Json): Option[String] = result.hcursor.downField("content").downArray.get[String]("text").toOption + + "An input-required outcome" should "reach a modern client as resultType input_required with its keyed input requests" in: + val response = callTool("greet") + response.statusCode shouldBe StatusCode.Ok + val result = resultOf(response) + result.hcursor.get[String]("resultType") shouldBe Right("input_required") + result.hcursor.downField("inputRequests").downField("user_name").get[String]("method") shouldBe Right("elicitation/create") + result.hcursor.downField("_meta").downField(ProtocolMeta.ServerInfo).succeeded shouldBe true + result.hcursor.downField("requestState").succeeded shouldBe false + result.hcursor.downField("ttlMs").succeeded shouldBe false + + it should "let the handler read the answer on the retry, and ignore keys it did not ask for" in: + val responses = Json.obj("user_name" -> nameAnswer("Alice"), "unexpected" -> nameAnswer("Mallory")) + val result = resultOf(callTool("greet", "inputResponses" -> responses)) + result.hcursor.get[String]("resultType") shouldBe Right("complete") + textOf(result) shouldBe Some("Hello, Alice!") + + it should "let the handler ask again when the retry lacks an answer it needs" in: + val result = resultOf(callTool("greet", "inputResponses" -> Json.obj("wrong_key" -> nameAnswer("Alice")))) + result.hcursor.get[String]("resultType") shouldBe Right("input_required") + + it should "carry the request state across several rounds" in: + val first = resultOf(callTool("two_rounds")) + first.hcursor.get[String]("requestState") shouldBe Right("round-1") + val second = + resultOf(callTool("two_rounds", "inputResponses" -> Json.obj("first" -> nameAnswer("a")), "requestState" -> "round-1".asJson)) + second.hcursor.get[String]("requestState") shouldBe Right("round-2") + second.hcursor.downField("inputRequests").keys.map(_.toList) shouldBe Some(List("second")) + val third = + resultOf(callTool("two_rounds", "inputResponses" -> Json.obj("second" -> nameAnswer("b")), "requestState" -> "round-2".asJson)) + textOf(third) shouldBe Some("done: b") + + it should "carry only a request state when the handler asks for no input" in: + val result = resultOf(callTool("state_only")) + result.hcursor.get[String]("requestState") shouldBe Right("again") + result.hcursor.downField("inputRequests").succeeded shouldBe false + textOf(resultOf(callTool("state_only", "requestState" -> "again".asJson))) shouldBe Some("state again") + + it should "encode form, URL, sampling and roots requests as the schema defines them" in: + val requests = resultOf(callTool("ask_all")).hcursor.downField("inputRequests") + requests.downField("form").downField("params").get[String]("message") shouldBe Right("Name?") + requests.downField("url").downField("params").get[String]("mode") shouldBe Right("url") + requests.downField("sample").get[String]("method") shouldBe Right("sampling/createMessage") + requests.downField("roots").get[String]("method") shouldBe Right("roots/list") + + it should "let the handler choose what to ask for from the client capabilities" in: + def asked(capabilities: Json) = + call("tools/call", Json.obj("name" -> "adaptive".asJson), capabilities).body.flatMap( + _.hcursor.downField("result").downField("inputRequests").downField("q").get[String]("method").toOption + ) + asked(Json.obj("sampling" -> Json.obj())) shouldBe Some("sampling/createMessage") + asked(Json.obj("elicitation" -> Json.obj())) shouldBe Some("elicitation/create") + + it should "be possible for prompts, resources and resource templates" in: + resultOf(call("prompts/get", Json.obj("name" -> "greet".asJson))).hcursor.get[String]("resultType") shouldBe Right("input_required") + for uri <- List("test://roots", "test://roots/1") do + withClue(uri): + val first = resultOf(call("resources/read", Json.obj("uri" -> uri.asJson))) + first.hcursor.get[String]("resultType") shouldBe Right("input_required") + first.hcursor.downField("ttlMs").succeeded shouldBe false + val roots = ListRootsResult(List(Root("file:///a"))).asJson + val retry = call("resources/read", Json.obj("uri" -> uri.asJson, "inputResponses" -> Json.obj("roots" -> roots))) + resultOf(retry).hcursor.downField("contents").downArray.get[String]("text") shouldBe Right("file:///a") + val promptRetry = call("prompts/get", Json.obj("name" -> "greet".asJson, "inputResponses" -> Json.obj("user_name" -> nameAnswer("Bo")))) + resultOf(promptRetry).hcursor.downField("messages").downArray.downField("content").get[String]("text") shouldBe Right("Hello, Bo") + + it should "fail with -32021, the missing capabilities and HTTP 400 when the client did not declare what an input request needs" in: + val cases = List( + Json.obj() -> Json.obj( + "sampling" -> Json.obj(), + "roots" -> Json.obj(), + "elicitation" -> Json.obj("form" -> Json.obj(), "url" -> Json.obj()) + ), + Json.obj("sampling" -> Json.obj(), "roots" -> Json.obj(), "elicitation" -> Json.obj()) -> Json.obj( + "elicitation" -> Json.obj("url" -> Json.obj()) + ), + Json.obj("sampling" -> Json.obj(), "roots" -> Json.obj(), "elicitation" -> Json.obj("url" -> Json.obj())) -> Json.obj( + "elicitation" -> Json.obj("form" -> Json.obj()) + ) + ) + for (declared, missing) <- cases do + withClue(declared.noSpaces): + val response = call("tools/call", Json.obj("name" -> "ask_all".asJson), declared) + response.statusCode shouldBe StatusCode.BadRequest + val error = errorOf(response) + error.code shouldBe JSONRPCErrorCodes.MissingRequiredClientCapability.code + error.data.flatMap(_.hcursor.downField("requiredCapabilities").focus) shouldBe Some(missing) + + it should "fail with -32602 and HTTP 400 when inputResponses or requestState is malformed, before the handler runs" in: + val malformed = List( + "inputResponses" -> 12345.asJson, + "inputResponses" -> Json.Null, + "inputResponses" -> Json.arr(), + "inputResponses" -> Json.obj("user_name" -> 12345.asJson), + "inputResponses" -> Json.obj("user_name" -> Json.obj("action" -> "maybe".asJson)), + "requestState" -> 1.asJson + ) + for field <- malformed do + withClue(field): + val response = callTool("greet", field) + response.statusCode shouldBe StatusCode.BadRequest + errorOf(response).code shouldBe JSONRPCErrorCodes.InvalidParams.code + + it should "become an internal error naming the required protocol version on a legacy request" in: + val response = call("tools/call", Json.obj("name" -> "greet".asJson, "arguments" -> Json.obj()), modern = false) + response.statusCode shouldBe StatusCode.Ok + val error = errorOf(response) + error.code shouldBe JSONRPCErrorCodes.InternalError.code + error.message should include("2026-07-28") diff --git a/server/src/test/scala/chimp/server/McpHandlerRequestStateSpec.scala b/server/src/test/scala/chimp/server/McpHandlerRequestStateSpec.scala new file mode 100644 index 00000000..e1643eb6 --- /dev/null +++ b/server/src/test/scala/chimp/server/McpHandlerRequestStateSpec.scala @@ -0,0 +1,109 @@ +package chimp.server + +import chimp.protocol.* +import io.circe.* +import io.circe.syntax.* +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.model.StatusCode +import sttp.monad.{IdentityMonad, MonadError} +import sttp.shared.Identity +import sttp.tapir.Schema + +import java.time.{Clock, Instant, ZoneId, ZoneOffset} +import java.util.concurrent.atomic.{AtomicInteger, AtomicLong} +import scala.concurrent.duration.* + +class McpHandlerRequestStateSpec extends AnyFlatSpec with Matchers: + import JSONRPCMessage.* + + given MonadError[Identity] = IdentityMonad + + case class CountInput(count: Int) derives Codec, Schema + + private final class SettableClock(millis: AtomicLong) extends Clock: + def getZone: ZoneId = ZoneOffset.UTC + override def withZone(zone: ZoneId): Clock = this + def instant(): Instant = Instant.ofEpochMilli(millis.get()) + + private val now = AtomicLong(1_000_000L) + private val signer = RequestStateSigner(Array.fill[Byte](32)(7), ttl = 1.minute, clock = SettableClock(now)) + private val runs = AtomicInteger(0) + + private def stateful(name: String) = tool(name) + .input[CountInput] + .serverLogicWithContext[Identity]: (in, ctx, _) => + runs.incrementAndGet() + ctx.request.requestState match + case Some(state) => ToolResult.text(s"state $state") + case None => InputRequired.withRequestState(s"count=${in.count}") + + private val handler = McpHandler(McpServer[Identity]().addTools(stateful("a"), stateful("b")).withRequestStateSigner(signer)) + + private val securedHandler = + val secured = McpServer[Identity]() + .serverSecurityLogicPure[Unit, Unit, String](sttp.tapir.emptyInput, sttp.tapir.emptyOutput)(_ => Right("ignored")) + .addTool(stateful("a")) + .withRequestStateSigner(signer, principal => principal) + McpHandler[Identity, SecuredServerContext[Identity, String]](secured) + + private def request(tool: String, count: Int, state: Option[String]): Json = + val meta = Json.obj(ProtocolMeta.ProtocolVersionKey -> "2026-07-28".asJson, ProtocolMeta.ClientCapabilities -> Json.obj()) + val params = Json.obj("name" -> tool.asJson, "arguments" -> Json.obj("count" -> count.asJson), "_meta" -> meta) + val message: JSONRPCMessage = + Request( + method = "tools/call", + params = Some(params.deepMerge(Json.obj("requestState" -> state.asJson)).dropNullValues), + id = RequestId("r") + ) + message.asJson + + private def call(tool: String, count: Int = 1, state: Option[String] = None): McpResponse = + handler.handleJsonRpc(request(tool, count, state), Seq.empty) + + private def callAs(principal: String, state: Option[String] = None): McpResponse = + securedHandler.handleJsonRpc(request("a", 1, state), Seq.empty, info => SecuredServerContext[Identity, String](principal, info)) + + private def result(response: McpResponse): Json = + response.body.flatMap(_.hcursor.downField("result").focus).getOrElse(fail(s"Expected a result, got ${response.body}")) + + private def stateOf(response: McpResponse): String = result(response).hcursor.get[String]("requestState").getOrElse(fail("No state")) + + private def textOf(response: McpResponse): Option[String] = + result(response).hcursor.downField("content").downArray.get[String]("text").toOption + + private def shouldBeRejected(call: => McpResponse): org.scalatest.Assertion = + val runsBefore = runs.get() + val response = call + response.statusCode shouldBe StatusCode.BadRequest + response.body.flatMap(_.hcursor.downField("error").get[Int]("code").toOption) shouldBe Some(JSONRPCErrorCodes.InvalidParams.code) + runs.get() shouldBe runsBefore + + "A request state signer" should "sign the state and give the handler the original state on the retry" in: + val signed = stateOf(call("a", count = 3)) + signed should not include "count=3" + textOf(call("a", count = 3, state = Some(signed))) shouldBe Some("state count=3") + + it should "reject a changed state before the handler runs" in: + val signed = stateOf(call("a")) + shouldBeRejected(call("a", state = Some(signed + "-TAMPERED"))) + shouldBeRejected(call("a", state = Some("x" + signed.drop(1)))) + shouldBeRejected(call("a", state = Some("count=1"))) + + it should "reject an expired state" in: + val signed = stateOf(call("a")) + now.addAndGet(2.minutes.toMillis) + shouldBeRejected(call("a", state = Some(signed))) + + it should "reject a state of another tool, or of other arguments" in: + val signed = stateOf(call("a", count = 1)) + shouldBeRejected(call("b", count = 1, state = Some(signed))) + shouldBeRejected(call("a", count = 2, state = Some(signed))) + + it should "reject a state of another principal on a secured server" in: + val signed = stateOf(callAs("alice")) + textOf(callAs("alice", Some(signed))) shouldBe Some("state count=1") + shouldBeRejected(callAs("bob", Some(signed))) + + it should "need a secret of at least 32 bytes" in: + an[IllegalArgumentException] should be thrownBy RequestStateSigner(Array.fill[Byte](31)(1)) diff --git a/server/src/test/scala/chimp/server/McpHandlerSpec.scala b/server/src/test/scala/chimp/server/McpHandlerSpec.scala index 5770649d..b3feabc2 100644 --- a/server/src/test/scala/chimp/server/McpHandlerSpec.scala +++ b/server/src/test/scala/chimp/server/McpHandlerSpec.scala @@ -178,7 +178,7 @@ class McpHandlerSpec extends AnyFlatSpec with Matchers: private def modernHttpCall(method: String, params: Json, extraHeaders: (String, String)*): McpResponse = val req: JSONRPCMessage = Request(method = method, params = Some(params.deepMerge(Json.obj(modernMeta))), id = RequestId("h")) - val headers = Header("Host", "localhost") +: extraHeaders.map(Header(_, _)) + val headers = Header("Host", "localhost") +: Header(ProtocolMeta.ProtocolVersionHeader, "2026-07-28") +: extraHeaders.map(Header(_, _)) headersHandler.handleJsonRpc(req.asJson, headers) private def shouldBeHeaderMismatch(response: McpResponse): org.scalatest.Assertion = @@ -211,6 +211,63 @@ class McpHandlerSpec extends AnyFlatSpec with Matchers: response.statusCode shouldBe sttp.model.StatusCode.Ok extractJsonFromResponse(response).as[JSONRPCMessage].getOrElse(fail("decode")) shouldBe a[Response] + it should "decode a Base64 sentinel Mcp-Name before comparing it, and reject a malformed one with -32020" in: + val params = Json.obj("name" -> Json.fromString("echo"), "arguments" -> Json.obj("message" -> Json.fromString("hi"))) + val encoded = "=?base64?ZWNobw==?=" + modernHttpCall("tools/call", params, "Mcp-Method" -> "tools/call", "Mcp-Name" -> encoded).statusCode shouldBe sttp.model.StatusCode.Ok + shouldBeHeaderMismatch(modernHttpCall("tools/call", params, "Mcp-Method" -> "tools/call", "Mcp-Name" -> "=?base64?ZWNobw?=")) + shouldBeHeaderMismatch(modernHttpCall("tools/call", params, "Mcp-Method" -> "tools/call", "Mcp-Name" -> "=?base64?d3Jvbmc=?=")) + + case class RegionInput(region: String, limit: Option[Int], dryRun: Option[Boolean]) derives Schema, Codec + + private val regionTool = tool("region") + .input[RegionInput] + .withMcpHeader("region", "Region") + .withMcpHeader("limit", "Limit") + .withMcpHeader("dryRun", "Dry-Run") + .handle(in => ToolResult.text(in.region)) + private val customHeadersHandler = McpHandler(McpServer(tools = List(regionTool))) + + private def regionCall(arguments: Json, headers: (String, String)*): McpResponse = + val params = Json.obj("name" -> Json.fromString("region"), "arguments" -> arguments).deepMerge(Json.obj(modernMeta)) + val req: JSONRPCMessage = Request(method = "tools/call", params = Some(params), id = RequestId("c")) + val standard = Seq("Host" -> "localhost", ProtocolMeta.ProtocolVersionHeader -> "2026-07-28", "Mcp-Method" -> "tools/call") + customHeadersHandler.handleJsonRpc(req.asJson, (standard ++ (("Mcp-Name" -> "region") +: headers)).map(Header(_, _))) + + private def shouldBeServed(response: McpResponse): org.scalatest.Assertion = + response.statusCode shouldBe sttp.model.StatusCode.Ok + extractJsonFromResponse(response).as[JSONRPCMessage].getOrElse(fail("decode")) shouldBe a[Response] + + it should "serve a tools/call whose Mcp-Param headers match the arguments" in: + val arguments = Json.obj("region" -> Json.fromString("us-west1"), "limit" -> Json.fromInt(42), "dryRun" -> Json.True) + shouldBeServed(regionCall(arguments, "mcp-param-region" -> " us-west1 ", "Mcp-Param-Limit" -> "42.0", "MCP-PARAM-DRY-RUN" -> "true")) + shouldBeServed( + regionCall(arguments, "Mcp-Param-Region" -> "=?base64?dXMtd2VzdDE=?=", "Mcp-Param-Limit" -> "42", "Mcp-Param-Dry-Run" -> "true") + ) + + it should "expect no Mcp-Param header for a null or absent argument, and ignore unknown Mcp-Param headers" in: + shouldBeServed(regionCall(Json.obj("region" -> Json.fromString("eu"), "limit" -> Json.Null), "Mcp-Param-Region" -> "eu")) + shouldBeServed(regionCall(Json.obj("region" -> Json.fromString("eu")), "Mcp-Param-Region" -> "eu", "Mcp-Param-Other" -> "x")) + + it should "reject a tools/call whose Mcp-Param headers are missing, malformed or do not match the arguments with -32020" in: + val arguments = Json.obj("region" -> Json.fromString("Hello"), "limit" -> Json.fromInt(42)) + val matching = List("Mcp-Param-Region" -> "Hello", "Mcp-Param-Limit" -> "42") + shouldBeServed(regionCall(arguments, matching*)) + shouldBeHeaderMismatch(regionCall(arguments, "Mcp-Param-Limit" -> "42")) + shouldBeHeaderMismatch(regionCall(arguments, "Mcp-Param-Region" -> "hello", "Mcp-Param-Limit" -> "42")) + shouldBeHeaderMismatch(regionCall(arguments, "Mcp-Param-Region" -> "Hello", "Mcp-Param-Limit" -> "43")) + shouldBeHeaderMismatch(regionCall(arguments, "Mcp-Param-Region" -> "=?base64?SGVsbG8?=", "Mcp-Param-Limit" -> "42")) + shouldBeHeaderMismatch(regionCall(arguments, "Mcp-Param-Region" -> "=?base64?SGVs!!!bG8=?=", "Mcp-Param-Limit" -> "42")) + shouldBeHeaderMismatch(regionCall(arguments, "Mcp-Param-Region" -> "SGVsbG8=", "Mcp-Param-Limit" -> "42")) + shouldBeHeaderMismatch(regionCall(arguments, matching :+ ("Mcp-Param-Dry-Run" -> "true")*)) + + it should "not check Mcp-Param headers on a legacy request or a modern request without HTTP" in: + val params = Json.obj("name" -> Json.fromString("region"), "arguments" -> Json.obj("region" -> Json.fromString("eu"))) + val legacy: JSONRPCMessage = Request(method = "tools/call", params = Some(params), id = RequestId("l")) + shouldBeServed(customHeadersHandler.handleJsonRpc(legacy.asJson, Seq(Header("Host", "localhost"), Header("Mcp-Param-Region", "x")))) + val stdio: JSONRPCMessage = Request(method = "tools/call", params = Some(params.deepMerge(Json.obj(modernMeta))), id = RequestId("s")) + shouldBeServed(customHeadersHandler.handleJsonRpc(stdio.asJson, Seq.empty)) + it should "serve a classic initialize with no modern protocol version rather than rejecting it with -32022" in: val req: JSONRPCMessage = Request(method = "initialize", id = RequestId("legacy-init")) val respJson = extractJsonFromResponse(handler.handleJsonRpc(req.asJson, Seq.empty)) @@ -697,3 +754,216 @@ class McpHandlerSpec extends AnyFlatSpec with Matchers: featureResult("resources/subscribe", Some(params), "n1") match case Error(_, _, error) => error.code shouldBe MethodNotFound.code case _ => fail("Expected Error") + + private val seenRequest = new java.util.concurrent.atomic.AtomicReference(Option.empty[RequestInfo]) + + private val loggingTool = tool("logs") + .input[EchoInput] + .streamingServerLogic[Identity]: (_, ctx, _) => + seenRequest.set(Some(ctx.request)) + LoggingLevel.values.foreach(level => ctx.log(level, Json.fromString(level.toString))) + ToolResult.text("logged") + + private val streamingHandler = + McpHandler[Identity, StreamingServerContext[Identity]](McpServer[Identity]().streaming.addStreamingTool(loggingTool)) + + private def callLoggingTool(meta: Json, headers: Seq[Header] = Seq.empty): (McpResponse, List[LoggingLevel]) = + val sent = scala.collection.mutable.ListBuffer.empty[JSONRPCMessage] + val sink = new OutboundSink[Identity]: + def send(message: JSONRPCMessage): Unit = sent += message + val params = Json.obj("name" -> Json.fromString("logs"), "arguments" -> Json.obj("message" -> Json.fromString("hi")), "_meta" -> meta) + val req: JSONRPCMessage = Request(method = "tools/call", params = Some(params), id = RequestId("log")) + val response = streamingHandler.handleJsonRpc(req.asJson, headers, info => SinkStreamingServerContext(sink, info)) + val levels = sent.toList.collect { case Notification(_, "notifications/message", Some(p)) => + p.as[LoggingMessageParams].toOption.get.level + } + (response, levels) + + private def modernToolMeta(extra: (String, Json)*): Json = + Json.obj( + (ProtocolMeta.ProtocolVersionKey -> Json.fromString("2026-07-28")) +: + (ProtocolMeta.ClientCapabilities -> Json.obj("sampling" -> Json.obj())) +: + extra* + ) + + it should "give a handler the facts of a modern request" in: + val clientInfo = Json.obj("name" -> Json.fromString("cli"), "version" -> Json.fromString("1.2")) + val meta = modernToolMeta( + ProtocolMeta.ClientInfo -> clientInfo, + ProtocolMeta.LogLevel -> Json.fromString("warning"), + "progressToken" -> Json.fromString("p1") + ) + callLoggingTool(meta)._1.statusCode shouldBe sttp.model.StatusCode.Ok + seenRequest.get() shouldBe Some( + RequestInfo( + protocolVersion = Some(ProtocolVersion.V2026_07_28), + clientCapabilities = Some(ClientCapabilities(sampling = Some(Json.obj()))), + clientInfo = Some(Implementation("cli", "1.2")), + logLevel = Some(LoggingLevel.Warning), + progressToken = Some(ProgressToken("p1")) + ) + ) + + it should "give a handler only the protocol version and progress token of a legacy request" in: + val meta = Json.obj("progressToken" -> Json.fromString("p2"), ProtocolMeta.LogLevel -> Json.fromString("debug")) + callLoggingTool(meta, Seq(Header(ProtocolMeta.ProtocolVersionHeader, "2025-11-25")))._1.statusCode shouldBe sttp.model.StatusCode.Ok + seenRequest.get() shouldBe Some( + RequestInfo(protocolVersion = Some(ProtocolVersion.V2025_11_25), progressToken = Some(ProgressToken("p2"))) + ) + + it should "send no log messages for a modern request without a logLevel" in: + callLoggingTool(modernToolMeta())._2 shouldBe Nil + + it should "send only log messages at or above the logLevel of a modern request" in: + callLoggingTool(modernToolMeta(ProtocolMeta.LogLevel -> Json.fromString("error")))._2 shouldBe + List(LoggingLevel.Error, LoggingLevel.Critical, LoggingLevel.Alert, LoggingLevel.Emergency) + + it should "send all log messages for a legacy request" in: + callLoggingTool(Json.obj())._2 shouldBe LoggingLevel.values.toList + + private def describe(ctx: ServerContext[Identity]): String = + s"${ctx.request.protocolVersion.map(_.name).getOrElse("-")} ${ctx.request.clientInfo.map(_.name).getOrElse("-")}" + + private val contextServer = McpServer[Identity]() + .addTool(tool("ctx").input[EchoInput].serverLogicWithContext[Identity]((_, ctx, _) => ToolResult.text(describe(ctx)))) + .addPrompt( + prompt("ctx").serverLogicWithContext[Identity]((_, ctx, _) => + GetPromptResult(messages = List(PromptMessage(Role.User, ToolContent.Text(text = describe(ctx))))) + ) + ) + .addResource( + resource("test://ctx").serverLogicWithContext[Identity]((ctx, _) => Right(List(ResourceContents.Text("test://ctx", describe(ctx))))) + ) + .addResourceTemplate( + resourceTemplate("test://ctx/{id}") + .serverLogicWithContext[Identity]((_, uri, ctx, _) => Right(List(ResourceContents.Text(uri, describe(ctx))))) + ) + + it should "give the request facts to tool, prompt, resource and resource template logic" in: + val meta = Json.obj( + "_meta" -> modernToolMeta(ProtocolMeta.ClientInfo -> Json.obj("name" -> Json.fromString("cli"), "version" -> Json.fromString("1"))) + ) + val calls = List( + "tools/call" -> Json.obj("name" -> Json.fromString("ctx"), "arguments" -> Json.obj("message" -> Json.fromString("hi"))), + "prompts/get" -> Json.obj("name" -> Json.fromString("ctx")), + "resources/read" -> Json.obj("uri" -> Json.fromString("test://ctx")), + "resources/read" -> Json.obj("uri" -> Json.fromString("test://ctx/1")) + ) + for (method, params) <- calls do + val req: JSONRPCMessage = Request(method = method, params = Some(params.deepMerge(meta)), id = RequestId("ctx")) + val body = extractJsonFromResponse(McpHandler(contextServer).handleJsonRpc(req.asJson, Seq.empty)) + withClue(s"$method $params"): + body.noSpaces should include("2026-07-28 cli") + + private val samplingTool = tool("needs_sampling") + .withRequiredClientCapabilities(ClientCapabilities(sampling = Some(Json.obj()))) + .input[EchoInput] + .handle(in => ToolResult.text(in.message)) + + private val capabilityHandler = McpHandler(McpServer[Identity]().addTool(samplingTool)) + + private def samplingToolCall(meta: Json): Json = + val params = Json.obj("name" -> Json.fromString("needs_sampling"), "arguments" -> Json.obj("message" -> Json.fromString("hi"))) + val req: JSONRPCMessage = + Request(method = "tools/call", params = Some(params.deepMerge(Json.obj("_meta" -> meta))), id = RequestId("cap")) + req.asJson + + private def callSamplingTool(meta: Json): McpResponse = capabilityHandler.handleJsonRpc(samplingToolCall(meta), Seq.empty) + + it should "reject a modern call of a tool that needs an undeclared client capability with -32021 and HTTP 400" in: + val meta = Json.obj(ProtocolMeta.ProtocolVersionKey -> Json.fromString("2026-07-28"), ProtocolMeta.ClientCapabilities -> Json.obj()) + val response = callSamplingTool(meta) + response.statusCode shouldBe sttp.model.StatusCode.BadRequest + capabilityHandler.validateRequest(samplingToolCall(meta), Seq.empty).map(_.statusCode) shouldBe Some(sttp.model.StatusCode.BadRequest) + extractJsonFromResponse(response).as[JSONRPCMessage].getOrElse(fail("decode")) match + case Error(_, id, err) => + id shouldBe RequestId("cap") + err.code shouldBe MissingRequiredClientCapability.code + err.data.flatMap(_.hcursor.downField("requiredCapabilities").focus) shouldBe Some(Json.obj("sampling" -> Json.obj())) + case other => fail(s"Expected Error, got $other") + + it should "serve a modern call of a tool whose required client capabilities are declared" in: + callSamplingTool(modernToolMeta()).statusCode shouldBe sttp.model.StatusCode.Ok + + it should "serve a legacy call of a tool that needs a client capability" in: + callSamplingTool(Json.obj()).statusCode shouldBe sttp.model.StatusCode.Ok + + it should "check nested required client capabilities key by key" in: + val formTool = tool("needs_form") + .withRequiredClientCapabilities(ClientCapabilities(elicitation = Some(Json.obj("form" -> Json.obj())))) + .input[EchoInput] + .handle(in => ToolResult.text(in.message)) + val formHandler = McpHandler(McpServer[Identity]().addTool(formTool)) + def call(declared: Json): sttp.model.StatusCode = + val meta = Json.obj(ProtocolMeta.ProtocolVersionKey -> Json.fromString("2026-07-28"), ProtocolMeta.ClientCapabilities -> declared) + val params = + Json.obj("name" -> Json.fromString("needs_form"), "arguments" -> Json.obj("message" -> Json.fromString("hi")), "_meta" -> meta) + val req: JSONRPCMessage = Request(method = "tools/call", params = Some(params), id = RequestId("form")) + formHandler.handleJsonRpc(req.asJson, Seq.empty).statusCode + call(Json.obj("elicitation" -> Json.obj())) shouldBe sttp.model.StatusCode.BadRequest + call(Json.obj("elicitation" -> Json.obj("url" -> Json.obj()))) shouldBe sttp.model.StatusCode.BadRequest + call(Json.obj("elicitation" -> Json.obj("form" -> Json.obj(), "url" -> Json.obj()))) shouldBe sttp.model.StatusCode.Ok + + it should "serve a request whose numeric id is beyond the Int range and echo the id" in: + val json = parseJson("""{"jsonrpc":"2.0","id":1763561090000,"method":"tools/list"}""") + extractJsonFromResponse(handler.handleJsonRpc(json, Seq.empty)).hcursor.downField("id").focus shouldBe Some( + Json.fromLong(1763561090000L) + ) + + private def discoverOverHttp(meta: Option[Json], versionHeader: Option[String], extraHeaders: (String, String)*): McpResponse = + val req: JSONRPCMessage = Request(method = "server/discover", params = meta.map(m => Json.obj("_meta" -> m)), id = RequestId("dv")) + val headers = (("Host" -> "localhost") +: ("Mcp-Method" -> "server/discover") +: extraHeaders) ++ + versionHeader.map(ProtocolMeta.ProtocolVersionHeader -> _) + handler.handleJsonRpc(req.asJson, headers.map(Header(_, _))) + + private def errorOf(response: McpResponse): JSONRPCErrorObject = + extractJsonFromResponse(response).as[JSONRPCMessage].getOrElse(fail("decode")) match + case Error(_, _, err) => err + case other => fail(s"Expected Error, got $other") + + private def discoverMeta(version: String = "2026-07-28"): Json = + Json.obj(ProtocolMeta.ProtocolVersionKey -> Json.fromString(version), ProtocolMeta.ClientCapabilities -> Json.obj()) + + it should "answer a modern server/discover whose _meta and header are valid" in: + val response = discoverOverHttp(Some(discoverMeta()), Some("2026-07-28")) + response.statusCode shouldBe sttp.model.StatusCode.Ok + extractJsonFromResponse(response).as[JSONRPCMessage].getOrElse(fail("decode")) match + case Response(_, _, result) => result.as[DiscoverResult].isRight shouldBe true + case other => fail(s"Expected Response, got $other") + + it should "answer a server/discover that declares no protocol version, or a legacy one" in: + discoverOverHttp(None, None).statusCode shouldBe sttp.model.StatusCode.Ok + discoverOverHttp(None, Some("2025-11-25")).statusCode shouldBe sttp.model.StatusCode.Ok + + it should "reject a modern server/discover with missing _meta keys with -32602 and HTTP 400" in: + val missingCapabilities = Json.obj(ProtocolMeta.ProtocolVersionKey -> Json.fromString("2026-07-28")) + val missingVersion = Json.obj(ProtocolMeta.ClientCapabilities -> Json.obj()) + for meta <- List(None, Some(missingCapabilities), Some(missingVersion)) do + val response = discoverOverHttp(meta, Some("2026-07-28")) + withClue(meta): + response.statusCode shouldBe sttp.model.StatusCode.BadRequest + errorOf(response).code shouldBe InvalidParams.code + + it should "reject a server/discover with an unsupported protocol version with -32022, the supported and the requested versions" in: + val response = discoverOverHttp(Some(discoverMeta("v999.0.0")), Some("v999.0.0")) + response.statusCode shouldBe sttp.model.StatusCode.BadRequest + val err = errorOf(response) + err.code shouldBe UnsupportedProtocolVersion.code + err.data.flatMap(_.hcursor.get[List[String]]("supported").toOption) shouldBe Some(ProtocolVersion.supported.map(_.name)) + err.data.flatMap(_.hcursor.get[String]("requested").toOption) shouldBe Some("v999.0.0") + + it should "reject a modern request whose MCP-Protocol-Version header differs from _meta with -32020, even for an unsupported _meta version" in: + val response = discoverOverHttp(Some(discoverMeta("v999.0.0")), Some("2026-07-28")) + response.statusCode shouldBe sttp.model.StatusCode.BadRequest + errorOf(response).code shouldBe HeaderMismatch.code + + it should "reject a modern HTTP request without the MCP-Protocol-Version header with -32020 and HTTP 400" in: + val response = discoverOverHttp(Some(discoverMeta()), None) + response.statusCode shouldBe sttp.model.StatusCode.BadRequest + errorOf(response).code shouldBe HeaderMismatch.code + + it should "reject a modern server/discover over HTTP with a mismatched Mcp-Method header with -32020" in: + val req: JSONRPCMessage = Request(method = "server/discover", params = Some(Json.obj("_meta" -> discoverMeta())), id = RequestId("dm")) + val headers = + Seq(Header("Host", "localhost"), Header(ProtocolMeta.ProtocolVersionHeader, "2026-07-28"), Header("Mcp-Method", "tools/list")) + errorOf(handler.handleJsonRpc(req.asJson, headers)).code shouldBe HeaderMismatch.code diff --git a/server/src/test/scala/chimp/server/McpHeaderToolSpec.scala b/server/src/test/scala/chimp/server/McpHeaderToolSpec.scala new file mode 100644 index 00000000..c3c33c32 --- /dev/null +++ b/server/src/test/scala/chimp/server/McpHeaderToolSpec.scala @@ -0,0 +1,114 @@ +package chimp.server + +import chimp.protocol.{JSONRPCMessage, RequestId} +import io.circe.parser.parse +import io.circe.syntax.* +import io.circe.{Codec, Json} +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.monad.{IdentityMonad, MonadError} +import sttp.shared.Identity +import sttp.tapir.Schema + +class McpHeaderToolSpec extends AnyFlatSpec with Matchers: + case class Inner(zone: String) derives Schema, Codec + case class Query(region: String, limit: Int, dryRun: Boolean, ratio: Double, tags: List[String], inner: Inner) derives Schema, Codec + + private def json(text: String): Json = parse(text).fold(throw _, identity) + + private def rawTool(properties: String, extra: String = "") = + tool("raw").inputJson(json(s"""{"type": "object", "properties": $properties $extra}""")) + + private given MonadError[Identity] = IdentityMonad + + private def inputSchema(tool: Tool[?, NoStructuredOutput]): Json = + val handler = McpHandler(McpServer(tools = List(tool.handle(_ => ToolResult.text("ok"))))) + val request: JSONRPCMessage = JSONRPCMessage.Request(method = "tools/list", id = RequestId("1")) + val tools = handler.handleJsonRpc(request.asJson, Nil).body.flatMap(_.hcursor.downField("result").downField("tools").focus) + tools.flatMap(_.hcursor.downArray.downField("inputSchema").focus).getOrElse(fail("no input schema")) + + "withMcpHeader" should "annotate a property of a schema derived from a case class" in: + val annotated = tool("derived").input[Query].withMcpHeader("region", "Region").withMcpHeader("limit", "Limit") + val schema = inputSchema(annotated) + schema.hcursor.downField("properties").downField("region").get[String]("x-mcp-header") shouldBe Right("Region") + schema.hcursor.downField("properties").downField("limit").get[String]("x-mcp-header") shouldBe Right("Limit") + McpHeaders.params(schema) should contain theSameElementsAs List(McpHeader(List("region"), "Region"), McpHeader(List("limit"), "Limit")) + + it should "annotate a nested property of a raw JSON schema" in: + val annotated = + rawTool("""{"location": {"type": "object", "properties": {"region": {"type": "string"}}}}""") + .withMcpHeader(List("location", "region"), "Region") + val schema = inputSchema(annotated) + schema.hcursor + .downField("properties") + .downField("location") + .downField("properties") + .downField("region") + .get[String]( + "x-mcp-header" + ) shouldBe Right("Region") + + it should "keep an annotation already present in a raw JSON schema" in: + val schema = inputSchema(rawTool("""{"region": {"type": ["string", "null"], "x-mcp-header": "Region"}}""")) + McpHeaders.params(schema) shouldBe List(McpHeader(List("region"), "Region")) + + it should "reject a header name which breaks the spec constraints" in: + val base = tool("derived").input[Query] + for name <- List("", "Bad Name", "Bad:Name", "Line\nBreak", "Ctrl\u0001", "Région") do + withClue(name): + an[IllegalArgumentException] should be thrownBy base.withMcpHeader("region", name) + + it should "reject a header name used twice in one tool, regardless of case" in: + an[IllegalArgumentException] should be thrownBy + tool("derived").input[Query].withMcpHeader("region", "Region").withMcpHeader("limit", "REGION") + + it should "reject a property whose type is not string, integer or boolean" in: + for property <- List("ratio", "tags", "inner") do + withClue(property): + an[IllegalArgumentException] should be thrownBy tool("derived").input[Query].withMcpHeader(property, "Header") + tool("derived").input[Query].withMcpHeader("dryRun", "Dry-Run").mcpHeaders shouldBe List(McpHeader(List("dryRun"), "Dry-Run")) + + it should "reject a property which does not exist or is not reachable through properties keys only" in: + an[IllegalArgumentException] should be thrownBy tool("derived").input[Query].withMcpHeader("missing", "Header") + an[IllegalArgumentException] should be thrownBy tool("derived").input[Query].withMcpHeader(List("inner", "zone"), "Zone") + an[IllegalArgumentException] should be thrownBy tool("derived").input[Query].withMcpHeader(Nil, "Header") + + "inputJson" should "reject an annotation which is not reachable through properties keys only" in: + an[IllegalArgumentException] should be thrownBy + rawTool("""{"tags": {"type": "array", "items": {"type": "string", "x-mcp-header": "Tag"}}}""") + an[IllegalArgumentException] should be thrownBy + rawTool("""{"a": {"type": "string"}}""", """, "anyOf": [{"properties": {"a": {"type": "string", "x-mcp-header": "A"}}}]""") + an[IllegalArgumentException] should be thrownBy + rawTool("""{"a": {"$ref": "#/$defs/A"}}""", """, "$defs": {"A": {"type": "string", "x-mcp-header": "A"}}""") + + it should "reject an annotation with an invalid value or on an invalid type" in: + an[IllegalArgumentException] should be thrownBy rawTool("""{"a": {"type": "string", "x-mcp-header": 1}}""") + an[IllegalArgumentException] should be thrownBy rawTool("""{"a": {"type": "number", "x-mcp-header": "A"}}""") + an[IllegalArgumentException] should be thrownBy rawTool("""{"a": {"x-mcp-header": "A"}}""") + an[IllegalArgumentException] should be thrownBy + rawTool("""{"a": {"type": "string", "x-mcp-header": "A"}, "b": {"type": "integer", "x-mcp-header": "a"}}""") + + it should "not treat a property named x-mcp-header or a default value as an annotation" in: + noException should be thrownBy rawTool("""{"x-mcp-header": {"type": "object", "default": {"x-mcp-header": 1}}}""") + + "McpHeaders.decode" should "decode the Base64 sentinel form and keep any other value literal" in: + McpHeaders.decode("=?base64?SGVsbG8=?=") shouldBe Right("Hello") + McpHeaders.decode("=?base64?SGVsbG8sIOS4lueVjA==?=") shouldBe Right("Hello, 世界") + McpHeaders.decode("=?base64??=") shouldBe Right("") + McpHeaders.decode("SGVsbG8=") shouldBe Right("SGVsbG8=") + McpHeaders.decode("=?base64?SGVsbG8=") shouldBe Right("=?base64?SGVsbG8=") + McpHeaders.decode("SGVsbG8=?=") shouldBe Right("SGVsbG8=?=") + + it should "reject a malformed sentinel or invalid characters" in: + for value <- List("=?base64?SGVsbG8?=", "=?base64?SGVs!!!bG8=?=", "=?base64?S===?=", "=?base64?/w==?=", "café", "a\u0000b") do + withClue(value)(McpHeaders.decode(value).isLeft shouldBe true) + + "McpHeaders.matches" should "compare numbers numerically and other values literally" in: + McpHeaders.matches("42.0", Json.fromInt(42)) shouldBe true + McpHeaders.matches("4.2e1", Json.fromInt(42)) shouldBe true + McpHeaders.matches("43", Json.fromInt(42)) shouldBe false + McpHeaders.matches("x", Json.fromInt(42)) shouldBe false + McpHeaders.matches("true", Json.True) shouldBe true + McpHeaders.matches("True", Json.True) shouldBe false + McpHeaders.matches("us-west1", Json.fromString("us-west1")) shouldBe true + McpHeaders.matches("US-WEST1", Json.fromString("us-west1")) shouldBe false diff --git a/server/src/test/scala/chimp/server/McpServerHeaderHttpTests.scala b/server/src/test/scala/chimp/server/McpServerHeaderHttpTests.scala new file mode 100644 index 00000000..33ea2995 --- /dev/null +++ b/server/src/test/scala/chimp/server/McpServerHeaderHttpTests.scala @@ -0,0 +1,98 @@ +package chimp.server + +import chimp.protocol.* +import io.circe.syntax.* +import io.circe.{parser, Codec, Json} +import org.scalatest.Assertion +import org.scalatest.flatspec.AsyncFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.tapir.Schema + +import java.net.URI +import java.net.http.{HttpClient, HttpRequest, HttpResponse} +import scala.concurrent.Future + +/** Standard and `x-mcp-header` custom request headers of 2026-07-28 requests, on the plain and the streaming HTTP transports. */ +trait McpServerHeaderHttpTests[F[_]] extends AsyncFlatSpec with Matchers: + this: ToFuture[F] => + + /** Serves `server` on a plain HTTP endpoint and runs the blocking `test` with the endpoint's URI. */ + protected def withHttpEndpoint(server: McpServer[F])(test: URI => Assertion): Future[Assertion] + + /** Serves `server` on a streaming HTTP endpoint and runs the blocking `test` with the endpoint's URI. */ + protected def withStreamingHttpEndpoint(server: StreamingMcpServer[F])(test: URI => Assertion): Future[Assertion] + + private case class RegionInput(region: String, limit: Option[Int]) derives Codec, Schema + + private val headerServer: McpServer[F] = + McpServer[F]().addTool( + tool("region") + .input[RegionInput] + .withMcpHeader("region", "Region") + .withMcpHeader("limit", "Limit") + .serverLogic[F]((in, _) => monad.unit(ToolResult.text(s"region ${in.region}"))) + ) + + private val httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build() + + private def post(uri: URI, method: String, params: Json, headers: (String, String)*): HttpResponse[String] = + val meta = Json.obj(ProtocolMeta.ProtocolVersionKey -> "2026-07-28".asJson, ProtocolMeta.ClientCapabilities -> Json.obj()) + val body = Json.obj( + "jsonrpc" -> "2.0".asJson, + "id" -> 1.asJson, + "method" -> method.asJson, + "params" -> params.deepMerge(Json.obj("_meta" -> meta)) + ) + val request = headers + .foldLeft(HttpRequest.newBuilder(uri))((builder, header) => builder.header(header._1, header._2)) + .header("Content-Type", "application/json") + .header("Accept", "application/json, text/event-stream") + .header(ProtocolMeta.ProtocolVersionHeader, "2026-07-28") + .header("Mcp-Method", method) + .POST(HttpRequest.BodyPublishers.ofString(body.noSpaces)) + .build() + httpClient.send(request, HttpResponse.BodyHandlers.ofString()) + + private def callRegion(uri: URI, arguments: Json, headers: (String, String)*): HttpResponse[String] = + post(uri, "tools/call", Json.obj("name" -> "region".asJson, "arguments" -> arguments), ("Mcp-Name" -> "region") +: headers*) + + private def shouldBeServed(response: HttpResponse[String], text: String): Assertion = + response.statusCode() shouldBe 200 + response.body() should include(text) + response.body() should not include "\"error\"" + + private def shouldBeHeaderMismatch(response: HttpResponse[String]): Assertion = + response.statusCode() shouldBe 400 + response.headers().firstValue("Content-Type").orElse("") should startWith("application/json") + parser.parse(response.body()).flatMap(_.hcursor.downField("error").get[Int]("code")) shouldBe Right( + JSONRPCErrorCodes.HeaderMismatch.code + ) + + private def headerTests(transport: String, serve: McpServer[F] => (URI => Assertion) => Future[Assertion]): Unit = + it should s"list the x-mcp-header annotations in the input schema ($transport)" in: + serve(headerServer): uri => + val response = post(uri, "tools/list", Json.obj()) + response.statusCode() shouldBe 200 + response.body() should include("\"x-mcp-header\":\"Region\"") + + it should s"serve a tools/call whose Mcp-Param headers match the arguments, decoding Base64 ($transport)" in: + serve(headerServer): uri => + val arguments = Json.obj("region" -> "Hello".asJson, "limit" -> 42.asJson) + shouldBeServed(callRegion(uri, arguments, "Mcp-Param-Region" -> "=?base64?SGVsbG8=?=", "mcp-param-limit" -> "42.0"), "region Hello") + + it should s"reject a tools/call with a missing, malformed or mismatched Mcp-Param header with -32020 and HTTP 400 ($transport)" in: + serve(headerServer): uri => + val arguments = Json.obj("region" -> "Hello".asJson) + shouldBeHeaderMismatch(callRegion(uri, arguments)) + shouldBeHeaderMismatch(callRegion(uri, arguments, "Mcp-Param-Region" -> "=?base64?SGVsbG8?=")) + shouldBeHeaderMismatch(callRegion(uri, arguments, "Mcp-Param-Region" -> "Bye")) + + it should s"reject a tools/call whose Base64 Mcp-Name does not match the body with -32020 and HTTP 400 ($transport)" in: + serve(headerServer): uri => + val params = Json.obj("name" -> "region".asJson, "arguments" -> Json.obj("region" -> "eu".asJson)) + shouldBeServed(post(uri, "tools/call", params, "Mcp-Name" -> "=?base64?cmVnaW9u?=", "Mcp-Param-Region" -> "eu"), "region eu") + shouldBeHeaderMismatch(post(uri, "tools/call", params, "Mcp-Name" -> "=?base64?b3RoZXI=?=", "Mcp-Param-Region" -> "eu")) + + behavior of "an HTTP server checking request headers" + headerTests("plain", server => withHttpEndpoint(server)) + headerTests("streaming", server => withStreamingHttpEndpoint(server.streaming)) diff --git a/server/src/test/scala/chimp/server/McpServerInputRequiredHttpTests.scala b/server/src/test/scala/chimp/server/McpServerInputRequiredHttpTests.scala new file mode 100644 index 00000000..a6456bfa --- /dev/null +++ b/server/src/test/scala/chimp/server/McpServerInputRequiredHttpTests.scala @@ -0,0 +1,99 @@ +package chimp.server + +import chimp.protocol.* +import io.circe.syntax.* +import io.circe.{parser, Codec, Json} +import org.scalatest.Assertion +import org.scalatest.flatspec.AsyncFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.monad.syntax.* +import sttp.tapir.Schema + +import java.net.URI +import java.net.http.{HttpClient, HttpRequest, HttpResponse} +import scala.concurrent.Future + +/** Input-required outcomes over Streamable HTTP, where an error with its own HTTP status must not hide in a 200 event stream. */ +trait McpServerInputRequiredHttpTests[F[_]] extends AsyncFlatSpec with Matchers: + this: ToFuture[F] => + + /** Serves `server` on a streaming HTTP endpoint and runs the blocking `test` with the endpoint's URI. */ + protected def withStreamingHttpEndpoint(server: StreamingMcpServer[F])(test: URI => Assertion): Future[Assertion] + + private case class NoInput() derives Codec, Schema + + private val sampling = InputRequired.sample( + CreateMessageParams(List(SamplingMessage(Role.User, ToolContent.Text(text = "Capital of France?"))), maxTokens = 10) + ) + + private val inputRequiredServer: StreamingMcpServer[F] = + StreamingMcpServer[F]() + .addTool( + tool("ask") + .input[NoInput] + .serverLogicWithContext[F]((_, ctx, _) => + monad.unit(ctx.request.samplingResult("q").fold(InputRequired("q" -> sampling))(_ => ToolResult.text("answered"))) + ) + ) + .addStreamingTool( + tool("progress_then_ask") + .input[NoInput] + .streamingServerLogic[F]((_, ctx, _) => ctx.reportProgress(50, Some(100)).map(_ => InputRequired("q" -> sampling))) + ) + + private val httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build() + + private def callTool(uri: URI, name: String, capabilities: Json): HttpResponse[String] = + val meta = Json.obj( + ProtocolMeta.ProtocolVersionKey -> "2026-07-28".asJson, + ProtocolMeta.ClientCapabilities -> capabilities, + "progressToken" -> "p".asJson + ) + val body = Json.obj( + "jsonrpc" -> "2.0".asJson, + "id" -> 1.asJson, + "method" -> "tools/call".asJson, + "params" -> Json.obj("name" -> name.asJson, "arguments" -> Json.obj(), "_meta" -> meta) + ) + val request = HttpRequest + .newBuilder(uri) + .header("Content-Type", "application/json") + .header("Accept", "application/json, text/event-stream") + .header(ProtocolMeta.ProtocolVersionHeader, "2026-07-28") + .header("Mcp-Method", "tools/call") + .header("Mcp-Name", name) + .POST(HttpRequest.BodyPublishers.ofString(body.noSpaces)) + .build() + httpClient.send(request, HttpResponse.BodyHandlers.ofString()) + + private def contentType(response: HttpResponse[String]): String = response.headers().firstValue("Content-Type").orElse("") + + private def events(response: HttpResponse[String]): List[Json] = + response.body().linesIterator.collect { case s"data:$data" => parser.parse(data.trim).toOption }.flatten.toList + + "a streaming HTTP server" should "send an input-required result on the event stream" in: + withStreamingHttpEndpoint(inputRequiredServer): uri => + val response = callTool(uri, "ask", Json.obj("sampling" -> Json.obj())) + response.statusCode() shouldBe 200 + contentType(response) should include("text/event-stream") + val result = events(response).flatMap(_.hcursor.downField("result").focus) + result.map(_.hcursor.get[String]("resultType")) shouldBe List(Right("input_required")) + result.map(_.hcursor.downField("inputRequests").downField("q").get[String]("method")) shouldBe List(Right("sampling/createMessage")) + + it should "answer an input request the client has no capability for with a plain JSON -32021 and HTTP 400" in: + withStreamingHttpEndpoint(inputRequiredServer): uri => + val response = callTool(uri, "ask", Json.obj()) + response.statusCode() shouldBe 400 + contentType(response) should include("application/json") + val error = parser.parse(response.body()).toOption.flatMap(_.hcursor.downField("error").focus) + error.flatMap(_.hcursor.get[Int]("code").toOption) shouldBe Some(JSONRPCErrorCodes.MissingRequiredClientCapability.code) + error.flatMap(_.hcursor.downField("data").downField("requiredCapabilities").focus) shouldBe Some(Json.obj("sampling" -> Json.obj())) + + it should "end an event stream which already carries a message with the -32021" in: + withStreamingHttpEndpoint(inputRequiredServer): uri => + val response = callTool(uri, "progress_then_ask", Json.obj()) + response.statusCode() shouldBe 200 + val messages = events(response) + messages.map(_.hcursor.get[String]("method").toOption) shouldBe List(Some("notifications/progress"), None) + messages.lastOption.flatMap(_.hcursor.downField("error").get[Int]("code").toOption) shouldBe + Some(JSONRPCErrorCodes.MissingRequiredClientCapability.code) diff --git a/server/src/test/scala/chimp/server/McpServerStreamingTests.scala b/server/src/test/scala/chimp/server/McpServerStreamingTests.scala index da568d64..5c60540a 100644 --- a/server/src/test/scala/chimp/server/McpServerStreamingTests.scala +++ b/server/src/test/scala/chimp/server/McpServerStreamingTests.scala @@ -19,6 +19,9 @@ trait McpServerStreamingTests[F[_]] extends AsyncFlatSpec with Matchers: protected def withStreamingServer(server: StreamingMcpServer[F])(test: BidirectionalMcpClient[F] => F[Assertion]): Future[Assertion] + /** The protocol version of the spec's client. A modern client sets no `_meta` logLevel, so it must get no log notifications. */ + protected def clientProtocolVersion: ProtocolVersion + private case class NoInput() derives Codec, Schema protected def streamingServer: StreamingMcpServer[F] = @@ -36,7 +39,7 @@ trait McpServerStreamingTests[F[_]] extends AsyncFlatSpec with Matchers: yield ToolResult.text("done") ) - "a streaming MCP server" should "deliver log notifications emitted during a tool call" in + "a streaming MCP server" should "deliver log notifications emitted during a tool call, but none to a modern request without a log level" in withStreamingServer(streamingServer): client => val messages = ConcurrentLinkedQueue[Json]() val listener: ServerNotification => F[Unit] = { @@ -47,6 +50,10 @@ trait McpServerStreamingTests[F[_]] extends AsyncFlatSpec with Matchers: .onServerNotification(notification => listener(notification)) .flatMap(_ => client.callTool("noisy", Json.obj())) .flatMap: result => - waitUntil(messages.size >= 3).map: _ => - result.content shouldBe List(ToolContent.Text("text", "done")) - messages.asScala.toList shouldBe List(Json.fromString("one"), Json.fromString("two"), Json.fromString("three")) + val expected = + if clientProtocolVersion.isModern then Nil else List(Json.fromString("one"), Json.fromString("two"), Json.fromString("three")) + waitUntil(messages.size >= expected.size) + .flatMap(_ => sleep(50)) + .map: _ => + result.content shouldBe List(ToolContent.Text("text", "done")) + messages.asScala.toList shouldBe expected diff --git a/server/src/test/scala/chimp/server/McpServerSubscriptionHttpTests.scala b/server/src/test/scala/chimp/server/McpServerSubscriptionHttpTests.scala new file mode 100644 index 00000000..dd801177 --- /dev/null +++ b/server/src/test/scala/chimp/server/McpServerSubscriptionHttpTests.scala @@ -0,0 +1,136 @@ +package chimp.server + +import chimp.protocol.ProtocolMeta +import io.circe.syntax.* +import io.circe.{parser, Codec, Json} +import org.scalatest.Assertion +import org.scalatest.flatspec.AsyncFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.tapir.Schema + +import java.net.URI +import java.net.http.{HttpClient, HttpRequest, HttpResponse} +import java.util.concurrent.{LinkedBlockingQueue, TimeUnit} +import scala.concurrent.Future +import scala.jdk.CollectionConverters.* +import scala.util.Try + +/** `subscriptions/listen` over Streamable HTTP. Uses a plain HTTP client, because chimp's client does not listen yet. */ +trait McpServerSubscriptionHttpTests[F[_]] extends AsyncFlatSpec with Matchers: + this: ToFuture[F] => + + /** Serves `server` on a streaming HTTP endpoint and runs the blocking `test` with the endpoint's URI. */ + protected def withStreamingHttpEndpoint(server: StreamingMcpServer[F])(test: URI => Assertion): Future[Assertion] + + private case class NoInput() derives Codec, Schema + + private def listeningServer(publisher: NotificationPublisher): StreamingMcpServer[F] = + StreamingMcpServer[F]() + .addTool(tool("noop").input[NoInput].serverLogic[F]((_, _) => monad.unit(ToolResult.text("ok")))) + .addResource(resource("test://a").name("a").serverLogic[F](_ => monad.unit(Right(Nil)))) + .withPublisher(publisher) + + private val httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build() + + private final class SseStream(response: HttpResponse[java.util.stream.Stream[String]]): + private val messages = LinkedBlockingQueue[Option[Json]]() + private val reader = Thread: () => + try + response + .body() + .iterator() + .asScala + .collect { case s"data:$data" => parser.parse(data.trim).toOption } + .flatten + .foreach(json => messages.put(Some(json))) + catch case _: Exception => () + finally messages.put(None) + reader.setDaemon(true) + reader.start() + + def status: Int = response.statusCode() + + /** The next message, or `None` when the stream ended. */ + def next(): Option[Json] = Option(messages.poll(10, TimeUnit.SECONDS)).getOrElse(fail("No message on the stream within 10 seconds")) + + def close(): Unit = Try(response.body().close()).getOrElse(()) + + private def listen(uri: URI, id: Json, filter: Json): SseStream = + val meta = Json.obj(ProtocolMeta.ProtocolVersionKey -> "2026-07-28".asJson, ProtocolMeta.ClientCapabilities -> Json.obj()) + val body = Json.obj( + "jsonrpc" -> "2.0".asJson, + "id" -> id, + "method" -> "subscriptions/listen".asJson, + "params" -> Json.obj("notifications" -> filter, "_meta" -> meta) + ) + val request = HttpRequest + .newBuilder(uri) + .header("Content-Type", "application/json") + .header("Accept", "application/json, text/event-stream") + .header(ProtocolMeta.ProtocolVersionHeader, "2026-07-28") + .header("Mcp-Method", "subscriptions/listen") + .POST(HttpRequest.BodyPublishers.ofString(body.noSpaces)) + .build() + SseStream(httpClient.send(request, HttpResponse.BodyHandlers.ofLines())) + + private def method(message: Option[Json]): Option[String] = message.flatMap(_.hcursor.get[String]("method").toOption) + + private def subscriptionId(message: Option[Json]): Option[Json] = + message.flatMap(_.hcursor.downField("params").downField("_meta").downField(ProtocolMeta.SubscriptionId).focus) + + private def eventually(condition: => Boolean): Boolean = + Iterator.continually { Thread.sleep(20); condition }.take(250).exists(identity) + + "a streaming HTTP server" should "acknowledge a subscription first and send it only the requested notifications" in: + val publisher = NotificationPublisher() + withStreamingHttpEndpoint(listeningServer(publisher)): uri => + val filter = Json.obj( + "toolsListChanged" -> true.asJson, + "promptsListChanged" -> true.asJson, + "resourceSubscriptions" -> List("test://a").asJson + ) + val stream = listen(uri, 11.asJson, filter) + try + stream.status shouldBe 200 + val ack = stream.next() + method(ack) shouldBe Some("notifications/subscriptions/acknowledged") + ack.flatMap(_.hcursor.downField("params").downField("notifications").focus) shouldBe + Some(Json.obj("resourceSubscriptions" -> List("test://a").asJson, "toolsListChanged" -> true.asJson)) + publisher.promptsListChanged() + publisher.resourcesListChanged() + publisher.resourceUpdated("test://b") + publisher.resourceUpdated("test://a") + publisher.toolsListChanged() + val updated = stream.next() + val toolsChanged = stream.next() + method(updated) shouldBe Some("notifications/resources/updated") + updated.flatMap(_.hcursor.downField("params").get[String]("uri").toOption) shouldBe Some("test://a") + method(toolsChanged) shouldBe Some("notifications/tools/list_changed") + List(ack, updated, toolsChanged).map(subscriptionId) shouldBe List.fill(3)(Some(11.asJson)) + finally stream.close() + + it should "stop publishing to a subscription when the client closes its stream" in: + val publisher = NotificationPublisher() + withStreamingHttpEndpoint(listeningServer(publisher)): uri => + val stream = listen(uri, 1.asJson, Json.obj("toolsListChanged" -> true.asJson)) + method(stream.next()) shouldBe Some("notifications/subscriptions/acknowledged") + publisher.openSubscriptions shouldBe 1 + stream.close() + eventually { publisher.toolsListChanged(); publisher.openSubscriptions == 0 } shouldBe true + + it should "end every subscription with a successful listen result when the publisher closes" in: + val publisher = NotificationPublisher() + withStreamingHttpEndpoint(listeningServer(publisher)): uri => + val first = listen(uri, 1.asJson, Json.obj("toolsListChanged" -> true.asJson)) + val second = listen(uri, "second".asJson, Json.obj("resourcesListChanged" -> true.asJson)) + try + List(first.next(), second.next()).map(subscriptionId) shouldBe List(Some(1.asJson), Some("second".asJson)) + publisher.close() + val results = List(first.next(), second.next()) + results.map(_.flatMap(_.hcursor.downField("id").focus)) shouldBe List(Some(1.asJson), Some("second".asJson)) + results.map(_.flatMap(_.hcursor.downField("result").downField("_meta").downField(ProtocolMeta.SubscriptionId).focus)) shouldBe + List(Some(1.asJson), Some("second".asJson)) + List(first.next(), second.next()) shouldBe List(None, None) + finally + first.close() + second.close() diff --git a/server/src/test/scala/chimp/server/NotificationPublisherSpec.scala b/server/src/test/scala/chimp/server/NotificationPublisherSpec.scala new file mode 100644 index 00000000..cc52be8d --- /dev/null +++ b/server/src/test/scala/chimp/server/NotificationPublisherSpec.scala @@ -0,0 +1,206 @@ +package chimp.server + +import chimp.protocol.* +import io.circe.Json +import io.circe.syntax.* +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.model.StatusCode +import sttp.monad.{IdentityMonad, MonadError} +import sttp.shared.Identity +import sttp.tapir.Schema + +import java.util.concurrent.ConcurrentLinkedQueue +import java.util.concurrent.atomic.AtomicReference +import scala.collection.mutable.ListBuffer +import scala.jdk.CollectionConverters.* + +class NotificationPublisherSpec extends AnyFlatSpec with Matchers: + private given MonadError[Identity] = IdentityMonad + + private case class NoInput() derives io.circe.Codec, Schema + + private val noop = tool("noop").input[NoInput].handle(_ => ToolResult.text("ok")) + private val textResource = resource("test://a").name("a").handle(() => Right(Nil)) + private val greet = prompt("greet").handle(_ => GetPromptResult(messages = Nil)) + + private def fullServer(publisher: NotificationPublisher): StreamingMcpServer[Identity] = + McpServer[Identity]() + .addTool(noop) + .addResource(textResource) + .addPrompt(greet) + .withSubscriptions(ResourceSubscriptions[Identity](_ => (), _ => ())) + .withPublisher(publisher) + .streaming + + private val modernMeta: Json = + Json.obj(ProtocolMeta.ProtocolVersionKey -> "2026-07-28".asJson, ProtocolMeta.ClientCapabilities -> Json.obj()) + + private def request(method: String, id: RequestId, params: Json): Json = + (JSONRPCMessage.Request(method = method, params = Some(params), id = id): JSONRPCMessage).asJson + + private def listen(id: RequestId, filter: Json): Json = + request("subscriptions/listen", id, Json.obj("notifications" -> filter, "_meta" -> modernMeta)) + + private def capabilities(handler: McpHandler[Identity, ?], method: String): ServerCapabilities = + val params = if method == "initialize" then Json.obj("protocolVersion" -> "2025-11-25".asJson) else Json.obj() + handler + .handleJsonRpc(request(method, RequestId(1), params), Nil, _ => ???) + .body + .get + .hcursor + .downField("result") + .get[ServerCapabilities]("capabilities") + .toOption + .get + + private class RecordingOutput extends SubscriptionOutput: + val messages: ListBuffer[Json] = ListBuffer.empty + var closed = false + def send(message: Json): Unit = messages += message + def close(): Unit = closed = true + def methods: List[Option[String]] = messages.toList.map(_.hcursor.get[String]("method").toOption) + + private def subscriptionId(message: Json): Option[Json] = + message.hcursor.downField("params").downField("_meta").downField(ProtocolMeta.SubscriptionId).focus + + "a server with a publisher on a streaming transport" should "advertise list changes and subscribe to modern clients only" in: + val handler = McpHandler(fullServer(NotificationPublisher()), canListen = true) + val modern = capabilities(handler, "server/discover") + modern.tools.flatMap(_.listChanged) shouldBe Some(true) + modern.prompts.flatMap(_.listChanged) shouldBe Some(true) + modern.resources.flatMap(_.listChanged) shouldBe Some(true) + modern.resources.flatMap(_.subscribe) shouldBe Some(true) + val legacy = capabilities(handler, "initialize") + legacy.tools.flatMap(_.listChanged) shouldBe Some(false) + legacy.resources.flatMap(_.subscribe) shouldBe Some(true) + + it should "acknowledge only the notification types it can send" in: + val handler = McpHandler(McpServer[Identity]().addTool(noop).withPublisher(NotificationPublisher()).streaming, canListen = true) + val filter = + Json.obj("toolsListChanged" -> true.asJson, "promptsListChanged" -> true.asJson, "resourceSubscriptions" -> List("x").asJson) + val accepted = handler.listenRequest(listen(RequestId(7), filter), Nil) + accepted.map(_.id) shouldBe Some(RequestId(7)) + accepted.map(_.filter) shouldBe Some(SubscriptionFilter(toolsListChanged = Some(true))) + + it should "not take a legacy subscriptions/listen request as a subscription" in: + val handler = McpHandler(fullServer(NotificationPublisher()), canListen = true) + val legacyListen = request("subscriptions/listen", RequestId(1), Json.obj("notifications" -> Json.obj())) + handler.listenRequest(legacyListen, Nil) shouldBe None + + "a server with a publisher on the sync HTTP transport" should "advertise no list changes and answer subscriptions/listen with -32601" in: + val handler = McpHandler(fullServer(NotificationPublisher())) + val modern = capabilities(handler, "server/discover") + modern.tools.flatMap(_.listChanged) shouldBe Some(false) + modern.resources.flatMap(_.subscribe) shouldBe Some(false) + val listenRequest = listen(RequestId(1), Json.obj("toolsListChanged" -> true.asJson)) + handler.listenRequest(listenRequest, Nil) shouldBe None + val response = handler.handleJsonRpc(listenRequest, Nil, _ => ???) + response.statusCode shouldBe StatusCode.NotFound + response.body.flatMap(_.hcursor.downField("error").get[Int]("code").toOption) shouldBe Some(JSONRPCErrorCodes.MethodNotFound.code) + + "a subscription" should "start with the acknowledgement and carry the subscription id on every message" in: + val publisher = NotificationPublisher() + val output = RecordingOutput() + val _ = publisher.open(RequestId(3), SubscriptionFilter(toolsListChanged = Some(true)), output) + publisher.toolsListChanged() + output.methods shouldBe List(Some("notifications/subscriptions/acknowledged"), Some("notifications/tools/list_changed")) + output.messages.toList.map(subscriptionId) shouldBe List.fill(2)(Some(3.asJson)) + output.messages.head.hcursor.downField("params").get[SubscriptionFilter]("notifications") shouldBe + Right(SubscriptionFilter(toolsListChanged = Some(true))) + + it should "deliver an event which the client publishes as soon as it sees the acknowledgement" in: + val publisher = NotificationPublisher() + val received = ConcurrentLinkedQueue[Json]() + val client = AtomicReference(Option.empty[Thread]) + val output = new SubscriptionOutput: + def send(message: Json): Unit = + val _ = received.add(message) + if client.get.isEmpty then + val reacting = Thread(() => publisher.toolsListChanged()) + client.set(Some(reacting)) + reacting.start() + while reacting.isAlive && reacting.getState != Thread.State.BLOCKED do Thread.onSpinWait() + def close(): Unit = () + val _ = publisher.open(RequestId(1), SubscriptionFilter(toolsListChanged = Some(true)), output) + client.get.foreach(_.join(5000)) + received.asScala.toList.map(_.hcursor.get[String]("method").toOption) shouldBe + List(Some("notifications/subscriptions/acknowledged"), Some("notifications/tools/list_changed")) + + it should "send only the notification types and resource URIs the filter names" in: + val publisher = NotificationPublisher() + val output = RecordingOutput() + val _ = publisher.open(RequestId("s"), SubscriptionFilter(resourceSubscriptions = Some(List("test://a"))), output) + publisher.toolsListChanged() + publisher.promptsListChanged() + publisher.resourcesListChanged() + publisher.resourceUpdated("test://b") + publisher.resourceUpdated("test://a") + output.methods.drop(1) shouldBe List(Some("notifications/resources/updated")) + output.messages.last.hcursor.downField("params").get[String]("uri") shouldBe Right("test://a") + + it should "keep several subscriptions apart" in: + val publisher = NotificationPublisher() + val tools = RecordingOutput() + val prompts = RecordingOutput() + val _ = publisher.open(RequestId(1), SubscriptionFilter(toolsListChanged = Some(true)), tools) + val _ = publisher.open(RequestId(2), SubscriptionFilter(promptsListChanged = Some(true), toolsListChanged = Some(true)), prompts) + publisher.promptsListChanged() + publisher.toolsListChanged() + tools.methods.drop(1) shouldBe List(Some("notifications/tools/list_changed")) + tools.messages.toList.map(subscriptionId).distinct shouldBe List(Some(1.asJson)) + prompts.methods.drop(1) shouldBe List(Some("notifications/prompts/list_changed"), Some("notifications/tools/list_changed")) + prompts.messages.toList.map(subscriptionId).distinct shouldBe List(Some(2.asJson)) + + it should "get nothing more after the client cancels it" in: + val publisher = NotificationPublisher() + val output = RecordingOutput() + val subscription = publisher.open(RequestId(1), SubscriptionFilter(toolsListChanged = Some(true)), output) + subscription.cancel() + publisher.toolsListChanged() + publisher.close() + output.methods shouldBe List(Some("notifications/subscriptions/acknowledged")) + output.closed shouldBe false + publisher.openSubscriptions shouldBe 0 + + it should "end with a successful listen result when the publisher closes" in: + val publisher = NotificationPublisher() + val output = RecordingOutput() + val _ = publisher.open(RequestId(5), SubscriptionFilter(toolsListChanged = Some(true)), output) + publisher.close() + publisher.toolsListChanged() + val result = output.messages.last + result.hcursor.get[Int]("id") shouldBe Right(5) + result.hcursor.downField("result").get[String]("resultType") shouldBe Right("complete") + result.hcursor.downField("result").downField("_meta").downField(ProtocolMeta.SubscriptionId).focus shouldBe Some(5.asJson) + output.messages.size shouldBe 2 + output.closed shouldBe true + publisher.openSubscriptions shouldBe 0 + + it should "end at once when it opens after the publisher closed" in: + val publisher = NotificationPublisher() + publisher.close() + val output = RecordingOutput() + val _ = publisher.open(RequestId(1), SubscriptionFilter(), output) + output.messages.size shouldBe 2 + output.closed shouldBe true + + "stdio subscriptions" should "end on the client's notifications/cancelled, and send the result and notifications/cancelled at shutdown" in: + val publisher = NotificationPublisher() + val written = ListBuffer.empty[Json] + val stdio = StdioSubscriptions(McpHandler(McpServer[Identity]()), written += _) + val filter = SubscriptionFilter(toolsListChanged = Some(true)) + stdio.listen(ListenRequest(RequestId(1), filter, publisher)) + stdio.listen(ListenRequest(RequestId(2), filter, publisher)) + stdio.cancel( + (JSONRPCMessage + .Notification(method = "notifications/cancelled", params = Some(Json.obj("requestId" -> 1.asJson))): JSONRPCMessage).asJson + ) + publisher.toolsListChanged() + stdio.endAll() + val tail = written.toList.drop(2) + tail.map(subscriptionId) shouldBe List(Some(2.asJson), None, None) + tail(1).hcursor.get[Int]("id") shouldBe Right(2) + tail(2).hcursor.get[String]("method") shouldBe Right("notifications/cancelled") + tail(2).hcursor.downField("params").get[Int]("requestId") shouldBe Right(2) + publisher.openSubscriptions shouldBe 0 diff --git a/server/src/test/scala/chimp/server/SecuredMcpServerStreamingTests.scala b/server/src/test/scala/chimp/server/SecuredMcpServerStreamingTests.scala index 750f7202..17080f71 100644 --- a/server/src/test/scala/chimp/server/SecuredMcpServerStreamingTests.scala +++ b/server/src/test/scala/chimp/server/SecuredMcpServerStreamingTests.scala @@ -23,6 +23,9 @@ trait SecuredMcpServerTestFixtures[F[_]]: trait SecuredMcpServerStreamingTests[F[_]] extends AsyncFlatSpec with Matchers with RecoverMethods with SecuredMcpServerTestFixtures[F]: this: ToFuture[F] => + /** The protocol version of the spec's client. A modern client sets no `_meta` logLevel, so it must get no log notifications. */ + protected def clientProtocolVersion: ProtocolVersion + /** @param token * The bearer token the client authenticates with; defaults to [[validToken]] so most tests need not pass it. */ @@ -83,11 +86,14 @@ trait SecuredMcpServerStreamingTests[F[_]] extends AsyncFlatSpec with Matchers w .onServerNotification(notification => listener(notification)) .flatMap(_ => client.callTool("whoAmI", Json.obj())) .flatMap: result => - waitUntil(messages.size >= 1).map: _ => - result.content shouldBe List(ToolContent.Text("text", "employee@example.com")) - messages.asScala.toList shouldBe List(Json.fromString("employee@example.com")) - - "a secured streaming MCP server" should "give the principal to a streaming tool, and deliver its log notifications" in + val expected = if clientProtocolVersion.isModern then Nil else List(Json.fromString("employee@example.com")) + waitUntil(messages.size >= expected.size) + .flatMap(_ => sleep(50)) + .map: _ => + result.content shouldBe List(ToolContent.Text("text", "employee@example.com")) + messages.asScala.toList shouldBe expected + + "a secured streaming MCP server" should "give the principal to a streaming tool, and deliver its log notifications (none to a modern request without a log level)" in withSecuredStreamingServer(securedStreamingServer)(assertWhoAmIDeliversPrincipal) it should "give the principal to a streaming tool when the security logic is effectful" in diff --git a/server/src/test/scala/chimp/server/ServerStdioTransportTests.scala b/server/src/test/scala/chimp/server/ServerStdioTransportTests.scala index 34321d8c..59884fa1 100644 --- a/server/src/test/scala/chimp/server/ServerStdioTransportTests.scala +++ b/server/src/test/scala/chimp/server/ServerStdioTransportTests.scala @@ -1,6 +1,6 @@ package chimp.server -import chimp.protocol.{JSONRPCErrorCodes, LoggingLevel} +import chimp.protocol.{JSONRPCErrorCodes, LoggingLevel, ProtocolMeta} import chimp.transport.{McpLineTooLongException, StdioFraming} import io.circe.{parser, Codec, Json} import org.scalatest.flatspec.AnyFlatSpec @@ -11,6 +11,7 @@ import sttp.tapir.Schema import java.io.{ BufferedReader, BufferedWriter, + IOException, InputStream, InputStreamReader, OutputStream, @@ -19,6 +20,7 @@ import java.io.{ PipedOutputStream } import java.nio.charset.StandardCharsets +import java.util.concurrent.{LinkedBlockingQueue, TimeUnit} import scala.concurrent.duration.DurationInt import scala.concurrent.{Await, Future} @@ -30,10 +32,18 @@ trait ServerStdioTransportTests[F[_]] extends AnyFlatSpec with Matchers: private case class EchoInput(message: String) derives Codec, Schema private case class NoInput() derives Codec, Schema - private def server: StreamingMcpServer[F] = + private def server(publisher: NotificationPublisher): StreamingMcpServer[F] = StreamingMcpServer[F]() .withLoggingLevel(_ => monad.unit(())) + .withPublisher(publisher) .addTool(tool("echo").input[EchoInput].serverLogic[F]((in, _) => monad.unit(ToolResult.text(in.message)))) + .addTool( + tool("ask") + .input[NoInput] + .serverLogicWithContext[F]((_, ctx, _) => + monad.unit(ctx.request.rootsResult("roots").fold(InputRequired("roots" -> InputRequired.listRoots))(_ => ToolResult.text("ok"))) + ) + ) .addStreamingTool( tool("noisy") .input[NoInput] @@ -46,7 +56,12 @@ trait ServerStdioTransportTests[F[_]] extends AnyFlatSpec with Matchers: } ) - protected class StdioSession(toServer: PipedOutputStream, fromServer: PipedInputStream, val served: Future[Unit]): + protected class StdioSession( + toServer: PipedOutputStream, + fromServer: PipedInputStream, + val served: Future[Unit], + val publisher: NotificationPublisher + ): private val writer = BufferedWriter(OutputStreamWriter(toServer, StandardCharsets.UTF_8)) private val reader = BufferedReader(InputStreamReader(fromServer, StandardCharsets.UTF_8)) @@ -61,7 +76,20 @@ trait ServerStdioTransportTests[F[_]] extends AnyFlatSpec with Matchers: def closeInput(): Unit = writer.close() - def readResponse(): Json = parser.parse(reader.readLine()).toOption.get + private val lines = LinkedBlockingQueue[Option[String]]() + private val readerThread = Thread: () => + try Iterator.continually(reader.readLine()).takeWhile(_ != null).foreach(line => lines.put(Some(line))) + catch case _: IOException => () + finally lines.put(None) + readerThread.setDaemon(true) + readerThread.start() + + /** The next line from the server; fails instead of blocking when no line comes within 10 seconds. */ + def readResponse(): Json = + Option(lines.poll(10, TimeUnit.SECONDS)) match + case Some(Some(line)) => parser.parse(line).toOption.get + case Some(None) => fail("The server closed its output") + case None => fail("No line from the server within 10 seconds") private def withStdioServer[A](maxLineLength: Int = StdioFraming.defaultMaxLineLength)(body: StdioSession => A): A = val toServer = PipedOutputStream() @@ -69,7 +97,8 @@ trait ServerStdioTransportTests[F[_]] extends AnyFlatSpec with Matchers: val fromServer = PipedInputStream() val serverOut = PipedOutputStream(fromServer) - val session = StdioSession(toServer, fromServer, runStdioServer(server, serverIn, serverOut, maxLineLength)) + val publisher = NotificationPublisher() + val session = StdioSession(toServer, fromServer, runStdioServer(server(publisher), serverIn, serverOut, maxLineLength), publisher) try body(session) finally session.closeInput() @@ -125,3 +154,71 @@ trait ServerStdioTransportTests[F[_]] extends AnyFlatSpec with Matchers: val failure = intercept[McpLineTooLongException](Await.result(session.served, 10.seconds)) failure.maxLineLength shouldBe 1024 } + + private val modernMeta = """{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}""" + + private def listen(id: Int, filter: String): String = + s"""{"jsonrpc":"2.0","id":$id,"method":"subscriptions/listen","params":{"notifications":$filter,"_meta":$modernMeta}}""" + + private def subscriptionId(message: Json): Option[Int] = + message.hcursor.downField("params").downField("_meta").get[Int](ProtocolMeta.SubscriptionId).toOption + + private def method(message: Json): Option[String] = message.hcursor.get[String]("method").toOption + + private def echo(session: StdioSession, id: Int): Json = + session.send(s"""{"jsonrpc":"2.0","id":$id,"method":"tools/call","params":{"name":"echo","arguments":{"message":"m"}}}""") + session.readResponse() + + it should "keep several subscriptions on one channel apart, and end one on notifications/cancelled" in withStdioServer() { session => + session.send(listen(21, """{"toolsListChanged":true}""")) + val first = session.readResponse() + session.send(listen(22, """{"toolsListChanged":true,"promptsListChanged":true}""")) + val second = session.readResponse() + List(first, second).map(method) shouldBe List.fill(2)(Some("notifications/subscriptions/acknowledged")) + List(first, second).map(subscriptionId) shouldBe List(Some(21), Some(22)) + second.hcursor.downField("params").downField("notifications").focus shouldBe parser.parse("""{"toolsListChanged":true}""").toOption + + session.publisher.toolsListChanged() + val changes = List(session.readResponse(), session.readResponse()) + changes.map(method) shouldBe List.fill(2)(Some("notifications/tools/list_changed")) + changes.flatMap(subscriptionId).sorted shouldBe List(21, 22) + + session.send("""{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":21}}""") + echo(session, 23).hcursor.get[Int]("id") shouldBe Right(23) + session.publisher.toolsListChanged() + subscriptionId(session.readResponse()) shouldBe Some(22) + echo(session, 24).hcursor.get[Int]("id") shouldBe Right(24) + } + + it should "end open subscriptions with a listen result and notifications/cancelled when its input ends" in withStdioServer() { session => + session.send(listen(31, """{"toolsListChanged":true}""")) + method(session.readResponse()) shouldBe Some("notifications/subscriptions/acknowledged") + session.closeInput() + + val result = session.readResponse() + result.hcursor.get[Int]("id") shouldBe Right(31) + result.hcursor.downField("result").get[String]("resultType") shouldBe Right("complete") + result.hcursor.downField("result").downField("_meta").get[Int](ProtocolMeta.SubscriptionId) shouldBe Right(31) + val cancelled = session.readResponse() + method(cancelled) shouldBe Some("notifications/cancelled") + cancelled.hcursor.downField("params").get[Int]("requestId") shouldBe Right(31) + + Await.result(session.served, 10.seconds) + session.publisher.openSubscriptions shouldBe 0 + } + + private def ask(id: Int, capabilities: String, extra: String = ""): String = + val meta = s"""{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":$capabilities}""" + s"""{"jsonrpc":"2.0","id":$id,"method":"tools/call","params":{"name":"ask","arguments":{}$extra,"_meta":$meta}}""" + + it should "answer an input-required tool call, its retry, and an input request the client has no capability for" in withStdioServer() { + session => + session.send(ask(41, """{"roots":{}}""")) + session.readResponse().hcursor.downField("result").get[String]("resultType") shouldBe Right("input_required") + session.send(ask(42, """{"roots":{}}""", ""","inputResponses":{"roots":{"roots":[]}}""")) + session.readResponse().hcursor.downField("result").downField("content").downN(0).get[String]("text") shouldBe Right("ok") + session.send(ask(43, "{}")) + val error = session.readResponse() + error.hcursor.get[Int]("id") shouldBe Right(43) + error.hcursor.downField("error").get[Int]("code") shouldBe Right(JSONRPCErrorCodes.MissingRequiredClientCapability.code) + } diff --git a/server/src/test/scala/chimp/server/SyncHttpMcpServerSpec.scala b/server/src/test/scala/chimp/server/SyncHttpMcpServerSpec.scala index 8238a81e..255aa239 100644 --- a/server/src/test/scala/chimp/server/SyncHttpMcpServerSpec.scala +++ b/server/src/test/scala/chimp/server/SyncHttpMcpServerSpec.scala @@ -2,11 +2,15 @@ package chimp.server import chimp.client.McpClient import chimp.client.transport.ClientHttpTransport -import chimp.protocol.Implementation +import chimp.protocol.{Implementation, ProtocolMeta} +import io.circe.syntax.* +import io.circe.{parser, Codec, Json} import org.scalatest.Assertion import ox.supervised import sttp.client4.* +import sttp.model.StatusCode import sttp.shared.Identity +import sttp.tapir.Schema import sttp.tapir.server.netty.sync.NettySyncServer import scala.concurrent.Future @@ -26,3 +30,29 @@ class SyncHttpMcpServerSpec extends McpServerTests[Identity] with SyncToFuture: finally transport.close() finally backend.close() finally binding.stop() + + private case class NoInput() derives Codec, Schema + + "Sync HTTP transport" should "answer an input request the client has no capability for with HTTP 400 and -32021" in: + val server = McpServer[Identity]().addTool(tool("ask").input[NoInput].handle(_ => InputRequired("roots" -> InputRequired.listRoots))) + toFuture: + supervised: + val binding = NettySyncServer().port(0).addEndpoint(server.endpoint(List("mcp"))).start() + try + val backend = DefaultSyncBackend() + try + val meta = Json.obj(ProtocolMeta.ProtocolVersionKey -> "2026-07-28".asJson, ProtocolMeta.ClientCapabilities -> Json.obj()) + val params = Json.obj("name" -> "ask".asJson, "arguments" -> Json.obj(), "_meta" -> meta) + val body = Json.obj("jsonrpc" -> "2.0".asJson, "id" -> 1.asJson, "method" -> "tools/call".asJson, "params" -> params) + val response = basicRequest + .post(uri"http://localhost:${binding.port}/mcp") + .headers(Map("Content-Type" -> "application/json", "MCP-Protocol-Version" -> "2026-07-28", "Mcp-Method" -> "tools/call")) + .header("Mcp-Name", "ask") + .body(body.noSpaces) + .send(backend) + response.code shouldBe StatusCode.BadRequest + parser.parse(response.body.fold(identity, identity)).flatMap(_.hcursor.downField("error").get[Int]("code")) shouldBe Right( + -32021 + ) + finally backend.close() + finally binding.stop()