diff --git a/conformance-baseline-2026-07-28.yml b/conformance-baseline-2026-07-28.yml index 70e5f7bc..717f710b 100644 --- a/conformance-baseline-2026-07-28.yml +++ b/conformance-baseline-2026-07-28.yml @@ -12,25 +12,13 @@ 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 + # an integrity-protected requestState — T14 request-state helper, https://github.com/softwaremill/chimp/issues/252 - 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..ec6df093 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,97 @@ 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` | | 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). + +## 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..e22187fd --- /dev/null +++ b/docs/server/mrtr.md @@ -0,0 +1,70 @@ +# 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, and reject a state that you cannot verify. +``` + +### 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. 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..e1e69e5e 100644 --- a/docs/server/tools.md +++ b/docs/server/tools.md @@ -53,3 +53,5 @@ 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). 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..8a7ab7e6 100644 --- a/server-conformance/src/main/scala/chimp/conformance/server/Main.scala +++ b/server-conformance/src/main/scala/chimp/conformance/server/Main.scala @@ -132,6 +132,120 @@ 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 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 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 +293,47 @@ 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, + triggerToolChange, + triggerPromptChange + ) .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) - 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 +343,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..4c52d7a1 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,9 @@ import chimp.server.transport.SecuredServerStreamingHttpTransport import chimp.server.{ tool, McpServer, + McpServerInputRequiredHttpTests, McpServerStreamingTests, + McpServerSubscriptionHttpTests, McpServerTests, SecuredMcpServer, SecuredMcpServerStreamingTests, @@ -31,15 +33,19 @@ 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 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 +56,14 @@ 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 withSecuredServer( server: SecuredMcpServer[Identity, String, String, User], token: String @@ -77,11 +87,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..9ecf4080 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,13 @@ 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, + McpServerInputRequiredHttpTests, McpServerStreamingTests, + McpServerSubscriptionHttpTests, McpServerTests, SecuredMcpServer, SecuredMcpServerStreamingTests, @@ -24,16 +26,20 @@ 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 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 +50,15 @@ 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] = + given ExecutionContext = actorSystem.dispatcher + Http() + .newServerAt("localhost", 0) + .bind(PekkoHttpServerInterpreter().toRoute(PekkoServerHttpTransport(List("mcp")).serve(server))) + .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..b35b8460 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,9 @@ import chimp.protocol.{Implementation, ProtocolVersion} import chimp.server.transport.SecuredServerStreamingHttpTransport import chimp.server.{ McpServer, + McpServerInputRequiredHttpTests, McpServerStreamingTests, + McpServerSubscriptionHttpTests, McpServerTests, SecuredMcpServer, SecuredMcpServerStreamingTests, @@ -26,15 +28,19 @@ 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 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 +49,22 @@ 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] = + toFuture: + val routes = ZioHttpInterpreter().toHttp(ZioServerHttpTransport(List("mcp")).serve(server)) + (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 +72,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 +88,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..639ac43d --- /dev/null +++ b/server/src/main/scala/chimp/server/InputRequired.scala @@ -0,0 +1,66 @@ +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)) + +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..2e1ebbd1 100644 --- a/server/src/main/scala/chimp/server/McpHandler.scala +++ b/server/src/main/scala/chimp/server/McpHandler.scala @@ -30,8 +30,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 @@ -63,15 +68,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 +85,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, () => makeContext(info), isModern) case Right(notification: JSONRPCMessage.Notification) => logger.debug(s"Received notification: ${notification.method}") McpResponse.EmptyAcceptResponse.unit @@ -93,13 +101,15 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD params: Option[Json], id: RequestId, headers: Seq[Header], - makeContext: Option[ProgressToken] => C, + makeContext: () => 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 +121,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, 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 +133,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, 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 +143,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, 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 +167,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 +206,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 +215,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 +249,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 +277,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,10 +296,45 @@ 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") @@ -257,18 +357,47 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD 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 +409,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 +418,37 @@ 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 + private def handleToolsCall(params: Option[Json], id: RequestId, headers: Seq[Header], makeContext: () => C, isModern: Boolean)(using MonadError[F] - ): F[JSONRPCMessage] = + ): 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) + val context = makeContext() tool .logic(input, context, headers) - .map: result => - toolCallResponse(id, result) + .map: + case outcome: InputRequired => inputRequiredReply(id, 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 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 +468,33 @@ 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) + makeContext: () => C, + isModern: Boolean + )(using MonadError[F]): F[Reply] = + decodeReplyParams[ReadResourceParams](params, id): params => + val context = makeContext() + val respond = resourceReadReply(id, params.uri, context.request, isModern) resourcesByUri.get(params.uri) match - case Some(resource) => resource.read(context, headers).map(resourceReadResponse(id, params.uri)) + 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(resourceReadResponse(id, params.uri)) + case Some(result) => result.map(respond) case None => - protocolError( - id, - JSONRPCErrorCodes.ResourceNotFound.code, - s"Resource not found: ${params.uri}", - Some(Json.obj("uri" -> Json.fromString(params.uri))) - ).unit + 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 +503,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)( + outcome: Either[ResourceError, List[ResourceContents]] | InputRequired + ): Reply = + outcome match + case required: InputRequired => inputRequiredReply(id, 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 +530,19 @@ 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 => + makeContext: () => C, + isModern: Boolean + )(using MonadError[F]): F[Reply] = + decodeReplyParams[GetPromptParams](params, id): params => promptsByName.get(params.name) match case Some(prompt) => + val context = makeContext() 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 + .logic(params.arguments.getOrElse(Map.empty), context, headers) + .map: + case outcome: InputRequired => inputRequiredReply(id, 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/McpServer.scala b/server/src/main/scala/chimp/server/McpServer.scala index c994f3d6..71e753fe 100644 --- a/server/src/main/scala/chimp/server/McpServer.scala +++ b/server/src/main/scala/chimp/server/McpServer.scala @@ -31,6 +31,7 @@ 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] case class McpServer[F[_]]( name: String = "Chimp MCP server", @@ -44,7 +45,8 @@ 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 ) extends McpServerDef[F, ServerContext[F]]: def name(value: String): McpServer[F] = copy(name = value) @@ -94,6 +96,12 @@ 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)) + 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 +131,8 @@ case class McpServer[F[_]]( resourceTemplates, completion, loggingLevel, - subscriptions + subscriptions, + publisher ) case class StreamingMcpServer[F[_]]( @@ -138,7 +147,8 @@ 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 ) extends McpServerDef[F, StreamingServerContext[F]]: def name(value: String): StreamingMcpServer[F] = copy(name = value) @@ -194,6 +204,12 @@ 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)) + /** 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. @@ -223,6 +239,7 @@ 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 tools: List[ServerTool[?, ?, F, SecuredServerContext[F, P]]] = mergeBy(server.tools, securedTools)(_.name) @@ -288,6 +305,12 @@ 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)) + 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 +333,7 @@ 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 securityInput: EndpointInput[S] = server.securityInput def errorOutput: EndpointOutput[E] = server.errorOutput @@ -375,3 +399,9 @@ 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)) 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/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..870f6d51 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,102 @@ 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) + 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) + 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 ): /** 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) + /** 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 +179,6 @@ 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 ) 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/McpHandlerSpec.scala b/server/src/test/scala/chimp/server/McpHandlerSpec.scala index 5770649d..e2b51d03 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 = @@ -697,3 +697,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/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()