MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / serveMcpServer

Function serveMcpServer

packages/plugins/mcp/src/testing/server.ts:92–334  ·  view source on GitHub ↗
(factory: () => McpServer, options: McpTestServerOptions = {})

Source from the content-addressed store, hash-verified

90const protectedResourcePath = "/.well-known/oauth-protected-resource";
91
92export const serveMcpServer = (factory: () => McpServer, options: McpTestServerOptions = {}) =>
93 Effect.acquireRelease(
94 Effect.gen(function* () {
95 const transports = new Map<string, StreamableHTTPServerTransport>();
96 const allTransports = new Set<StreamableHTTPServerTransport>();
97 const requests = yield* Ref.make<readonly McpTestRequest[]>([]);
98 const path = options.path ?? "/";
99 let sessions = 0;
100 let nextSessionRequestStatus: number | undefined;
101 let sessionMethodRejection: { readonly method: string; readonly status: number } | undefined;
102
103 const writeUnauthorized = (response: http.ServerResponse, origin: string) =>
104 writeJson(
105 response,
106 401,
107 { error: "invalid_token" },
108 {
109 "www-authenticate":
110 options.auth?.wwwAuthenticate ??
111 `Bearer resource_metadata="${origin}${protectedResourcePath}${path}", error="invalid_token"`,
112 },
113 );
114
115 const namesJsonRpcMethod = (parsedBody: unknown, method: string): boolean => {
116 const messages = Array.isArray(parsedBody) ? parsedBody : [parsedBody];
117 return messages.some(
118 (message) =>
119 typeof message === "object" &&
120 message !== null &&
121 (message as { readonly method?: unknown }).method === method,
122 );
123 };
124
125 const handleMcpRequest = (
126 request: http.IncomingMessage,
127 response: http.ServerResponse,
128 ): Effect.Effect<void> =>
129 Effect.gen(function* () {
130 const requestUrl = request.url ?? "/";
131 const sessionId = Array.isArray(request.headers["mcp-session-id"])
132 ? request.headers["mcp-session-id"][0]
133 : request.headers["mcp-session-id"];
134 const authorization = Array.isArray(request.headers.authorization)
135 ? request.headers.authorization[0]
136 : request.headers.authorization;
137 const origin = request.headers.host
138 ? `http://${request.headers.host}`
139 : "http://127.0.0.1";
140
141 yield* Ref.update(requests, (all) => [
142 ...all,
143 {
144 method: request.method ?? "GET",
145 url: requestUrl,
146 authorization,
147 sessionId,
148 },
149 ]);

Calls 10

handleMcpRequestFunction · 0.85
resumeFunction · 0.85
listenMethod · 0.80
addressMethod · 0.80
toStringMethod · 0.80
clearMethod · 0.80
getMethod · 0.65
setMethod · 0.65
syncMethod · 0.65
closeMethod · 0.65

Tested by 1

serveTokenGatedMcpServerFunction · 0.72