Skip to content
Myr

mcp.ts

This file

Path

packages/protocol/test/mcp.ts

107 lines · 3.0 KiB · TypeScript

Last changed by

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–107why, one step back
1import { Effect, Layer, Schema } from "effect";
2import { McpProtocol, McpServer } from "effect/ai";3import { HttpRouter } from "effect/http";4import type { Rpc, RpcGroup } from "effect/rpc";5import { RpcTest } from "effect/rpc";67import { Client, Contracts, surfaced } from "../src/contracts.ts";8import { toToolkit } from "../src/mcp.ts";9import { asTester } from "../src/testing.ts";1011const endpoint = "http://localhost/mcp";1213const protocol = McpProtocol.v2026_07_28;1415// The 2026-07-28 protocol is stateless: each request names its version and16// client in `_meta` and repeats its method and target in headers.17const meta = {18 "io.modelcontextprotocol/clientCapabilities": {},19 "io.modelcontextprotocol/clientInfo": { name: "test", version: "0.0.0" },20 "io.modelcontextprotocol/protocolVersion": protocol.protocolVersion,21};2223const lastMessage = (response: Response) =>24 Effect.promise(() => response.text()).pipe(25 Effect.map((text) =>26 response.headers.get("content-type")?.includes("text/event-stream") ===27 true28 ? (text29 .split("\n")30 .findLast((line) => line.startsWith("data: "))31 ?.slice("data: ".length) ?? "")32 : text33 )34 );3536const Reply = Schema.fromJsonString(Schema.Struct({ result: Schema.Unknown }));3738interface Params {39 readonly arguments?: Readonly<Record<string, string>>;40 readonly name?: string;41}4243export const serve = Effect.fnUntraced(function* (44 handlers: Layer.Layer<Rpc.ToHandler<RpcGroup.Rpcs<typeof Contracts>>>45) {46 const projection = toToolkit(surfaced("mcp"), Effect.service(Client));4748 const app = McpServer.toolkit(projection.toolkit).pipe(49 Layer.provideMerge(projection.layer),50 Layer.provide(Layer.effect(Client)(RpcTest.makeClient(Contracts))),51 Layer.provide([handlers, asTester]),52 Layer.provide(53 McpServer.layerHttp({54 name: "test",55 path: "/mcp",56 protocols: [protocol],57 version: "0.0.0",58 })59 )60 );6162 const { dispose, handler } = HttpRouter.toWebHandler(app, {63 disableLogger: true,64 });6566 yield* Effect.addFinalizer(() => Effect.promise(dispose));6768 const call = Effect.fnUntraced(function* <A>(69 method: string,70 params: Params,71 result: Schema.Decoder<A>72 ) {73 const headers = new Headers({74 accept: "application/json, text/event-stream",75 "content-type": "application/json",76 "mcp-method": method,77 "mcp-protocol-version": protocol.protocolVersion,78 });7980 if (params.name !== undefined) {81 headers.set("mcp-name", params.name);82 }8384 const response = yield* Effect.promise(() =>85 handler(86 new Request(endpoint, {87 body: JSON.stringify({88 id: 1,89 jsonrpc: "2.0",90 method,91 params: { ...params, _meta: meta },92 }),93 headers,94 method: "POST",95 })96 )97 );9899 const reply = yield* Schema.decodeEffect(Reply)(100 yield* lastMessage(response)101 );102103 return yield* Schema.decodeUnknownEffect(result)(reply.result);104 });105106 return { call };107});