diff --git a/conformance-baseline-2026-07-28.yml b/conformance-baseline-2026-07-28.yml index 70e5f7b..aabc6c8 100644 --- a/conformance-baseline-2026-07-28.yml +++ b/conformance-baseline-2026-07-28.yml @@ -23,7 +23,8 @@ server: - input-required-result-request-state - input-required-result-result-type - input-required-result-tampered-state - # full stateless lifecycle (needs subscription serving) + # full stateless lifecycle: only http-server-no-independent-requests-on-stream fails, it needs the + # test_streaming_elicitation tool (multi-round-trip requests) — T14, https://github.com/softwaremill/chimp/issues/252 - server-stateless # x-mcp-header custom headers in the tool DSL — T13, https://github.com/softwaremill/chimp/issues/251 - http-custom-header-server-validation diff --git a/docs/server/capabilities.md b/docs/server/capabilities.md index dececaa..3da351f 100644 --- a/docs/server/capabilities.md +++ b/docs/server/capabilities.md @@ -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 @@ -45,6 +45,7 @@ A request is **modern** when its `_meta` or its `MCP-Protocol-Version` header de | 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). @@ -82,3 +83,49 @@ val ask = tool("ask") 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/resources.md b/docs/server/resources.md index 9333a77..6a6239c 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.* 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 f287803..2a74a83 100644 --- a/server-conformance/src/main/scala/chimp/conformance/server/Main.scala +++ b/server-conformance/src/main/scala/chimp/conformance/server/Main.scala @@ -145,6 +145,22 @@ object Main: 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") @@ -206,7 +222,9 @@ object Main: mixedTool, embeddedResourceTool, jsonSchemaTool, - missingCapabilityTool + missingCapabilityTool, + triggerToolChange, + triggerPromptChange ) .addResources(staticText, staticBinary) .addResourceTemplate(dataTemplate) @@ -214,6 +232,7 @@ object Main: .withCompletion((_, _, _) => Completion(values = List("alpha", "beta"))) .withLoggingLevel(_ => ()) .withSubscriptions(ResourceSubscriptions[Identity](_ => (), _ => ())) + .withPublisher(publisher) private val streamingServer = server.streaming.addStreamingTools(progressTool, loggingTool, perRequestLoggingTool) @@ -225,6 +244,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 c997361..b6d4823 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,7 +1,7 @@ package chimp.server.ox import chimp.protocol.JSONRPCMessage -import chimp.server.OutboundSink +import chimp.server.{ListenRequest, OutboundSink, SubscriptionOutput} import chimp.server.transport.ServerStreamingHttpTransport import io.circe.Json import io.circe.syntax.* @@ -32,3 +32,14 @@ final class OxServerHttpTransport(path: List[String]) extends ServerStreamingHtt try handle(sink).foreach(outbound.send) finally outbound.done() outbound.foreach(json => emit(ServerSentEvent(data = Some(json.noSpaces)))) + + private[server] def subscriptionStream(listen: ListenRequest): Flow[ServerSentEvent] = + Flow.usingEmit: emit => + 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 1492d75..5142983 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 @@ -2,7 +2,15 @@ package chimp.server.ox import chimp.protocol.JSONRPCMessage import chimp.server.transport.ServerStreamingStdioTransport -import chimp.server.{McpHandler, OutboundSink, RequestInfo, 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)) @@ -42,12 +50,17 @@ final class OxServerStdioTransport( 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(outbound.send) - case Left(error) => log.warn(s"Failed to parse JSON-RPC line: ${error.getMessage}; raw: $line") + 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 317c942..5a6fc7e 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 @@ -9,6 +9,7 @@ import chimp.server.{ tool, McpServer, McpServerStreamingTests, + McpServerSubscriptionHttpTests, McpServerTests, SecuredMcpServer, SecuredMcpServerStreamingTests, @@ -31,11 +32,13 @@ 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 SecuredMcpServerTests[Identity] with SecuredMcpServerStreamingTests[Identity] with SyncToFuture: @@ -55,6 +58,10 @@ class OxMcpServerHttpSpec 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 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 99f42d2..b1df7a5 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,7 +1,7 @@ package chimp.server.pekko import chimp.protocol.JSONRPCMessage -import chimp.server.OutboundSink +import chimp.server.{ListenRequest, OutboundSink, SubscriptionOutput} import chimp.server.transport.ServerStreamingHttpTransport import io.circe.Json import io.circe.syntax.* @@ -57,3 +57,16 @@ final class PekkoServerHttpTransport( queue.complete() NotUsed .map(json => ServerSentEvent(data = Some(json.noSpaces))) + + 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))) 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 d181bf8..734bb92 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 @@ -2,7 +2,15 @@ package chimp.server.pekko import chimp.protocol.JSONRPCMessage import chimp.server.transport.ServerStreamingStdioTransport -import chimp.server.{McpHandler, OutboundSink, RequestInfo, 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) @@ -42,30 +50,36 @@ final class PekkoServerStdioTransport( val makeContext: RequestInfo => StreamingServerContext[Future] = info => SinkStreamingServerContext(sink, info) + val subscriptions = StdioSubscriptions(handler, json => { val _ = PekkoOutbound.offer(outbound, json) }) + val reading = StreamConverters .fromInputStream(() => in) .via(Framing.delimiter(ByteString("\n"), maximumFrameLength = maxLineLength, allowTruncation = true)) .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]], + 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 e80782b..d5066cc 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 @@ -7,6 +7,7 @@ import chimp.server.transport.SecuredServerStreamingHttpTransport import chimp.server.{ McpServer, McpServerStreamingTests, + McpServerSubscriptionHttpTests, McpServerTests, SecuredMcpServer, SecuredMcpServerStreamingTests, @@ -24,12 +25,14 @@ 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 SecuredMcpServerTests[Future] with SecuredMcpServerStreamingTests[Future] with PekkoToFuture: @@ -45,6 +48,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 9b2c4d6..43b68f5 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,7 +1,7 @@ package chimp.server.zio import chimp.protocol.JSONRPCMessage -import chimp.server.OutboundSink +import chimp.server.{ListenRequest, OutboundSink, SubscriptionOutput} import chimp.server.transport.ServerStreamingHttpTransport import io.circe.Json import io.circe.syntax.* @@ -10,7 +10,7 @@ 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 @@ -42,6 +42,22 @@ final class ZioServerHttpTransport(path: List[String]) extends ServerStreamingHt } } + 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 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 0c7cbef..12aadfd 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 -import chimp.server.{McpHandler, OutboundSink, RequestInfo, SinkStreamingServerContext, StreamingMcpServer, StreamingServerContext} +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,16 +31,20 @@ 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) @@ -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 251b88c..9a2eb2e 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 @@ -8,6 +8,7 @@ import chimp.server.transport.SecuredServerStreamingHttpTransport import chimp.server.{ McpServer, McpServerStreamingTests, + McpServerSubscriptionHttpTests, McpServerTests, SecuredMcpServer, SecuredMcpServerStreamingTests, @@ -26,11 +27,13 @@ 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 SecuredMcpServerTests[Task] with SecuredMcpServerStreamingTests[Task] with ZioToFuture: @@ -46,6 +49,14 @@ class ZioMcpServerHttpSpec withHttpServer(ZioServerHttpTransport(List("mcp")).serve(server), Nil): transport => 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 diff --git a/server/src/main/scala/chimp/server/McpHandler.scala b/server/src/main/scala/chimp/server/McpHandler.scala index 04a5902..dc41ec2 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 @@ -141,6 +146,8 @@ private[server] class McpHandler[F[_], C <: ServerContext[F]](server: McpServerD 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 @@ -167,7 +174,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") @@ -321,18 +328,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, @@ -344,7 +380,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 ) diff --git a/server/src/main/scala/chimp/server/McpServer.scala b/server/src/main/scala/chimp/server/McpServer.scala index c994f3d..71e753f 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 0000000..9b18d23 --- /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/transport/JsonRpcHttpResponses.scala b/server/src/main/scala/chimp/server/transport/JsonRpcHttpResponses.scala index 929f3ad..8211250 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,21 @@ 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` inside a 200 event stream. */ private[transport] def respondWithEventStream[F[_], Caps]( originCheck: OriginCheck, headers: Seq[Header], backend: ServerStreamingHttpTransport[F, Caps], - rejection: => Option[McpResponse] + rejection: => Option[McpResponse], + listen: => Option[ListenRequest] )(handle: OutboundSink[F] => F[Option[Json]])(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(events => Right((StatusCode.Ok, events))) diff --git a/server/src/main/scala/chimp/server/transport/SecuredServerStreamingHttpTransport.scala b/server/src/main/scala/chimp/server/transport/SecuredServerStreamingHttpTransport.scala index 16d0df5..5291100 100644 --- a/server/src/main/scala/chimp/server/transport/SecuredServerStreamingHttpTransport.scala +++ b/server/src/main/scala/chimp/server/transport/SecuredServerStreamingHttpTransport.scala @@ -27,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)) @@ -44,7 +44,13 @@ 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 => + 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(_.body) diff --git a/server/src/main/scala/chimp/server/transport/ServerStdioTransport.scala b/server/src/main/scala/chimp/server/transport/ServerStdioTransport.scala index cd4da85..87176c5 100644 --- a/server/src/main/scala/chimp/server/transport/ServerStdioTransport.scala +++ b/server/src/main/scala/chimp/server/transport/ServerStdioTransport.scala @@ -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)) @@ -52,9 +52,14 @@ final class ServerStdioTransport( 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 cadf642..a50ff11 100644 --- a/server/src/main/scala/chimp/server/transport/ServerStreamingHttpTransport.scala +++ b/server/src/main/scala/chimp/server/transport/ServerStreamingHttpTransport.scala @@ -28,8 +28,13 @@ abstract class ServerStreamingHttpTransport[F[_], Caps](path: List[String]) exte def emptyStream: EventStream def eventStream(handle: OutboundSink[F] => F[Option[Json]]): F[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)) @@ -43,7 +48,13 @@ 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 => + 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).map(_.body) 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 0000000..dd80117 --- /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 0000000..cc52be8 --- /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/ServerStdioTransportTests.scala b/server/src/test/scala/chimp/server/ServerStdioTransportTests.scala index 34321d8..40fc325 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,9 +32,10 @@ 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)))) .addStreamingTool( tool("noisy") @@ -46,7 +49,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 +69,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 +90,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 +147,55 @@ 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 + }