fastmcp 1.24.0 → 1.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -7
- package/dist/FastMCP.d.ts +26 -1
- package/dist/FastMCP.js +81 -9
- package/dist/FastMCP.js.map +1 -1
- package/jsr.json +1 -1
- package/package.json +2 -2
- package/src/FastMCP.test.ts +96 -3
- package/src/FastMCP.ts +122 -12
- package/src/examples/addition.ts +66 -6
package/README.md
CHANGED
|
@@ -16,12 +16,13 @@ A TypeScript framework for building [MCP](https://glama.ai/mcp) servers capable
|
|
|
16
16
|
- [Logging](#logging)
|
|
17
17
|
- [Error handling](#errors)
|
|
18
18
|
- [SSE](#sse)
|
|
19
|
+
- [HTTP Streaming](#http-streaming)
|
|
19
20
|
- CORS (enabled by default)
|
|
20
21
|
- [Progress notifications](#progress)
|
|
21
22
|
- [Typed server events](#typed-server-events)
|
|
22
23
|
- [Prompt argument auto-completion](#prompt-argument-auto-completion)
|
|
23
24
|
- [Sampling](#requestsampling)
|
|
24
|
-
-
|
|
25
|
+
- [Configurable ping behavior](#configurable-ping-behavior)
|
|
25
26
|
- Roots
|
|
26
27
|
- CLI for [testing](#test-with-mcp-cli) and [debugging](#inspect-with-mcp-inspector)
|
|
27
28
|
|
|
@@ -80,11 +81,15 @@ npx fastmcp dev src/examples/addition.ts
|
|
|
80
81
|
npx fastmcp inspect src/examples/addition.ts
|
|
81
82
|
```
|
|
82
83
|
|
|
83
|
-
###
|
|
84
|
+
### Remote Server Options
|
|
84
85
|
|
|
85
|
-
|
|
86
|
+
FastMCP supports multiple transport options for remote communication, allowing an MCP hosted on a remote machine to be accessed over the network.
|
|
86
87
|
|
|
87
|
-
|
|
88
|
+
#### SSE
|
|
89
|
+
|
|
90
|
+
[Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) (SSE) provide a mechanism for servers to send real-time updates to clients over an HTTPS connection.
|
|
91
|
+
|
|
92
|
+
You can run the server with SSE support:
|
|
88
93
|
|
|
89
94
|
```ts
|
|
90
95
|
server.start({
|
|
@@ -98,7 +103,27 @@ server.start({
|
|
|
98
103
|
|
|
99
104
|
This will start the server and listen for SSE connections on `http://localhost:8080/sse`.
|
|
100
105
|
|
|
101
|
-
|
|
106
|
+
#### HTTP Streaming
|
|
107
|
+
|
|
108
|
+
[HTTP streaming](https://www.cloudflare.com/learning/video/what-is-http-live-streaming/) provides a more efficient alternative to SSE in environments that support it, with potentially better performance for larger payloads.
|
|
109
|
+
|
|
110
|
+
You can run the server with HTTP streaming support:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
server.start({
|
|
114
|
+
transportType: "httpStream",
|
|
115
|
+
httpStream: {
|
|
116
|
+
endpoint: "/stream",
|
|
117
|
+
port: 8080,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
This will start the server and listen for HTTP streaming connections on `http://localhost:8080/stream`.
|
|
123
|
+
|
|
124
|
+
You can connect to these servers using the appropriate client transport.
|
|
125
|
+
|
|
126
|
+
For SSE connections:
|
|
102
127
|
|
|
103
128
|
```ts
|
|
104
129
|
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
@@ -118,6 +143,26 @@ const transport = new SSEClientTransport(new URL(`http://localhost:8080/sse`));
|
|
|
118
143
|
await client.connect(transport);
|
|
119
144
|
```
|
|
120
145
|
|
|
146
|
+
For HTTP streaming connections:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
150
|
+
|
|
151
|
+
const client = new Client(
|
|
152
|
+
{
|
|
153
|
+
name: "example-client",
|
|
154
|
+
version: "1.0.0",
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
capabilities: {},
|
|
158
|
+
},
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const transport = new StreamableHTTPClientTransport(new URL(`http://localhost:8080/stream`));
|
|
162
|
+
|
|
163
|
+
await client.connect(transport);
|
|
164
|
+
```
|
|
165
|
+
|
|
121
166
|
## Core Concepts
|
|
122
167
|
|
|
123
168
|
### Tools
|
|
@@ -309,7 +354,32 @@ server.addTool({
|
|
|
309
354
|
});
|
|
310
355
|
```
|
|
311
356
|
|
|
312
|
-
####
|
|
357
|
+
#### Configurable Ping Behavior
|
|
358
|
+
|
|
359
|
+
FastMCP includes a configurable ping mechanism to maintain connection health. The ping behavior can be customized through server options:
|
|
360
|
+
|
|
361
|
+
```ts
|
|
362
|
+
const server = new FastMCP({
|
|
363
|
+
name: "My Server",
|
|
364
|
+
version: "1.0.0",
|
|
365
|
+
ping: {
|
|
366
|
+
// Explicitly enable or disable pings (defaults vary by transport)
|
|
367
|
+
enabled: true,
|
|
368
|
+
// Configure ping interval in milliseconds (default: 5000ms)
|
|
369
|
+
intervalMs: 10000,
|
|
370
|
+
// Set log level for ping-related messages (default: 'debug')
|
|
371
|
+
logLevel: 'debug'
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
By default, ping behavior is optimized for each transport type:
|
|
377
|
+
- Enabled for SSE and HTTP streaming connections (which benefit from keep-alive)
|
|
378
|
+
- Disabled for `stdio` connections (where pings are typically unnecessary)
|
|
379
|
+
|
|
380
|
+
This configurable approach helps reduce log verbosity and optimize performance for different usage scenarios.
|
|
381
|
+
|
|
382
|
+
### Returning an audio
|
|
313
383
|
|
|
314
384
|
Use the `audioContent` to create a content object for an audio:
|
|
315
385
|
|
|
@@ -745,7 +815,7 @@ import { AuthError } from "fastmcp";
|
|
|
745
815
|
const server = new FastMCP({
|
|
746
816
|
name: "My Server",
|
|
747
817
|
version: "1.0.0",
|
|
748
|
-
authenticate: (
|
|
818
|
+
authenticate: (request) => {
|
|
749
819
|
const apiKey = request.headers["x-api-key"];
|
|
750
820
|
|
|
751
821
|
if (apiKey !== "123") {
|
package/dist/FastMCP.d.ts
CHANGED
|
@@ -184,6 +184,24 @@ type ServerOptions<T extends FastMCPSessionAuth> = {
|
|
|
184
184
|
authenticate?: Authenticate<T>;
|
|
185
185
|
instructions?: string;
|
|
186
186
|
name: string;
|
|
187
|
+
ping?: {
|
|
188
|
+
/**
|
|
189
|
+
* Whether ping should be enabled by default.
|
|
190
|
+
* - true for SSE or HTTP Stream
|
|
191
|
+
* - false for stdio
|
|
192
|
+
*/
|
|
193
|
+
enabled?: boolean;
|
|
194
|
+
/**
|
|
195
|
+
* Interval
|
|
196
|
+
* @default 5000 (5s)
|
|
197
|
+
*/
|
|
198
|
+
intervalMs?: number;
|
|
199
|
+
/**
|
|
200
|
+
* Logging level for ping-related messages.
|
|
201
|
+
* @default 'debug'
|
|
202
|
+
*/
|
|
203
|
+
logLevel?: LoggingLevel;
|
|
204
|
+
};
|
|
187
205
|
version: `${number}.${number}.${number}`;
|
|
188
206
|
};
|
|
189
207
|
type Tool<T extends FastMCPSessionAuth, Params extends ToolParameters = ToolParameters> = {
|
|
@@ -244,10 +262,11 @@ declare class FastMCPSession<T extends FastMCPSessionAuth = FastMCPSessionAuth>
|
|
|
244
262
|
get loggingLevel(): LoggingLevel;
|
|
245
263
|
get roots(): Root[];
|
|
246
264
|
get server(): Server;
|
|
247
|
-
constructor({ auth, instructions, name, prompts, resources, resourcesTemplates, tools, version, }: {
|
|
265
|
+
constructor({ auth, instructions, name, ping, prompts, resources, resourcesTemplates, tools, version, }: {
|
|
248
266
|
auth?: T;
|
|
249
267
|
instructions?: string;
|
|
250
268
|
name: string;
|
|
269
|
+
ping?: ServerOptions<T>["ping"];
|
|
251
270
|
prompts: Prompt[];
|
|
252
271
|
resources: Resource[];
|
|
253
272
|
resourcesTemplates: InputResourceTemplate[];
|
|
@@ -300,6 +319,12 @@ declare class FastMCP<T extends Record<string, unknown> | undefined = undefined>
|
|
|
300
319
|
* Starts the server.
|
|
301
320
|
*/
|
|
302
321
|
start(options?: {
|
|
322
|
+
httpStream: {
|
|
323
|
+
endpoint: `/${string}`;
|
|
324
|
+
port: number;
|
|
325
|
+
};
|
|
326
|
+
transportType: "httpStream";
|
|
327
|
+
} | {
|
|
303
328
|
sse: {
|
|
304
329
|
endpoint: `/${string}`;
|
|
305
330
|
port: number;
|
package/dist/FastMCP.js
CHANGED
|
@@ -19,7 +19,7 @@ import { EventEmitter } from "events";
|
|
|
19
19
|
import { fileTypeFromBuffer } from "file-type";
|
|
20
20
|
import { readFile } from "fs/promises";
|
|
21
21
|
import Fuse from "fuse.js";
|
|
22
|
-
import { startSSEServer } from "mcp-proxy";
|
|
22
|
+
import { startHTTPStreamServer, startSSEServer } from "mcp-proxy";
|
|
23
23
|
import { setTimeout as delay } from "timers/promises";
|
|
24
24
|
import { fetch } from "undici";
|
|
25
25
|
import parseURITemplate from "uri-templates";
|
|
@@ -205,6 +205,7 @@ var FastMCPSession = class extends FastMCPSessionEventEmitter {
|
|
|
205
205
|
#capabilities = {};
|
|
206
206
|
#clientCapabilities;
|
|
207
207
|
#loggingLevel = "info";
|
|
208
|
+
#pingConfig;
|
|
208
209
|
#pingInterval = null;
|
|
209
210
|
#prompts = [];
|
|
210
211
|
#resources = [];
|
|
@@ -215,6 +216,7 @@ var FastMCPSession = class extends FastMCPSessionEventEmitter {
|
|
|
215
216
|
auth,
|
|
216
217
|
instructions,
|
|
217
218
|
name,
|
|
219
|
+
ping,
|
|
218
220
|
prompts,
|
|
219
221
|
resources,
|
|
220
222
|
resourcesTemplates,
|
|
@@ -223,6 +225,7 @@ var FastMCPSession = class extends FastMCPSessionEventEmitter {
|
|
|
223
225
|
}) {
|
|
224
226
|
super();
|
|
225
227
|
this.#auth = auth;
|
|
228
|
+
this.#pingConfig = ping;
|
|
226
229
|
if (tools.length) {
|
|
227
230
|
this.#capabilities.tools = {};
|
|
228
231
|
}
|
|
@@ -303,18 +306,46 @@ ${e instanceof Error ? e.stack : JSON.stringify(e)}`
|
|
|
303
306
|
}
|
|
304
307
|
}
|
|
305
308
|
if (this.#clientCapabilities) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
309
|
+
const pingConfig = this.#getPingConfig(transport);
|
|
310
|
+
if (pingConfig.enabled) {
|
|
311
|
+
this.#pingInterval = setInterval(async () => {
|
|
312
|
+
try {
|
|
313
|
+
await this.#server.ping();
|
|
314
|
+
} catch {
|
|
315
|
+
const logLevel = pingConfig.logLevel;
|
|
316
|
+
if (logLevel === "debug") {
|
|
317
|
+
console.debug("[FastMCP debug] server ping failed");
|
|
318
|
+
} else if (logLevel === "warning") {
|
|
319
|
+
console.warn(
|
|
320
|
+
"[FastMCP warning] server is not responding to ping"
|
|
321
|
+
);
|
|
322
|
+
} else if (logLevel === "error") {
|
|
323
|
+
console.error("[FastMCP error] server is not responding to ping");
|
|
324
|
+
} else {
|
|
325
|
+
console.info("[FastMCP info] server ping failed");
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}, pingConfig.intervalMs);
|
|
329
|
+
}
|
|
313
330
|
}
|
|
314
331
|
}
|
|
315
332
|
async requestSampling(message) {
|
|
316
333
|
return this.#server.createMessage(message);
|
|
317
334
|
}
|
|
335
|
+
#getPingConfig(transport) {
|
|
336
|
+
const pingConfig = this.#pingConfig || {};
|
|
337
|
+
let defaultEnabled = false;
|
|
338
|
+
if ("type" in transport) {
|
|
339
|
+
if (transport.type === "sse" || transport.type === "httpStream") {
|
|
340
|
+
defaultEnabled = true;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return {
|
|
344
|
+
enabled: pingConfig.enabled !== void 0 ? pingConfig.enabled : defaultEnabled,
|
|
345
|
+
intervalMs: pingConfig.intervalMs || 5e3,
|
|
346
|
+
logLevel: pingConfig.logLevel || "debug"
|
|
347
|
+
};
|
|
348
|
+
}
|
|
318
349
|
addPrompt(inputPrompt) {
|
|
319
350
|
const completers = {};
|
|
320
351
|
const enums = {};
|
|
@@ -757,6 +788,7 @@ var FastMCP = class extends FastMCPEventEmitter {
|
|
|
757
788
|
return this.#sessions;
|
|
758
789
|
}
|
|
759
790
|
#authenticate;
|
|
791
|
+
#httpStreamServer = null;
|
|
760
792
|
#options;
|
|
761
793
|
#prompts = [];
|
|
762
794
|
#resources = [];
|
|
@@ -799,6 +831,7 @@ var FastMCP = class extends FastMCPEventEmitter {
|
|
|
799
831
|
const session = new FastMCPSession({
|
|
800
832
|
instructions: this.#options.instructions,
|
|
801
833
|
name: this.#options.name,
|
|
834
|
+
ping: this.#options.ping,
|
|
802
835
|
prompts: this.#prompts,
|
|
803
836
|
resources: this.#resources,
|
|
804
837
|
resourcesTemplates: this.#resourcesTemplates,
|
|
@@ -820,6 +853,7 @@ var FastMCP = class extends FastMCPEventEmitter {
|
|
|
820
853
|
return new FastMCPSession({
|
|
821
854
|
auth,
|
|
822
855
|
name: this.#options.name,
|
|
856
|
+
ping: this.#options.ping,
|
|
823
857
|
prompts: this.#prompts,
|
|
824
858
|
resources: this.#resources,
|
|
825
859
|
resourcesTemplates: this.#resourcesTemplates,
|
|
@@ -844,6 +878,41 @@ var FastMCP = class extends FastMCPEventEmitter {
|
|
|
844
878
|
console.info(
|
|
845
879
|
`[FastMCP info] server is running on SSE at http://localhost:${options.sse.port}${options.sse.endpoint}`
|
|
846
880
|
);
|
|
881
|
+
} else if (options.transportType === "httpStream") {
|
|
882
|
+
this.#httpStreamServer = await startHTTPStreamServer({
|
|
883
|
+
createServer: async (request) => {
|
|
884
|
+
let auth;
|
|
885
|
+
if (this.#authenticate) {
|
|
886
|
+
auth = await this.#authenticate(request);
|
|
887
|
+
}
|
|
888
|
+
return new FastMCPSession({
|
|
889
|
+
auth,
|
|
890
|
+
name: this.#options.name,
|
|
891
|
+
ping: this.#options.ping,
|
|
892
|
+
prompts: this.#prompts,
|
|
893
|
+
resources: this.#resources,
|
|
894
|
+
resourcesTemplates: this.#resourcesTemplates,
|
|
895
|
+
tools: this.#tools,
|
|
896
|
+
version: this.#options.version
|
|
897
|
+
});
|
|
898
|
+
},
|
|
899
|
+
endpoint: options.httpStream.endpoint,
|
|
900
|
+
onClose: (session) => {
|
|
901
|
+
this.emit("disconnect", {
|
|
902
|
+
session
|
|
903
|
+
});
|
|
904
|
+
},
|
|
905
|
+
onConnect: async (session) => {
|
|
906
|
+
this.#sessions.push(session);
|
|
907
|
+
this.emit("connect", {
|
|
908
|
+
session
|
|
909
|
+
});
|
|
910
|
+
},
|
|
911
|
+
port: options.httpStream.port
|
|
912
|
+
});
|
|
913
|
+
console.info(
|
|
914
|
+
`[FastMCP info] server is running on HTTP Stream at http://localhost:${options.httpStream.port}${options.httpStream.endpoint}`
|
|
915
|
+
);
|
|
847
916
|
} else {
|
|
848
917
|
throw new Error("Invalid transport type");
|
|
849
918
|
}
|
|
@@ -853,7 +922,10 @@ var FastMCP = class extends FastMCPEventEmitter {
|
|
|
853
922
|
*/
|
|
854
923
|
async stop() {
|
|
855
924
|
if (this.#sseServer) {
|
|
856
|
-
this.#sseServer.close();
|
|
925
|
+
await this.#sseServer.close();
|
|
926
|
+
}
|
|
927
|
+
if (this.#httpStreamServer) {
|
|
928
|
+
await this.#httpStreamServer.close();
|
|
857
929
|
}
|
|
858
930
|
}
|
|
859
931
|
};
|
package/dist/FastMCP.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/FastMCP.ts"],"sourcesContent":["import { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport { Transport } from \"@modelcontextprotocol/sdk/shared/transport.js\";\nimport {\n AudioContent,\n CallToolRequestSchema,\n ClientCapabilities,\n CompleteRequestSchema,\n CreateMessageRequestSchema,\n ErrorCode,\n GetPromptRequestSchema,\n ListPromptsRequestSchema,\n ListResourcesRequestSchema,\n ListResourceTemplatesRequestSchema,\n ListToolsRequestSchema,\n McpError,\n ReadResourceRequestSchema,\n Root,\n RootsListChangedNotificationSchema,\n ServerCapabilities,\n SetLevelRequestSchema,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport { EventEmitter } from \"events\";\nimport { fileTypeFromBuffer } from \"file-type\";\nimport { readFile } from \"fs/promises\";\nimport Fuse from \"fuse.js\";\nimport http from \"http\";\nimport { startSSEServer } from \"mcp-proxy\";\nimport { StrictEventEmitter } from \"strict-event-emitter-types\";\nimport { setTimeout as delay } from \"timers/promises\";\nimport { fetch } from \"undici\";\nimport parseURITemplate from \"uri-templates\";\nimport { toJsonSchema } from \"xsschema\";\nimport { z } from \"zod\";\n\nexport type SSEServer = {\n close: () => Promise<void>;\n};\n\ntype FastMCPEvents<T extends FastMCPSessionAuth> = {\n connect: (event: { session: FastMCPSession<T> }) => void;\n disconnect: (event: { session: FastMCPSession<T> }) => void;\n};\n\ntype FastMCPSessionEvents = {\n error: (event: { error: Error }) => void;\n rootsChanged: (event: { roots: Root[] }) => void;\n};\n\nexport const imageContent = async (\n input: { buffer: Buffer } | { path: string } | { url: string },\n): Promise<ImageContent> => {\n let rawData: Buffer;\n\n try {\n if (\"url\" in input) {\n try {\n const response = await fetch(input.url);\n\n if (!response.ok) {\n throw new Error(\n `Server responded with status: ${response.status} - ${response.statusText}`,\n );\n }\n\n rawData = Buffer.from(await response.arrayBuffer());\n } catch (error) {\n throw new Error(\n `Failed to fetch image from URL (${input.url}): ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n } else if (\"path\" in input) {\n try {\n rawData = await readFile(input.path);\n } catch (error) {\n throw new Error(\n `Failed to read image from path (${input.path}): ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n } else if (\"buffer\" in input) {\n rawData = input.buffer;\n } else {\n throw new Error(\n \"Invalid input: Provide a valid 'url', 'path', or 'buffer'\",\n );\n }\n\n const mimeType = await fileTypeFromBuffer(rawData);\n\n if (!mimeType || !mimeType.mime.startsWith(\"image/\")) {\n console.warn(\n `Warning: Content may not be a valid image. Detected MIME: ${mimeType?.mime || \"unknown\"}`,\n );\n }\n\n const base64Data = rawData.toString(\"base64\");\n\n return {\n data: base64Data,\n mimeType: mimeType?.mime ?? \"image/png\",\n type: \"image\",\n } as const;\n } catch (error) {\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(`Unexpected error processing image: ${String(error)}`);\n }\n }\n};\n\nexport const audioContent = async (\n input: { buffer: Buffer } | { path: string } | { url: string },\n): Promise<AudioContent> => {\n let rawData: Buffer;\n\n try {\n if (\"url\" in input) {\n try {\n const response = await fetch(input.url);\n\n if (!response.ok) {\n throw new Error(\n `Server responded with status: ${response.status} - ${response.statusText}`,\n );\n }\n\n rawData = Buffer.from(await response.arrayBuffer());\n } catch (error) {\n throw new Error(\n `Failed to fetch audio from URL (${input.url}): ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n } else if (\"path\" in input) {\n try {\n rawData = await readFile(input.path);\n } catch (error) {\n throw new Error(\n `Failed to read audio from path (${input.path}): ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n } else if (\"buffer\" in input) {\n rawData = input.buffer;\n } else {\n throw new Error(\n \"Invalid input: Provide a valid 'url', 'path', or 'buffer'\",\n );\n }\n\n const mimeType = await fileTypeFromBuffer(rawData);\n\n if (!mimeType || !mimeType.mime.startsWith(\"audio/\")) {\n console.warn(\n `Warning: Content may not be a valid audio file. Detected MIME: ${mimeType?.mime || \"unknown\"}`,\n );\n }\n\n const base64Data = rawData.toString(\"base64\");\n\n return {\n data: base64Data,\n mimeType: mimeType?.mime ?? \"audio/mpeg\",\n type: \"audio\",\n } as const;\n } catch (error) {\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(`Unexpected error processing audio: ${String(error)}`);\n }\n }\n};\n\ntype Context<T extends FastMCPSessionAuth> = {\n log: {\n debug: (message: string, data?: SerializableValue) => void;\n error: (message: string, data?: SerializableValue) => void;\n info: (message: string, data?: SerializableValue) => void;\n warn: (message: string, data?: SerializableValue) => void;\n };\n reportProgress: (progress: Progress) => Promise<void>;\n session: T | undefined;\n};\n\ntype Extra = unknown;\n\ntype Extras = Record<string, Extra>;\n\ntype Literal = boolean | null | number | string | undefined;\n\ntype Progress = {\n /**\n * The progress thus far. This should increase every time progress is made, even if the total is unknown.\n */\n progress: number;\n /**\n * Total number of items to process (or total progress required), if known.\n */\n total?: number;\n};\n\ntype SerializableValue =\n | { [key: string]: SerializableValue }\n | Literal\n | SerializableValue[];\n\ntype TextContent = {\n text: string;\n type: \"text\";\n};\n\ntype ToolParameters = StandardSchemaV1;\n\nabstract class FastMCPError extends Error {\n public constructor(message?: string) {\n super(message);\n this.name = new.target.name;\n }\n}\n\nexport class UnexpectedStateError extends FastMCPError {\n public extras?: Extras;\n\n public constructor(message: string, extras?: Extras) {\n super(message);\n this.name = new.target.name;\n this.extras = extras;\n }\n}\n\n/**\n * An error that is meant to be surfaced to the user.\n */\nexport class UserError extends UnexpectedStateError {}\n\nconst TextContentZodSchema = z\n .object({\n /**\n * The text content of the message.\n */\n text: z.string(),\n type: z.literal(\"text\"),\n })\n .strict() satisfies z.ZodType<TextContent>;\n\ntype ImageContent = {\n data: string;\n mimeType: string;\n type: \"image\";\n};\n\nconst ImageContentZodSchema = z\n .object({\n /**\n * The base64-encoded image data.\n */\n data: z.string().base64(),\n /**\n * The MIME type of the image. Different providers may support different image types.\n */\n mimeType: z.string(),\n type: z.literal(\"image\"),\n })\n .strict() satisfies z.ZodType<ImageContent>;\n\ntype Content = ImageContent | TextContent;\n\nconst ContentZodSchema = z.discriminatedUnion(\"type\", [\n TextContentZodSchema,\n ImageContentZodSchema,\n]) satisfies z.ZodType<Content>;\n\ntype ContentResult = {\n content: Content[];\n isError?: boolean;\n};\n\nconst ContentResultZodSchema = z\n .object({\n content: ContentZodSchema.array(),\n isError: z.boolean().optional(),\n })\n .strict() satisfies z.ZodType<ContentResult>;\n\ntype Completion = {\n hasMore?: boolean;\n total?: number;\n values: string[];\n};\n\n/**\n * https://github.com/modelcontextprotocol/typescript-sdk/blob/3164da64d085ec4e022ae881329eee7b72f208d4/src/types.ts#L983-L1003\n */\nconst CompletionZodSchema = z.object({\n /**\n * Indicates whether there are additional completion options beyond those provided in the current response, even if the exact total is unknown.\n */\n hasMore: z.optional(z.boolean()),\n /**\n * The total number of completion options available. This can exceed the number of values actually sent in the response.\n */\n total: z.optional(z.number().int()),\n /**\n * An array of completion values. Must not exceed 100 items.\n */\n values: z.array(z.string()).max(100),\n}) satisfies z.ZodType<Completion>;\n\ntype ArgumentValueCompleter = (value: string) => Promise<Completion>;\n\ntype InputPrompt<\n Arguments extends InputPromptArgument[] = InputPromptArgument[],\n Args = PromptArgumentsToObject<Arguments>,\n> = {\n arguments?: InputPromptArgument[];\n description?: string;\n load: (args: Args) => Promise<string>;\n name: string;\n};\n\ntype InputPromptArgument = Readonly<{\n complete?: ArgumentValueCompleter;\n description?: string;\n enum?: string[];\n name: string;\n required?: boolean;\n}>;\n\ntype InputResourceTemplate<\n Arguments extends ResourceTemplateArgument[] = ResourceTemplateArgument[],\n> = {\n arguments: Arguments;\n description?: string;\n load: (\n args: ResourceTemplateArgumentsToObject<Arguments>,\n ) => Promise<ResourceResult>;\n mimeType?: string;\n name: string;\n uriTemplate: string;\n};\n\ntype InputResourceTemplateArgument = Readonly<{\n complete?: ArgumentValueCompleter;\n description?: string;\n name: string;\n}>;\n\ntype LoggingLevel =\n | \"alert\"\n | \"critical\"\n | \"debug\"\n | \"emergency\"\n | \"error\"\n | \"info\"\n | \"notice\"\n | \"warning\";\n\ntype Prompt<\n Arguments extends PromptArgument[] = PromptArgument[],\n Args = PromptArgumentsToObject<Arguments>,\n> = {\n arguments?: PromptArgument[];\n complete?: (name: string, value: string) => Promise<Completion>;\n description?: string;\n load: (args: Args) => Promise<string>;\n name: string;\n};\n\ntype PromptArgument = Readonly<{\n complete?: ArgumentValueCompleter;\n description?: string;\n enum?: string[];\n name: string;\n required?: boolean;\n}>;\n\ntype PromptArgumentsToObject<T extends { name: string; required?: boolean }[]> =\n {\n [K in T[number][\"name\"]]: Extract<\n T[number],\n { name: K }\n >[\"required\"] extends true\n ? string\n : string | undefined;\n };\n\ntype Resource = {\n complete?: (name: string, value: string) => Promise<Completion>;\n description?: string;\n load: () => Promise<ResourceResult | ResourceResult[]>;\n mimeType?: string;\n name: string;\n uri: string;\n};\n\ntype ResourceResult =\n | {\n blob: string;\n }\n | {\n text: string;\n };\n\ntype ResourceTemplate<\n Arguments extends ResourceTemplateArgument[] = ResourceTemplateArgument[],\n> = {\n arguments: Arguments;\n complete?: (name: string, value: string) => Promise<Completion>;\n description?: string;\n load: (\n args: ResourceTemplateArgumentsToObject<Arguments>,\n ) => Promise<ResourceResult>;\n mimeType?: string;\n name: string;\n uriTemplate: string;\n};\n\ntype ResourceTemplateArgument = Readonly<{\n complete?: ArgumentValueCompleter;\n description?: string;\n name: string;\n}>;\n\ntype ResourceTemplateArgumentsToObject<T extends { name: string }[]> = {\n [K in T[number][\"name\"]]: string;\n};\n\ntype ServerOptions<T extends FastMCPSessionAuth> = {\n authenticate?: Authenticate<T>;\n instructions?: string;\n name: string;\n version: `${number}.${number}.${number}`;\n};\n\ntype Tool<\n T extends FastMCPSessionAuth,\n Params extends ToolParameters = ToolParameters,\n> = {\n annotations?: ToolAnnotations;\n description?: string;\n execute: (\n args: StandardSchemaV1.InferOutput<Params>,\n context: Context<T>,\n ) => Promise<\n AudioContent | ContentResult | ImageContent | string | TextContent\n >;\n name: string;\n parameters?: Params;\n timeoutMs?: number;\n};\n\n/**\n * Tool annotations as defined in MCP Specification (2025-03-26)\n * These provide hints about a tool's behavior.\n */\ntype ToolAnnotations = {\n /**\n * If true, the tool may perform destructive updates\n * Only meaningful when readOnlyHint is false\n * @default true\n */\n destructiveHint?: boolean;\n\n /**\n * If true, calling the tool repeatedly with the same arguments has no additional effect\n * Only meaningful when readOnlyHint is false\n * @default false\n */\n idempotentHint?: boolean;\n\n /**\n * If true, the tool may interact with an \"open world\" of external entities\n * @default true\n */\n openWorldHint?: boolean;\n\n /**\n * If true, indicates the tool does not modify its environment\n * @default false\n */\n readOnlyHint?: boolean;\n\n /**\n * A human-readable title for the tool, useful for UI display\n */\n title?: string;\n};\n\nconst FastMCPSessionEventEmitterBase: {\n new (): StrictEventEmitter<EventEmitter, FastMCPSessionEvents>;\n} = EventEmitter;\n\ntype FastMCPSessionAuth = Record<string, unknown> | undefined;\n\ntype SamplingResponse = {\n content: AudioContent | ImageContent | TextContent;\n model: string;\n role: \"assistant\" | \"user\";\n stopReason?: \"endTurn\" | \"maxTokens\" | \"stopSequence\" | string;\n};\n\nclass FastMCPSessionEventEmitter extends FastMCPSessionEventEmitterBase {}\n\nexport class FastMCPSession<\n T extends FastMCPSessionAuth = FastMCPSessionAuth,\n> extends FastMCPSessionEventEmitter {\n public get clientCapabilities(): ClientCapabilities | null {\n return this.#clientCapabilities ?? null;\n }\n public get loggingLevel(): LoggingLevel {\n return this.#loggingLevel;\n }\n public get roots(): Root[] {\n return this.#roots;\n }\n public get server(): Server {\n return this.#server;\n }\n #auth: T | undefined;\n #capabilities: ServerCapabilities = {};\n #clientCapabilities?: ClientCapabilities;\n #loggingLevel: LoggingLevel = \"info\";\n #pingInterval: null | ReturnType<typeof setInterval> = null;\n\n #prompts: Prompt[] = [];\n\n #resources: Resource[] = [];\n\n #resourceTemplates: ResourceTemplate[] = [];\n\n #roots: Root[] = [];\n\n #server: Server;\n\n constructor({\n auth,\n instructions,\n name,\n prompts,\n resources,\n resourcesTemplates,\n tools,\n version,\n }: {\n auth?: T;\n instructions?: string;\n name: string;\n prompts: Prompt[];\n resources: Resource[];\n resourcesTemplates: InputResourceTemplate[];\n tools: Tool<T>[];\n version: string;\n }) {\n super();\n\n this.#auth = auth;\n\n if (tools.length) {\n this.#capabilities.tools = {};\n }\n\n if (resources.length || resourcesTemplates.length) {\n this.#capabilities.resources = {};\n }\n\n if (prompts.length) {\n for (const prompt of prompts) {\n this.addPrompt(prompt);\n }\n\n this.#capabilities.prompts = {};\n }\n\n this.#capabilities.logging = {};\n\n this.#server = new Server(\n { name: name, version: version },\n { capabilities: this.#capabilities, instructions: instructions },\n );\n\n this.setupErrorHandling();\n this.setupLoggingHandlers();\n this.setupRootsHandlers();\n this.setupCompleteHandlers();\n\n if (tools.length) {\n this.setupToolHandlers(tools);\n }\n\n if (resources.length || resourcesTemplates.length) {\n for (const resource of resources) {\n this.addResource(resource);\n }\n\n this.setupResourceHandlers(resources);\n\n if (resourcesTemplates.length) {\n for (const resourceTemplate of resourcesTemplates) {\n this.addResourceTemplate(resourceTemplate);\n }\n\n this.setupResourceTemplateHandlers(resourcesTemplates);\n }\n }\n\n if (prompts.length) {\n this.setupPromptHandlers(prompts);\n }\n }\n\n public async close() {\n if (this.#pingInterval) {\n clearInterval(this.#pingInterval);\n }\n\n try {\n await this.#server.close();\n } catch (error) {\n console.error(\"[FastMCP error]\", \"could not close server\", error);\n }\n }\n\n public async connect(transport: Transport) {\n if (this.#server.transport) {\n throw new UnexpectedStateError(\"Server is already connected\");\n }\n\n await this.#server.connect(transport);\n\n let attempt = 0;\n\n while (attempt++ < 10) {\n const capabilities = await this.#server.getClientCapabilities();\n\n if (capabilities) {\n this.#clientCapabilities = capabilities;\n\n break;\n }\n\n await delay(100);\n }\n\n if (!this.#clientCapabilities) {\n console.warn(\"[FastMCP warning] could not infer client capabilities\");\n }\n\n if (this.#clientCapabilities?.roots?.listChanged) {\n try {\n const roots = await this.#server.listRoots();\n this.#roots = roots.roots;\n } catch (e) {\n console.error(\n `[FastMCP error] received error listing roots.\\n\\n${e instanceof Error ? e.stack : JSON.stringify(e)}`,\n );\n }\n }\n\n if (this.#clientCapabilities) {\n this.#pingInterval = setInterval(async () => {\n try {\n await this.#server.ping();\n } catch {\n // The reason we are not emitting an error here is because some clients\n // seem to not respond to the ping request, and we don't want to crash the server,\n // e.g., https://github.com/punkpeye/fastmcp/issues/38.\n console.warn(\"[FastMCP warning] server is not responding to ping\");\n }\n }, 1000);\n }\n }\n\n public async requestSampling(\n message: z.infer<typeof CreateMessageRequestSchema>[\"params\"],\n ): Promise<SamplingResponse> {\n return this.#server.createMessage(message);\n }\n\n private addPrompt(inputPrompt: InputPrompt) {\n const completers: Record<string, ArgumentValueCompleter> = {};\n const enums: Record<string, string[]> = {};\n\n for (const argument of inputPrompt.arguments ?? []) {\n if (argument.complete) {\n completers[argument.name] = argument.complete;\n }\n\n if (argument.enum) {\n enums[argument.name] = argument.enum;\n }\n }\n\n const prompt = {\n ...inputPrompt,\n complete: async (name: string, value: string) => {\n if (completers[name]) {\n return await completers[name](value);\n }\n\n if (enums[name]) {\n const fuse = new Fuse(enums[name], {\n keys: [\"value\"],\n });\n\n const result = fuse.search(value);\n\n return {\n total: result.length,\n values: result.map((item) => item.item),\n };\n }\n\n return {\n values: [],\n };\n },\n };\n\n this.#prompts.push(prompt);\n }\n\n private addResource(inputResource: Resource) {\n this.#resources.push(inputResource);\n }\n\n private addResourceTemplate(inputResourceTemplate: InputResourceTemplate) {\n const completers: Record<string, ArgumentValueCompleter> = {};\n\n for (const argument of inputResourceTemplate.arguments ?? []) {\n if (argument.complete) {\n completers[argument.name] = argument.complete;\n }\n }\n\n const resourceTemplate = {\n ...inputResourceTemplate,\n complete: async (name: string, value: string) => {\n if (completers[name]) {\n return await completers[name](value);\n }\n\n return {\n values: [],\n };\n },\n };\n\n this.#resourceTemplates.push(resourceTemplate);\n }\n\n private setupCompleteHandlers() {\n this.#server.setRequestHandler(CompleteRequestSchema, async (request) => {\n if (request.params.ref.type === \"ref/prompt\") {\n const prompt = this.#prompts.find(\n (prompt) => prompt.name === request.params.ref.name,\n );\n\n if (!prompt) {\n throw new UnexpectedStateError(\"Unknown prompt\", {\n request,\n });\n }\n\n if (!prompt.complete) {\n throw new UnexpectedStateError(\"Prompt does not support completion\", {\n request,\n });\n }\n\n const completion = CompletionZodSchema.parse(\n await prompt.complete(\n request.params.argument.name,\n request.params.argument.value,\n ),\n );\n\n return {\n completion,\n };\n }\n\n if (request.params.ref.type === \"ref/resource\") {\n const resource = this.#resourceTemplates.find(\n (resource) => resource.uriTemplate === request.params.ref.uri,\n );\n\n if (!resource) {\n throw new UnexpectedStateError(\"Unknown resource\", {\n request,\n });\n }\n\n if (!(\"uriTemplate\" in resource)) {\n throw new UnexpectedStateError(\"Unexpected resource\");\n }\n\n if (!resource.complete) {\n throw new UnexpectedStateError(\n \"Resource does not support completion\",\n {\n request,\n },\n );\n }\n\n const completion = CompletionZodSchema.parse(\n await resource.complete(\n request.params.argument.name,\n request.params.argument.value,\n ),\n );\n\n return {\n completion,\n };\n }\n\n throw new UnexpectedStateError(\"Unexpected completion request\", {\n request,\n });\n });\n }\n\n private setupErrorHandling() {\n this.#server.onerror = (error) => {\n console.error(\"[FastMCP error]\", error);\n };\n }\n\n private setupLoggingHandlers() {\n this.#server.setRequestHandler(SetLevelRequestSchema, (request) => {\n this.#loggingLevel = request.params.level;\n\n return {};\n });\n }\n\n private setupPromptHandlers(prompts: Prompt[]) {\n this.#server.setRequestHandler(ListPromptsRequestSchema, async () => {\n return {\n prompts: prompts.map((prompt) => {\n return {\n arguments: prompt.arguments,\n complete: prompt.complete,\n description: prompt.description,\n name: prompt.name,\n };\n }),\n };\n });\n\n this.#server.setRequestHandler(GetPromptRequestSchema, async (request) => {\n const prompt = prompts.find(\n (prompt) => prompt.name === request.params.name,\n );\n\n if (!prompt) {\n throw new McpError(\n ErrorCode.MethodNotFound,\n `Unknown prompt: ${request.params.name}`,\n );\n }\n\n const args = request.params.arguments;\n\n for (const arg of prompt.arguments ?? []) {\n if (arg.required && !(args && arg.name in args)) {\n throw new McpError(\n ErrorCode.InvalidRequest,\n `Missing required argument: ${arg.name}`,\n );\n }\n }\n\n let result: Awaited<ReturnType<Prompt[\"load\"]>>;\n\n try {\n result = await prompt.load(args as Record<string, string | undefined>);\n } catch (error) {\n throw new McpError(\n ErrorCode.InternalError,\n `Error loading prompt: ${error}`,\n );\n }\n\n return {\n description: prompt.description,\n messages: [\n {\n content: { text: result, type: \"text\" },\n role: \"user\",\n },\n ],\n };\n });\n }\n\n private setupResourceHandlers(resources: Resource[]) {\n this.#server.setRequestHandler(ListResourcesRequestSchema, async () => {\n return {\n resources: resources.map((resource) => {\n return {\n mimeType: resource.mimeType,\n name: resource.name,\n uri: resource.uri,\n };\n }),\n };\n });\n\n this.#server.setRequestHandler(\n ReadResourceRequestSchema,\n async (request) => {\n if (\"uri\" in request.params) {\n const resource = resources.find(\n (resource) =>\n \"uri\" in resource && resource.uri === request.params.uri,\n );\n\n if (!resource) {\n for (const resourceTemplate of this.#resourceTemplates) {\n const uriTemplate = parseURITemplate(\n resourceTemplate.uriTemplate,\n );\n\n const match = uriTemplate.fromUri(request.params.uri);\n\n if (!match) {\n continue;\n }\n\n const uri = uriTemplate.fill(match);\n\n const result = await resourceTemplate.load(match);\n\n return {\n contents: [\n {\n mimeType: resourceTemplate.mimeType,\n name: resourceTemplate.name,\n uri: uri,\n ...result,\n },\n ],\n };\n }\n\n throw new McpError(\n ErrorCode.MethodNotFound,\n `Unknown resource: ${request.params.uri}`,\n );\n }\n\n if (!(\"uri\" in resource)) {\n throw new UnexpectedStateError(\"Resource does not support reading\");\n }\n\n let maybeArrayResult: Awaited<ReturnType<Resource[\"load\"]>>;\n\n try {\n maybeArrayResult = await resource.load();\n } catch (error) {\n throw new McpError(\n ErrorCode.InternalError,\n `Error reading resource: ${error}`,\n {\n uri: resource.uri,\n },\n );\n }\n\n if (Array.isArray(maybeArrayResult)) {\n return {\n contents: maybeArrayResult.map((result) => ({\n mimeType: resource.mimeType,\n name: resource.name,\n uri: resource.uri,\n ...result,\n })),\n };\n } else {\n return {\n contents: [\n {\n mimeType: resource.mimeType,\n name: resource.name,\n uri: resource.uri,\n ...maybeArrayResult,\n },\n ],\n };\n }\n }\n\n throw new UnexpectedStateError(\"Unknown resource request\", {\n request,\n });\n },\n );\n }\n\n private setupResourceTemplateHandlers(resourceTemplates: ResourceTemplate[]) {\n this.#server.setRequestHandler(\n ListResourceTemplatesRequestSchema,\n async () => {\n return {\n resourceTemplates: resourceTemplates.map((resourceTemplate) => {\n return {\n name: resourceTemplate.name,\n uriTemplate: resourceTemplate.uriTemplate,\n };\n }),\n };\n },\n );\n }\n\n private setupRootsHandlers() {\n this.#server.setNotificationHandler(\n RootsListChangedNotificationSchema,\n () => {\n this.#server.listRoots().then((roots) => {\n this.#roots = roots.roots;\n\n this.emit(\"rootsChanged\", {\n roots: roots.roots,\n });\n });\n },\n );\n }\n\n private setupToolHandlers(tools: Tool<T>[]) {\n this.#server.setRequestHandler(ListToolsRequestSchema, async () => {\n return {\n tools: await Promise.all(\n tools.map(async (tool) => {\n return {\n annotations: tool.annotations,\n description: tool.description,\n inputSchema: tool.parameters\n ? await toJsonSchema(tool.parameters)\n : undefined,\n name: tool.name,\n };\n }),\n ),\n };\n });\n\n this.#server.setRequestHandler(CallToolRequestSchema, async (request) => {\n const tool = tools.find((tool) => tool.name === request.params.name);\n\n if (!tool) {\n throw new McpError(\n ErrorCode.MethodNotFound,\n `Unknown tool: ${request.params.name}`,\n );\n }\n\n let args: unknown = undefined;\n\n if (tool.parameters) {\n const parsed = await tool.parameters[\"~standard\"].validate(\n request.params.arguments,\n );\n\n if (parsed.issues) {\n throw new McpError(\n ErrorCode.InvalidParams,\n `Invalid ${request.params.name} parameters: ${JSON.stringify(parsed.issues)}`,\n );\n }\n\n args = parsed.value;\n }\n\n const progressToken = request.params?._meta?.progressToken;\n\n let result: ContentResult;\n\n try {\n const reportProgress = async (progress: Progress) => {\n await this.#server.notification({\n method: \"notifications/progress\",\n params: {\n ...progress,\n progressToken,\n },\n });\n };\n\n const log = {\n debug: (message: string, context?: SerializableValue) => {\n this.#server.sendLoggingMessage({\n data: {\n context,\n message,\n },\n level: \"debug\",\n });\n },\n error: (message: string, context?: SerializableValue) => {\n this.#server.sendLoggingMessage({\n data: {\n context,\n message,\n },\n level: \"error\",\n });\n },\n info: (message: string, context?: SerializableValue) => {\n this.#server.sendLoggingMessage({\n data: {\n context,\n message,\n },\n level: \"info\",\n });\n },\n warn: (message: string, context?: SerializableValue) => {\n this.#server.sendLoggingMessage({\n data: {\n context,\n message,\n },\n level: \"warning\",\n });\n },\n };\n\n // Create a promise for tool execution\n const executeToolPromise = tool.execute(args, {\n log,\n reportProgress,\n session: this.#auth,\n });\n\n // Handle timeout if specified\n const maybeStringResult = await (tool.timeoutMs\n ? Promise.race([\n executeToolPromise,\n new Promise<never>((_, reject) => {\n setTimeout(() => {\n reject(\n new UserError(\n `Tool execution timed out after ${tool.timeoutMs}ms`,\n ),\n );\n }, tool.timeoutMs);\n }),\n ])\n : executeToolPromise);\n\n if (typeof maybeStringResult === \"string\") {\n result = ContentResultZodSchema.parse({\n content: [{ text: maybeStringResult, type: \"text\" }],\n });\n } else if (\"type\" in maybeStringResult) {\n result = ContentResultZodSchema.parse({\n content: [maybeStringResult],\n });\n } else {\n result = ContentResultZodSchema.parse(maybeStringResult);\n }\n } catch (error) {\n if (error instanceof UserError) {\n return {\n content: [{ text: error.message, type: \"text\" }],\n isError: true,\n };\n }\n\n return {\n content: [{ text: `Error: ${error}`, type: \"text\" }],\n isError: true,\n };\n }\n\n return result;\n });\n }\n}\n\nconst FastMCPEventEmitterBase: {\n new (): StrictEventEmitter<EventEmitter, FastMCPEvents<FastMCPSessionAuth>>;\n} = EventEmitter;\n\ntype Authenticate<T> = (request: http.IncomingMessage) => Promise<T>;\n\nclass FastMCPEventEmitter extends FastMCPEventEmitterBase {}\n\nexport class FastMCP<\n T extends Record<string, unknown> | undefined = undefined,\n> extends FastMCPEventEmitter {\n public get sessions(): FastMCPSession<T>[] {\n return this.#sessions;\n }\n #authenticate: Authenticate<T> | undefined;\n #options: ServerOptions<T>;\n #prompts: InputPrompt[] = [];\n #resources: Resource[] = [];\n #resourcesTemplates: InputResourceTemplate[] = [];\n #sessions: FastMCPSession<T>[] = [];\n #sseServer: null | SSEServer = null;\n\n #tools: Tool<T>[] = [];\n\n constructor(public options: ServerOptions<T>) {\n super();\n\n this.#options = options;\n this.#authenticate = options.authenticate;\n }\n\n /**\n * Adds a prompt to the server.\n */\n public addPrompt<const Args extends InputPromptArgument[]>(\n prompt: InputPrompt<Args>,\n ) {\n this.#prompts.push(prompt);\n }\n\n /**\n * Adds a resource to the server.\n */\n public addResource(resource: Resource) {\n this.#resources.push(resource);\n }\n\n /**\n * Adds a resource template to the server.\n */\n public addResourceTemplate<\n const Args extends InputResourceTemplateArgument[],\n >(resource: InputResourceTemplate<Args>) {\n this.#resourcesTemplates.push(resource);\n }\n\n /**\n * Adds a tool to the server.\n */\n public addTool<Params extends ToolParameters>(tool: Tool<T, Params>) {\n this.#tools.push(tool as unknown as Tool<T>);\n }\n\n /**\n * Starts the server.\n */\n public async start(\n options:\n | {\n sse: { endpoint: `/${string}`; port: number };\n transportType: \"sse\";\n }\n | { transportType: \"stdio\" } = {\n transportType: \"stdio\",\n },\n ) {\n if (options.transportType === \"stdio\") {\n const transport = new StdioServerTransport();\n\n const session = new FastMCPSession<T>({\n instructions: this.#options.instructions,\n name: this.#options.name,\n prompts: this.#prompts,\n resources: this.#resources,\n resourcesTemplates: this.#resourcesTemplates,\n tools: this.#tools,\n version: this.#options.version,\n });\n\n await session.connect(transport);\n\n this.#sessions.push(session);\n\n this.emit(\"connect\", {\n session,\n });\n } else if (options.transportType === \"sse\") {\n this.#sseServer = await startSSEServer<FastMCPSession<T>>({\n createServer: async (request) => {\n let auth: T | undefined;\n\n if (this.#authenticate) {\n auth = await this.#authenticate(request);\n }\n\n return new FastMCPSession<T>({\n auth,\n name: this.#options.name,\n prompts: this.#prompts,\n resources: this.#resources,\n resourcesTemplates: this.#resourcesTemplates,\n tools: this.#tools,\n version: this.#options.version,\n });\n },\n endpoint: options.sse.endpoint as `/${string}`,\n onClose: (session) => {\n this.emit(\"disconnect\", {\n session,\n });\n },\n onConnect: async (session) => {\n this.#sessions.push(session);\n\n this.emit(\"connect\", {\n session,\n });\n },\n port: options.sse.port,\n });\n\n console.info(\n `[FastMCP info] server is running on SSE at http://localhost:${options.sse.port}${options.sse.endpoint}`,\n );\n } else {\n throw new Error(\"Invalid transport type\");\n }\n }\n\n /**\n * Stops the server.\n */\n public async stop() {\n if (this.#sseServer) {\n this.#sseServer.close();\n }\n }\n}\n\nexport type { Context };\nexport type { Tool, ToolParameters };\nexport type { Content, ContentResult, ImageContent, TextContent };\nexport type { Progress, SerializableValue };\nexport type { Resource, ResourceResult };\nexport type { ResourceTemplate, ResourceTemplateArgument };\nexport type { Prompt, PromptArgument };\nexport type { InputPrompt, InputPromptArgument };\nexport type { LoggingLevel, ServerOptions };\nexport type { FastMCPEvents, FastMCPSessionEvents };\n"],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,4BAA4B;AAErC;AAAA,EAEE;AAAA,EAEA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EAEA;AAAA,OACK;AAEP,SAAS,oBAAoB;AAC7B,SAAS,0BAA0B;AACnC,SAAS,gBAAgB;AACzB,OAAO,UAAU;AAEjB,SAAS,sBAAsB;AAE/B,SAAS,cAAc,aAAa;AACpC,SAAS,aAAa;AACtB,OAAO,sBAAsB;AAC7B,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAgBX,IAAM,eAAe,OAC1B,UAC0B;AAC1B,MAAI;AAEJ,MAAI;AACF,QAAI,SAAS,OAAO;AAClB,UAAI;AACF,cAAM,WAAW,MAAM,MAAM,MAAM,GAAG;AAEtC,YAAI,CAAC,SAAS,IAAI;AAChB,gBAAM,IAAI;AAAA,YACR,iCAAiC,SAAS,MAAM,MAAM,SAAS,UAAU;AAAA,UAC3E;AAAA,QACF;AAEA,kBAAU,OAAO,KAAK,MAAM,SAAS,YAAY,CAAC;AAAA,MACpD,SAAS,OAAO;AACd,cAAM,IAAI;AAAA,UACR,mCAAmC,MAAM,GAAG,MAAM,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;AAAA,QAC1G;AAAA,MACF;AAAA,IACF,WA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AAA,UACF,CAAC;AAAA,QACH;AAAA,QACA,MAAM,QAAQ,IAAI;AAAA,MACpB,CAAC;AAED,cAAQ;AAAA,QACN,+DAA+D,QAAQ,IAAI,IAAI,GAAG,QAAQ,IAAI,QAAQ;AAAA,MACxG;AAAA,IACF,OAAO;AACL,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,MAAa,OAAO;AAClB,QAAI,KAAK,YAAY;AACnB,WAAK,WAAW,MAAM;AAAA,IACxB;AAAA,EACF;AACF;","names":["prompt","resource","tool"]}
|
|
1
|
+
{"version":3,"sources":["../src/FastMCP.ts"],"sourcesContent":["import { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport { Transport } from \"@modelcontextprotocol/sdk/shared/transport.js\";\nimport {\n AudioContent,\n CallToolRequestSchema,\n ClientCapabilities,\n CompleteRequestSchema,\n CreateMessageRequestSchema,\n ErrorCode,\n GetPromptRequestSchema,\n ListPromptsRequestSchema,\n ListResourcesRequestSchema,\n ListResourceTemplatesRequestSchema,\n ListToolsRequestSchema,\n McpError,\n ReadResourceRequestSchema,\n Root,\n RootsListChangedNotificationSchema,\n ServerCapabilities,\n SetLevelRequestSchema,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport { EventEmitter } from \"events\";\nimport { fileTypeFromBuffer } from \"file-type\";\nimport { readFile } from \"fs/promises\";\nimport Fuse from \"fuse.js\";\nimport http from \"http\";\nimport { startHTTPStreamServer, startSSEServer } from \"mcp-proxy\";\nimport { StrictEventEmitter } from \"strict-event-emitter-types\";\nimport { setTimeout as delay } from \"timers/promises\";\nimport { fetch } from \"undici\";\nimport parseURITemplate from \"uri-templates\";\nimport { toJsonSchema } from \"xsschema\";\nimport { z } from \"zod\";\n\nexport type SSEServer = {\n close: () => Promise<void>;\n};\n\ntype FastMCPEvents<T extends FastMCPSessionAuth> = {\n connect: (event: { session: FastMCPSession<T> }) => void;\n disconnect: (event: { session: FastMCPSession<T> }) => void;\n};\n\ntype FastMCPSessionEvents = {\n error: (event: { error: Error }) => void;\n rootsChanged: (event: { roots: Root[] }) => void;\n};\n\nexport const imageContent = async (\n input: { buffer: Buffer } | { path: string } | { url: string },\n): Promise<ImageContent> => {\n let rawData: Buffer;\n\n try {\n if (\"url\" in input) {\n try {\n const response = await fetch(input.url);\n\n if (!response.ok) {\n throw new Error(\n `Server responded with status: ${response.status} - ${response.statusText}`,\n );\n }\n\n rawData = Buffer.from(await response.arrayBuffer());\n } catch (error) {\n throw new Error(\n `Failed to fetch image from URL (${input.url}): ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n } else if (\"path\" in input) {\n try {\n rawData = await readFile(input.path);\n } catch (error) {\n throw new Error(\n `Failed to read image from path (${input.path}): ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n } else if (\"buffer\" in input) {\n rawData = input.buffer;\n } else {\n throw new Error(\n \"Invalid input: Provide a valid 'url', 'path', or 'buffer'\",\n );\n }\n\n const mimeType = await fileTypeFromBuffer(rawData);\n\n if (!mimeType || !mimeType.mime.startsWith(\"image/\")) {\n console.warn(\n `Warning: Content may not be a valid image. Detected MIME: ${mimeType?.mime || \"unknown\"}`,\n );\n }\n\n const base64Data = rawData.toString(\"base64\");\n\n return {\n data: base64Data,\n mimeType: mimeType?.mime ?? \"image/png\",\n type: \"image\",\n } as const;\n } catch (error) {\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(`Unexpected error processing image: ${String(error)}`);\n }\n }\n};\n\nexport const audioContent = async (\n input: { buffer: Buffer } | { path: string } | { url: string },\n): Promise<AudioContent> => {\n let rawData: Buffer;\n\n try {\n if (\"url\" in input) {\n try {\n const response = await fetch(input.url);\n\n if (!response.ok) {\n throw new Error(\n `Server responded with status: ${response.status} - ${response.statusText}`,\n );\n }\n\n rawData = Buffer.from(await response.arrayBuffer());\n } catch (error) {\n throw new Error(\n `Failed to fetch audio from URL (${input.url}): ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n } else if (\"path\" in input) {\n try {\n rawData = await readFile(input.path);\n } catch (error) {\n throw new Error(\n `Failed to read audio from path (${input.path}): ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n } else if (\"buffer\" in input) {\n rawData = input.buffer;\n } else {\n throw new Error(\n \"Invalid input: Provide a valid 'url', 'path', or 'buffer'\",\n );\n }\n\n const mimeType = await fileTypeFromBuffer(rawData);\n\n if (!mimeType || !mimeType.mime.startsWith(\"audio/\")) {\n console.warn(\n `Warning: Content may not be a valid audio file. Detected MIME: ${mimeType?.mime || \"unknown\"}`,\n );\n }\n\n const base64Data = rawData.toString(\"base64\");\n\n return {\n data: base64Data,\n mimeType: mimeType?.mime ?? \"audio/mpeg\",\n type: \"audio\",\n } as const;\n } catch (error) {\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(`Unexpected error processing audio: ${String(error)}`);\n }\n }\n};\n\ntype Context<T extends FastMCPSessionAuth> = {\n log: {\n debug: (message: string, data?: SerializableValue) => void;\n error: (message: string, data?: SerializableValue) => void;\n info: (message: string, data?: SerializableValue) => void;\n warn: (message: string, data?: SerializableValue) => void;\n };\n reportProgress: (progress: Progress) => Promise<void>;\n session: T | undefined;\n};\n\ntype Extra = unknown;\n\ntype Extras = Record<string, Extra>;\n\ntype Literal = boolean | null | number | string | undefined;\n\ntype Progress = {\n /**\n * The progress thus far. This should increase every time progress is made, even if the total is unknown.\n */\n progress: number;\n /**\n * Total number of items to process (or total progress required), if known.\n */\n total?: number;\n};\n\ntype SerializableValue =\n | { [key: string]: SerializableValue }\n | Literal\n | SerializableValue[];\n\ntype TextContent = {\n text: string;\n type: \"text\";\n};\n\ntype ToolParameters = StandardSchemaV1;\n\nabstract class FastMCPError extends Error {\n public constructor(message?: string) {\n super(message);\n this.name = new.target.name;\n }\n}\n\nexport class UnexpectedStateError extends FastMCPError {\n public extras?: Extras;\n\n public constructor(message: string, extras?: Extras) {\n super(message);\n this.name = new.target.name;\n this.extras = extras;\n }\n}\n\n/**\n * An error that is meant to be surfaced to the user.\n */\nexport class UserError extends UnexpectedStateError {}\n\nconst TextContentZodSchema = z\n .object({\n /**\n * The text content of the message.\n */\n text: z.string(),\n type: z.literal(\"text\"),\n })\n .strict() satisfies z.ZodType<TextContent>;\n\ntype ImageContent = {\n data: string;\n mimeType: string;\n type: \"image\";\n};\n\nconst ImageContentZodSchema = z\n .object({\n /**\n * The base64-encoded image data.\n */\n data: z.string().base64(),\n /**\n * The MIME type of the image. Different providers may support different image types.\n */\n mimeType: z.string(),\n type: z.literal(\"image\"),\n })\n .strict() satisfies z.ZodType<ImageContent>;\n\ntype Content = ImageContent | TextContent;\n\nconst ContentZodSchema = z.discriminatedUnion(\"type\", [\n TextContentZodSchema,\n ImageContentZodSchema,\n]) satisfies z.ZodType<Content>;\n\ntype ContentResult = {\n content: Content[];\n isError?: boolean;\n};\n\nconst ContentResultZodSchema = z\n .object({\n content: ContentZodSchema.array(),\n isError: z.boolean().optional(),\n })\n .strict() satisfies z.ZodType<ContentResult>;\n\ntype Completion = {\n hasMore?: boolean;\n total?: number;\n values: string[];\n};\n\n/**\n * https://github.com/modelcontextprotocol/typescript-sdk/blob/3164da64d085ec4e022ae881329eee7b72f208d4/src/types.ts#L983-L1003\n */\nconst CompletionZodSchema = z.object({\n /**\n * Indicates whether there are additional completion options beyond those provided in the current response, even if the exact total is unknown.\n */\n hasMore: z.optional(z.boolean()),\n /**\n * The total number of completion options available. This can exceed the number of values actually sent in the response.\n */\n total: z.optional(z.number().int()),\n /**\n * An array of completion values. Must not exceed 100 items.\n */\n values: z.array(z.string()).max(100),\n}) satisfies z.ZodType<Completion>;\n\ntype ArgumentValueCompleter = (value: string) => Promise<Completion>;\n\ntype InputPrompt<\n Arguments extends InputPromptArgument[] = InputPromptArgument[],\n Args = PromptArgumentsToObject<Arguments>,\n> = {\n arguments?: InputPromptArgument[];\n description?: string;\n load: (args: Args) => Promise<string>;\n name: string;\n};\n\ntype InputPromptArgument = Readonly<{\n complete?: ArgumentValueCompleter;\n description?: string;\n enum?: string[];\n name: string;\n required?: boolean;\n}>;\n\ntype InputResourceTemplate<\n Arguments extends ResourceTemplateArgument[] = ResourceTemplateArgument[],\n> = {\n arguments: Arguments;\n description?: string;\n load: (\n args: ResourceTemplateArgumentsToObject<Arguments>,\n ) => Promise<ResourceResult>;\n mimeType?: string;\n name: string;\n uriTemplate: string;\n};\n\ntype InputResourceTemplateArgument = Readonly<{\n complete?: ArgumentValueCompleter;\n description?: string;\n name: string;\n}>;\n\ntype LoggingLevel =\n | \"alert\"\n | \"critical\"\n | \"debug\"\n | \"emergency\"\n | \"error\"\n | \"info\"\n | \"notice\"\n | \"warning\";\n\ntype Prompt<\n Arguments extends PromptArgument[] = PromptArgument[],\n Args = PromptArgumentsToObject<Arguments>,\n> = {\n arguments?: PromptArgument[];\n complete?: (name: string, value: string) => Promise<Completion>;\n description?: string;\n load: (args: Args) => Promise<string>;\n name: string;\n};\n\ntype PromptArgument = Readonly<{\n complete?: ArgumentValueCompleter;\n description?: string;\n enum?: string[];\n name: string;\n required?: boolean;\n}>;\n\ntype PromptArgumentsToObject<T extends { name: string; required?: boolean }[]> =\n {\n [K in T[number][\"name\"]]: Extract<\n T[number],\n { name: K }\n >[\"required\"] extends true\n ? string\n : string | undefined;\n };\n\ntype Resource = {\n complete?: (name: string, value: string) => Promise<Completion>;\n description?: string;\n load: () => Promise<ResourceResult | ResourceResult[]>;\n mimeType?: string;\n name: string;\n uri: string;\n};\n\ntype ResourceResult =\n | {\n blob: string;\n }\n | {\n text: string;\n };\n\ntype ResourceTemplate<\n Arguments extends ResourceTemplateArgument[] = ResourceTemplateArgument[],\n> = {\n arguments: Arguments;\n complete?: (name: string, value: string) => Promise<Completion>;\n description?: string;\n load: (\n args: ResourceTemplateArgumentsToObject<Arguments>,\n ) => Promise<ResourceResult>;\n mimeType?: string;\n name: string;\n uriTemplate: string;\n};\n\ntype ResourceTemplateArgument = Readonly<{\n complete?: ArgumentValueCompleter;\n description?: string;\n name: string;\n}>;\n\ntype ResourceTemplateArgumentsToObject<T extends { name: string }[]> = {\n [K in T[number][\"name\"]]: string;\n};\n\ntype ServerOptions<T extends FastMCPSessionAuth> = {\n authenticate?: Authenticate<T>;\n instructions?: string;\n name: string;\n ping?: {\n /**\n * Whether ping should be enabled by default.\n * - true for SSE or HTTP Stream\n * - false for stdio\n */\n enabled?: boolean;\n /**\n * Interval\n * @default 5000 (5s)\n */\n intervalMs?: number;\n /**\n * Logging level for ping-related messages.\n * @default 'debug'\n */\n logLevel?: LoggingLevel;\n };\n version: `${number}.${number}.${number}`;\n};\n\ntype Tool<\n T extends FastMCPSessionAuth,\n Params extends ToolParameters = ToolParameters,\n> = {\n annotations?: ToolAnnotations;\n description?: string;\n execute: (\n args: StandardSchemaV1.InferOutput<Params>,\n context: Context<T>,\n ) => Promise<\n AudioContent | ContentResult | ImageContent | string | TextContent\n >;\n name: string;\n parameters?: Params;\n timeoutMs?: number;\n};\n\n/**\n * Tool annotations as defined in MCP Specification (2025-03-26)\n * These provide hints about a tool's behavior.\n */\ntype ToolAnnotations = {\n /**\n * If true, the tool may perform destructive updates\n * Only meaningful when readOnlyHint is false\n * @default true\n */\n destructiveHint?: boolean;\n\n /**\n * If true, calling the tool repeatedly with the same arguments has no additional effect\n * Only meaningful when readOnlyHint is false\n * @default false\n */\n idempotentHint?: boolean;\n\n /**\n * If true, the tool may interact with an \"open world\" of external entities\n * @default true\n */\n openWorldHint?: boolean;\n\n /**\n * If true, indicates the tool does not modify its environment\n * @default false\n */\n readOnlyHint?: boolean;\n\n /**\n * A human-readable title for the tool, useful for UI display\n */\n title?: string;\n};\n\nconst FastMCPSessionEventEmitterBase: {\n new (): StrictEventEmitter<EventEmitter, FastMCPSessionEvents>;\n} = EventEmitter;\n\ntype FastMCPSessionAuth = Record<string, unknown> | undefined;\n\ntype SamplingResponse = {\n content: AudioContent | ImageContent | TextContent;\n model: string;\n role: \"assistant\" | \"user\";\n stopReason?: \"endTurn\" | \"maxTokens\" | \"stopSequence\" | string;\n};\n\nclass FastMCPSessionEventEmitter extends FastMCPSessionEventEmitterBase {}\n\nexport class FastMCPSession<\n T extends FastMCPSessionAuth = FastMCPSessionAuth,\n> extends FastMCPSessionEventEmitter {\n public get clientCapabilities(): ClientCapabilities | null {\n return this.#clientCapabilities ?? null;\n }\n public get loggingLevel(): LoggingLevel {\n return this.#loggingLevel;\n }\n public get roots(): Root[] {\n return this.#roots;\n }\n public get server(): Server {\n return this.#server;\n }\n #auth: T | undefined;\n #capabilities: ServerCapabilities = {};\n #clientCapabilities?: ClientCapabilities;\n #loggingLevel: LoggingLevel = \"info\";\n #pingConfig?: ServerOptions<T>[\"ping\"];\n #pingInterval: null | ReturnType<typeof setInterval> = null;\n\n #prompts: Prompt[] = [];\n\n #resources: Resource[] = [];\n\n #resourceTemplates: ResourceTemplate[] = [];\n\n #roots: Root[] = [];\n\n #server: Server;\n\n constructor({\n auth,\n instructions,\n name,\n ping,\n prompts,\n resources,\n resourcesTemplates,\n tools,\n version,\n }: {\n auth?: T;\n instructions?: string;\n name: string;\n ping?: ServerOptions<T>[\"ping\"];\n prompts: Prompt[];\n resources: Resource[];\n resourcesTemplates: InputResourceTemplate[];\n tools: Tool<T>[];\n version: string;\n }) {\n super();\n\n this.#auth = auth;\n this.#pingConfig = ping;\n\n if (tools.length) {\n this.#capabilities.tools = {};\n }\n\n if (resources.length || resourcesTemplates.length) {\n this.#capabilities.resources = {};\n }\n\n if (prompts.length) {\n for (const prompt of prompts) {\n this.addPrompt(prompt);\n }\n\n this.#capabilities.prompts = {};\n }\n\n this.#capabilities.logging = {};\n\n this.#server = new Server(\n { name: name, version: version },\n { capabilities: this.#capabilities, instructions: instructions },\n );\n\n this.setupErrorHandling();\n this.setupLoggingHandlers();\n this.setupRootsHandlers();\n this.setupCompleteHandlers();\n\n if (tools.length) {\n this.setupToolHandlers(tools);\n }\n\n if (resources.length || resourcesTemplates.length) {\n for (const resource of resources) {\n this.addResource(resource);\n }\n\n this.setupResourceHandlers(resources);\n\n if (resourcesTemplates.length) {\n for (const resourceTemplate of resourcesTemplates) {\n this.addResourceTemplate(resourceTemplate);\n }\n\n this.setupResourceTemplateHandlers(resourcesTemplates);\n }\n }\n\n if (prompts.length) {\n this.setupPromptHandlers(prompts);\n }\n }\n\n public async close() {\n if (this.#pingInterval) {\n clearInterval(this.#pingInterval);\n }\n\n try {\n await this.#server.close();\n } catch (error) {\n console.error(\"[FastMCP error]\", \"could not close server\", error);\n }\n }\n\n public async connect(transport: Transport) {\n if (this.#server.transport) {\n throw new UnexpectedStateError(\"Server is already connected\");\n }\n\n await this.#server.connect(transport);\n\n let attempt = 0;\n\n while (attempt++ < 10) {\n const capabilities = await this.#server.getClientCapabilities();\n\n if (capabilities) {\n this.#clientCapabilities = capabilities;\n\n break;\n }\n\n await delay(100);\n }\n\n if (!this.#clientCapabilities) {\n console.warn(\"[FastMCP warning] could not infer client capabilities\");\n }\n\n if (this.#clientCapabilities?.roots?.listChanged) {\n try {\n const roots = await this.#server.listRoots();\n this.#roots = roots.roots;\n } catch (e) {\n console.error(\n `[FastMCP error] received error listing roots.\\n\\n${e instanceof Error ? e.stack : JSON.stringify(e)}`,\n );\n }\n }\n\n if (this.#clientCapabilities) {\n const pingConfig = this.#getPingConfig(transport);\n\n if (pingConfig.enabled) {\n this.#pingInterval = setInterval(async () => {\n try {\n await this.#server.ping();\n } catch {\n // The reason we are not emitting an error here is because some clients\n // seem to not respond to the ping request, and we don't want to crash the server,\n // e.g., https://github.com/punkpeye/fastmcp/issues/38.\n const logLevel = pingConfig.logLevel;\n if (logLevel === \"debug\") {\n console.debug(\"[FastMCP debug] server ping failed\");\n } else if (logLevel === \"warning\") {\n console.warn(\n \"[FastMCP warning] server is not responding to ping\",\n );\n } else if (logLevel === \"error\") {\n console.error(\"[FastMCP error] server is not responding to ping\");\n } else {\n console.info(\"[FastMCP info] server ping failed\");\n }\n }\n }, pingConfig.intervalMs);\n }\n }\n }\n\n public async requestSampling(\n message: z.infer<typeof CreateMessageRequestSchema>[\"params\"],\n ): Promise<SamplingResponse> {\n return this.#server.createMessage(message);\n }\n\n #getPingConfig(transport: Transport): {\n enabled: boolean;\n intervalMs: number;\n logLevel: LoggingLevel;\n } {\n const pingConfig = this.#pingConfig || {};\n\n let defaultEnabled = false;\n\n if (\"type\" in transport) {\n // Enable by default for SSE and HTTP streaming\n if (transport.type === \"sse\" || transport.type === \"httpStream\") {\n defaultEnabled = true;\n }\n }\n\n return {\n enabled:\n pingConfig.enabled !== undefined ? pingConfig.enabled : defaultEnabled,\n intervalMs: pingConfig.intervalMs || 5000,\n logLevel: pingConfig.logLevel || \"debug\",\n };\n }\n\n private addPrompt(inputPrompt: InputPrompt) {\n const completers: Record<string, ArgumentValueCompleter> = {};\n const enums: Record<string, string[]> = {};\n\n for (const argument of inputPrompt.arguments ?? []) {\n if (argument.complete) {\n completers[argument.name] = argument.complete;\n }\n\n if (argument.enum) {\n enums[argument.name] = argument.enum;\n }\n }\n\n const prompt = {\n ...inputPrompt,\n complete: async (name: string, value: string) => {\n if (completers[name]) {\n return await completers[name](value);\n }\n\n if (enums[name]) {\n const fuse = new Fuse(enums[name], {\n keys: [\"value\"],\n });\n\n const result = fuse.search(value);\n\n return {\n total: result.length,\n values: result.map((item) => item.item),\n };\n }\n\n return {\n values: [],\n };\n },\n };\n\n this.#prompts.push(prompt);\n }\n\n private addResource(inputResource: Resource) {\n this.#resources.push(inputResource);\n }\n\n private addResourceTemplate(inputResourceTemplate: InputResourceTemplate) {\n const completers: Record<string, ArgumentValueCompleter> = {};\n\n for (const argument of inputResourceTemplate.arguments ?? []) {\n if (argument.complete) {\n completers[argument.name] = argument.complete;\n }\n }\n\n const resourceTemplate = {\n ...inputResourceTemplate,\n complete: async (name: string, value: string) => {\n if (completers[name]) {\n return await completers[name](value);\n }\n\n return {\n values: [],\n };\n },\n };\n\n this.#resourceTemplates.push(resourceTemplate);\n }\n\n private setupCompleteHandlers() {\n this.#server.setRequestHandler(CompleteRequestSchema, async (request) => {\n if (request.params.ref.type === \"ref/prompt\") {\n const prompt = this.#prompts.find(\n (prompt) => prompt.name === request.params.ref.name,\n );\n\n if (!prompt) {\n throw new UnexpectedStateError(\"Unknown prompt\", {\n request,\n });\n }\n\n if (!prompt.complete) {\n throw new UnexpectedStateError(\"Prompt does not support completion\", {\n request,\n });\n }\n\n const completion = CompletionZodSchema.parse(\n await prompt.complete(\n request.params.argument.name,\n request.params.argument.value,\n ),\n );\n\n return {\n completion,\n };\n }\n\n if (request.params.ref.type === \"ref/resource\") {\n const resource = this.#resourceTemplates.find(\n (resource) => resource.uriTemplate === request.params.ref.uri,\n );\n\n if (!resource) {\n throw new UnexpectedStateError(\"Unknown resource\", {\n request,\n });\n }\n\n if (!(\"uriTemplate\" in resource)) {\n throw new UnexpectedStateError(\"Unexpected resource\");\n }\n\n if (!resource.complete) {\n throw new UnexpectedStateError(\n \"Resource does not support completion\",\n {\n request,\n },\n );\n }\n\n const completion = CompletionZodSchema.parse(\n await resource.complete(\n request.params.argument.name,\n request.params.argument.value,\n ),\n );\n\n return {\n completion,\n };\n }\n\n throw new UnexpectedStateError(\"Unexpected completion request\", {\n request,\n });\n });\n }\n\n private setupErrorHandling() {\n this.#server.onerror = (error) => {\n console.error(\"[FastMCP error]\", error);\n };\n }\n\n private setupLoggingHandlers() {\n this.#server.setRequestHandler(SetLevelRequestSchema, (request) => {\n this.#loggingLevel = request.params.level;\n\n return {};\n });\n }\n\n private setupPromptHandlers(prompts: Prompt[]) {\n this.#server.setRequestHandler(ListPromptsRequestSchema, async () => {\n return {\n prompts: prompts.map((prompt) => {\n return {\n arguments: prompt.arguments,\n complete: prompt.complete,\n description: prompt.description,\n name: prompt.name,\n };\n }),\n };\n });\n\n this.#server.setRequestHandler(GetPromptRequestSchema, async (request) => {\n const prompt = prompts.find(\n (prompt) => prompt.name === request.params.name,\n );\n\n if (!prompt) {\n throw new McpError(\n ErrorCode.MethodNotFound,\n `Unknown prompt: ${request.params.name}`,\n );\n }\n\n const args = request.params.arguments;\n\n for (const arg of prompt.arguments ?? []) {\n if (arg.required && !(args && arg.name in args)) {\n throw new McpError(\n ErrorCode.InvalidRequest,\n `Missing required argument: ${arg.name}`,\n );\n }\n }\n\n let result: Awaited<ReturnType<Prompt[\"load\"]>>;\n\n try {\n result = await prompt.load(args as Record<string, string | undefined>);\n } catch (error) {\n throw new McpError(\n ErrorCode.InternalError,\n `Error loading prompt: ${error}`,\n );\n }\n\n return {\n description: prompt.description,\n messages: [\n {\n content: { text: result, type: \"text\" },\n role: \"user\",\n },\n ],\n };\n });\n }\n\n private setupResourceHandlers(resources: Resource[]) {\n this.#server.setRequestHandler(ListResourcesRequestSchema, async () => {\n return {\n resources: resources.map((resource) => {\n return {\n mimeType: resource.mimeType,\n name: resource.name,\n uri: resource.uri,\n };\n }),\n };\n });\n\n this.#server.setRequestHandler(\n ReadResourceRequestSchema,\n async (request) => {\n if (\"uri\" in request.params) {\n const resource = resources.find(\n (resource) =>\n \"uri\" in resource && resource.uri === request.params.uri,\n );\n\n if (!resource) {\n for (const resourceTemplate of this.#resourceTemplates) {\n const uriTemplate = parseURITemplate(\n resourceTemplate.uriTemplate,\n );\n\n const match = uriTemplate.fromUri(request.params.uri);\n\n if (!match) {\n continue;\n }\n\n const uri = uriTemplate.fill(match);\n\n const result = await resourceTemplate.load(match);\n\n return {\n contents: [\n {\n mimeType: resourceTemplate.mimeType,\n name: resourceTemplate.name,\n uri: uri,\n ...result,\n },\n ],\n };\n }\n\n throw new McpError(\n ErrorCode.MethodNotFound,\n `Unknown resource: ${request.params.uri}`,\n );\n }\n\n if (!(\"uri\" in resource)) {\n throw new UnexpectedStateError(\"Resource does not support reading\");\n }\n\n let maybeArrayResult: Awaited<ReturnType<Resource[\"load\"]>>;\n\n try {\n maybeArrayResult = await resource.load();\n } catch (error) {\n throw new McpError(\n ErrorCode.InternalError,\n `Error reading resource: ${error}`,\n {\n uri: resource.uri,\n },\n );\n }\n\n if (Array.isArray(maybeArrayResult)) {\n return {\n contents: maybeArrayResult.map((result) => ({\n mimeType: resource.mimeType,\n name: resource.name,\n uri: resource.uri,\n ...result,\n })),\n };\n } else {\n return {\n contents: [\n {\n mimeType: resource.mimeType,\n name: resource.name,\n uri: resource.uri,\n ...maybeArrayResult,\n },\n ],\n };\n }\n }\n\n throw new UnexpectedStateError(\"Unknown resource request\", {\n request,\n });\n },\n );\n }\n\n private setupResourceTemplateHandlers(resourceTemplates: ResourceTemplate[]) {\n this.#server.setRequestHandler(\n ListResourceTemplatesRequestSchema,\n async () => {\n return {\n resourceTemplates: resourceTemplates.map((resourceTemplate) => {\n return {\n name: resourceTemplate.name,\n uriTemplate: resourceTemplate.uriTemplate,\n };\n }),\n };\n },\n );\n }\n\n private setupRootsHandlers() {\n this.#server.setNotificationHandler(\n RootsListChangedNotificationSchema,\n () => {\n this.#server.listRoots().then((roots) => {\n this.#roots = roots.roots;\n\n this.emit(\"rootsChanged\", {\n roots: roots.roots,\n });\n });\n },\n );\n }\n\n private setupToolHandlers(tools: Tool<T>[]) {\n this.#server.setRequestHandler(ListToolsRequestSchema, async () => {\n return {\n tools: await Promise.all(\n tools.map(async (tool) => {\n return {\n annotations: tool.annotations,\n description: tool.description,\n inputSchema: tool.parameters\n ? await toJsonSchema(tool.parameters)\n : undefined,\n name: tool.name,\n };\n }),\n ),\n };\n });\n\n this.#server.setRequestHandler(CallToolRequestSchema, async (request) => {\n const tool = tools.find((tool) => tool.name === request.params.name);\n\n if (!tool) {\n throw new McpError(\n ErrorCode.MethodNotFound,\n `Unknown tool: ${request.params.name}`,\n );\n }\n\n let args: unknown = undefined;\n\n if (tool.parameters) {\n const parsed = await tool.parameters[\"~standard\"].validate(\n request.params.arguments,\n );\n\n if (parsed.issues) {\n throw new McpError(\n ErrorCode.InvalidParams,\n `Invalid ${request.params.name} parameters: ${JSON.stringify(parsed.issues)}`,\n );\n }\n\n args = parsed.value;\n }\n\n const progressToken = request.params?._meta?.progressToken;\n\n let result: ContentResult;\n\n try {\n const reportProgress = async (progress: Progress) => {\n await this.#server.notification({\n method: \"notifications/progress\",\n params: {\n ...progress,\n progressToken,\n },\n });\n };\n\n const log = {\n debug: (message: string, context?: SerializableValue) => {\n this.#server.sendLoggingMessage({\n data: {\n context,\n message,\n },\n level: \"debug\",\n });\n },\n error: (message: string, context?: SerializableValue) => {\n this.#server.sendLoggingMessage({\n data: {\n context,\n message,\n },\n level: \"error\",\n });\n },\n info: (message: string, context?: SerializableValue) => {\n this.#server.sendLoggingMessage({\n data: {\n context,\n message,\n },\n level: \"info\",\n });\n },\n warn: (message: string, context?: SerializableValue) => {\n this.#server.sendLoggingMessage({\n data: {\n context,\n message,\n },\n level: \"warning\",\n });\n },\n };\n\n // Create a promise for tool execution\n const executeToolPromise = tool.execute(args, {\n log,\n reportProgress,\n session: this.#auth,\n });\n\n // Handle timeout if specified\n const maybeStringResult = await (tool.timeoutMs\n ? Promise.race([\n executeToolPromise,\n new Promise<never>((_, reject) => {\n setTimeout(() => {\n reject(\n new UserError(\n `Tool execution timed out after ${tool.timeoutMs}ms`,\n ),\n );\n }, tool.timeoutMs);\n }),\n ])\n : executeToolPromise);\n\n if (typeof maybeStringResult === \"string\") {\n result = ContentResultZodSchema.parse({\n content: [{ text: maybeStringResult, type: \"text\" }],\n });\n } else if (\"type\" in maybeStringResult) {\n result = ContentResultZodSchema.parse({\n content: [maybeStringResult],\n });\n } else {\n result = ContentResultZodSchema.parse(maybeStringResult);\n }\n } catch (error) {\n if (error instanceof UserError) {\n return {\n content: [{ text: error.message, type: \"text\" }],\n isError: true,\n };\n }\n\n return {\n content: [{ text: `Error: ${error}`, type: \"text\" }],\n isError: true,\n };\n }\n\n return result;\n });\n }\n}\n\nconst FastMCPEventEmitterBase: {\n new (): StrictEventEmitter<EventEmitter, FastMCPEvents<FastMCPSessionAuth>>;\n} = EventEmitter;\n\ntype Authenticate<T> = (request: http.IncomingMessage) => Promise<T>;\n\nclass FastMCPEventEmitter extends FastMCPEventEmitterBase {}\n\nexport class FastMCP<\n T extends Record<string, unknown> | undefined = undefined,\n> extends FastMCPEventEmitter {\n public get sessions(): FastMCPSession<T>[] {\n return this.#sessions;\n }\n #authenticate: Authenticate<T> | undefined;\n #httpStreamServer: null | SSEServer = null;\n #options: ServerOptions<T>;\n #prompts: InputPrompt[] = [];\n #resources: Resource[] = [];\n #resourcesTemplates: InputResourceTemplate[] = [];\n #sessions: FastMCPSession<T>[] = [];\n #sseServer: null | SSEServer = null;\n\n #tools: Tool<T>[] = [];\n\n constructor(public options: ServerOptions<T>) {\n super();\n\n this.#options = options;\n this.#authenticate = options.authenticate;\n }\n\n /**\n * Adds a prompt to the server.\n */\n public addPrompt<const Args extends InputPromptArgument[]>(\n prompt: InputPrompt<Args>,\n ) {\n this.#prompts.push(prompt);\n }\n\n /**\n * Adds a resource to the server.\n */\n public addResource(resource: Resource) {\n this.#resources.push(resource);\n }\n\n /**\n * Adds a resource template to the server.\n */\n public addResourceTemplate<\n const Args extends InputResourceTemplateArgument[],\n >(resource: InputResourceTemplate<Args>) {\n this.#resourcesTemplates.push(resource);\n }\n\n /**\n * Adds a tool to the server.\n */\n public addTool<Params extends ToolParameters>(tool: Tool<T, Params>) {\n this.#tools.push(tool as unknown as Tool<T>);\n }\n\n /**\n * Starts the server.\n */\n public async start(\n options:\n | {\n httpStream: { endpoint: `/${string}`; port: number };\n transportType: \"httpStream\";\n }\n | {\n sse: { endpoint: `/${string}`; port: number };\n transportType: \"sse\";\n }\n | { transportType: \"stdio\" } = {\n transportType: \"stdio\",\n },\n ) {\n if (options.transportType === \"stdio\") {\n const transport = new StdioServerTransport();\n\n const session = new FastMCPSession<T>({\n instructions: this.#options.instructions,\n name: this.#options.name,\n ping: this.#options.ping,\n prompts: this.#prompts,\n resources: this.#resources,\n resourcesTemplates: this.#resourcesTemplates,\n tools: this.#tools,\n version: this.#options.version,\n });\n\n await session.connect(transport);\n\n this.#sessions.push(session);\n\n this.emit(\"connect\", {\n session,\n });\n } else if (options.transportType === \"sse\") {\n this.#sseServer = await startSSEServer<FastMCPSession<T>>({\n createServer: async (request) => {\n let auth: T | undefined;\n\n if (this.#authenticate) {\n auth = await this.#authenticate(request);\n }\n\n return new FastMCPSession<T>({\n auth,\n name: this.#options.name,\n ping: this.#options.ping,\n prompts: this.#prompts,\n resources: this.#resources,\n resourcesTemplates: this.#resourcesTemplates,\n tools: this.#tools,\n version: this.#options.version,\n });\n },\n endpoint: options.sse.endpoint as `/${string}`,\n onClose: (session) => {\n this.emit(\"disconnect\", {\n session,\n });\n },\n onConnect: async (session) => {\n this.#sessions.push(session);\n\n this.emit(\"connect\", {\n session,\n });\n },\n port: options.sse.port,\n });\n\n console.info(\n `[FastMCP info] server is running on SSE at http://localhost:${options.sse.port}${options.sse.endpoint}`,\n );\n } else if (options.transportType === \"httpStream\") {\n this.#httpStreamServer = await startHTTPStreamServer<FastMCPSession<T>>({\n createServer: async (request) => {\n let auth: T | undefined;\n\n if (this.#authenticate) {\n auth = await this.#authenticate(request);\n }\n\n return new FastMCPSession<T>({\n auth,\n name: this.#options.name,\n ping: this.#options.ping,\n prompts: this.#prompts,\n resources: this.#resources,\n resourcesTemplates: this.#resourcesTemplates,\n tools: this.#tools,\n version: this.#options.version,\n });\n },\n endpoint: options.httpStream.endpoint as `/${string}`,\n onClose: (session) => {\n this.emit(\"disconnect\", {\n session,\n });\n },\n onConnect: async (session) => {\n this.#sessions.push(session);\n\n this.emit(\"connect\", {\n session,\n });\n },\n port: options.httpStream.port,\n });\n\n console.info(\n `[FastMCP info] server is running on HTTP Stream at http://localhost:${options.httpStream.port}${options.httpStream.endpoint}`,\n );\n } else {\n throw new Error(\"Invalid transport type\");\n }\n }\n\n /**\n * Stops the server.\n */\n public async stop() {\n if (this.#sseServer) {\n await this.#sseServer.close();\n }\n if (this.#httpStreamServer) {\n await this.#httpStreamServer.close();\n }\n }\n}\n\nexport type { Context };\nexport type { Tool, ToolParameters };\nexport type { Content, ContentResult, ImageContent, TextContent };\nexport type { Progress, SerializableValue };\nexport type { Resource, ResourceResult };\nexport type { ResourceTemplate, ResourceTemplateArgument };\nexport type { Prompt, PromptArgument };\nexport type { InputPrompt, InputPromptArgument };\nexport type { LoggingLevel, ServerOptions };\nexport type { FastMCPEvents, FastMCPSessionEvents };\n"],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,4BAA4B;AAErC;AAAA,EAEE;AAAA,EAEA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EAEA;AAAA,OACK;AAEP,SAAS,oBAAoB;AAC7B,SAAS,0BAA0B;AACnC,SAAS,gBAAgB;AACzB,OAAO,UAAU;AAEjB,SAAS,uBAAuB,sBAAsB;AAEtD,SAAS,cAAc,aAAa;AACpC,SAAS,aAAa;AACtB,OAAO,sBAAsB;AAC7B,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAgBX,IAAM,eAAe,OAC1B,UAC0B;AAC1B,MAAI;AAEJ,MAAI;AACF,QAAI,SAAS,OAAO;AAClB,UAAI;AACF,cAAM,WAAW,MAAM,MAAM,MAAM,GAAG;AAEtC,YAAI,CAAC,SAAS,IAAI;AAChB,gBAAM,IAAI;AAAA,YACR,iCAAiC,SAAS,MAAM,MAAM,SAAS,UAAU;AAAA,UAC3E;AAAA,QACF;AAEA,kBAAU,OAAO,KAAK,MAAM,SAAS,YAAY,CAAC;AAAA,MACpD,SAAS,OAAO;AACd,cAAM,IAAI;AAAA,UACR,mCAAmC,MAAM,GAAG,MAAM,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;AAAA,QAC1G;AAAA,MACF;AAAA,IACF,WAAW,UAAU,OAAO;AAC1B,UAAI;AACF,kBAAU,MAAM,SAAS,MAAM,IAAI;AAAA,MACrC,SAAS,OAAO;AACd,cAAM,IAAI;AAAA,UACR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S;AAAA,UACzB,CAAC;AAAA,QACH;AAAA,QACA,UAAU,QAAQ,WAAW;AAAA,QAC7B,SAAS,CAAC,YAAY;AACpB,eAAK,KAAK,cAAc;AAAA,YACtB;AAAA,UACF,CAAC;AAAA,QACH;AAAA,QACA,WAAW,OAAO,YAAY;AAC5B,eAAK,UAAU,KAAK,OAAO;AAE3B,eAAK,KAAK,WAAW;AAAA,YACnB;AAAA,UACF,CAAC;AAAA,QACH;AAAA,QACA,MAAM,QAAQ,WAAW;AAAA,MAC3B,CAAC;AAED,cAAQ;AAAA,QACN,uEAAuE,QAAQ,WAAW,IAAI,GAAG,QAAQ,WAAW,QAAQ;AAAA,MAC9H;AAAA,IACF,OAAO;AACL,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,MAAa,OAAO;AAClB,QAAI,KAAK,YAAY;AACnB,YAAM,KAAK,WAAW,MAAM;AAAA,IAC9B;AACA,QAAI,KAAK,mBAAmB;AAC1B,YAAM,KAAK,kBAAkB,MAAM;AAAA,IACrC;AAAA,EACF;AACF;","names":["prompt","resource","tool"]}
|
package/jsr.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fastmcp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.26.0",
|
|
4
4
|
"main": "dist/FastMCP.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "tsup",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"execa": "^9.5.2",
|
|
27
27
|
"file-type": "^20.4.1",
|
|
28
28
|
"fuse.js": "^7.1.0",
|
|
29
|
-
"mcp-proxy": "^2.
|
|
29
|
+
"mcp-proxy": "^2.13.1",
|
|
30
30
|
"strict-event-emitter-types": "^2.0.0",
|
|
31
31
|
"undici": "^7.8.0",
|
|
32
32
|
"uri-templates": "^0.2.0",
|
package/src/FastMCP.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
2
|
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
3
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
3
4
|
import {
|
|
4
5
|
CreateMessageRequestSchema,
|
|
5
6
|
ErrorCode,
|
|
@@ -1003,6 +1004,17 @@ test("session listens to roots changes", async () => {
|
|
|
1003
1004
|
|
|
1004
1005
|
test("session sends pings to the client", async () => {
|
|
1005
1006
|
await runWithTestServer({
|
|
1007
|
+
server: async () => {
|
|
1008
|
+
const server = new FastMCP({
|
|
1009
|
+
name: "Test",
|
|
1010
|
+
version: "1.0.0",
|
|
1011
|
+
ping: {
|
|
1012
|
+
enabled: true,
|
|
1013
|
+
intervalMs: 1000,
|
|
1014
|
+
},
|
|
1015
|
+
});
|
|
1016
|
+
return server;
|
|
1017
|
+
},
|
|
1006
1018
|
run: async ({ client }) => {
|
|
1007
1019
|
const onPing = vi.fn().mockReturnValue({});
|
|
1008
1020
|
|
|
@@ -1010,7 +1022,8 @@ test("session sends pings to the client", async () => {
|
|
|
1010
1022
|
|
|
1011
1023
|
await delay(2000);
|
|
1012
1024
|
|
|
1013
|
-
expect(onPing).
|
|
1025
|
+
expect(onPing.mock.calls.length).toBeGreaterThanOrEqual(1);
|
|
1026
|
+
expect(onPing.mock.calls.length).toBeLessThanOrEqual(3);
|
|
1014
1027
|
},
|
|
1015
1028
|
});
|
|
1016
1029
|
});
|
|
@@ -1353,7 +1366,7 @@ test("throws ErrorCode.InvalidParams if tool parameters do not match zod schema"
|
|
|
1353
1366
|
|
|
1354
1367
|
// @ts-expect-error - we know that error is an McpError
|
|
1355
1368
|
expect(error.message).toBe(
|
|
1356
|
-
|
|
1369
|
+
'MCP error -32602: MCP error -32602: Invalid add parameters: [{"code":"invalid_type","expected":"number","received":"string","path":["b"],"message":"Expected number, received string"}]',
|
|
1357
1370
|
);
|
|
1358
1371
|
}
|
|
1359
1372
|
},
|
|
@@ -1399,7 +1412,7 @@ test("server remains usable after InvalidParams error", async () => {
|
|
|
1399
1412
|
|
|
1400
1413
|
// @ts-expect-error - we know that error is an McpError
|
|
1401
1414
|
expect(error.message).toBe(
|
|
1402
|
-
|
|
1415
|
+
'MCP error -32602: MCP error -32602: Invalid add parameters: [{"code":"invalid_type","expected":"number","received":"string","path":["b"],"message":"Expected number, received string"}]',
|
|
1403
1416
|
);
|
|
1404
1417
|
}
|
|
1405
1418
|
|
|
@@ -1747,3 +1760,83 @@ test("blocks unauthorized requests", async () => {
|
|
|
1747
1760
|
await client.connect(transport);
|
|
1748
1761
|
}).rejects.toThrow("SSE error: Non-200 status code (401)");
|
|
1749
1762
|
});
|
|
1763
|
+
|
|
1764
|
+
// We now use a direct approach for testing HTTP Stream functionality
|
|
1765
|
+
// rather than a helper function
|
|
1766
|
+
|
|
1767
|
+
// Set longer timeout for HTTP Stream tests
|
|
1768
|
+
test("HTTP Stream: calls a tool", { timeout: 20000 }, async () => {
|
|
1769
|
+
console.log("Starting HTTP Stream test...");
|
|
1770
|
+
const port = await getRandomPort();
|
|
1771
|
+
|
|
1772
|
+
// Create server directly (don't use helper function)
|
|
1773
|
+
const server = new FastMCP({
|
|
1774
|
+
name: "Test",
|
|
1775
|
+
version: "1.0.0",
|
|
1776
|
+
});
|
|
1777
|
+
|
|
1778
|
+
server.addTool({
|
|
1779
|
+
description: "Add two numbers",
|
|
1780
|
+
execute: async (args) => {
|
|
1781
|
+
return String(args.a + args.b);
|
|
1782
|
+
},
|
|
1783
|
+
name: "add",
|
|
1784
|
+
parameters: z.object({
|
|
1785
|
+
a: z.number(),
|
|
1786
|
+
b: z.number(),
|
|
1787
|
+
}),
|
|
1788
|
+
});
|
|
1789
|
+
|
|
1790
|
+
await server.start({
|
|
1791
|
+
httpStream: {
|
|
1792
|
+
endpoint: "/httpStream",
|
|
1793
|
+
port,
|
|
1794
|
+
},
|
|
1795
|
+
transportType: "httpStream",
|
|
1796
|
+
});
|
|
1797
|
+
|
|
1798
|
+
try {
|
|
1799
|
+
// Create client
|
|
1800
|
+
const client = new Client(
|
|
1801
|
+
{
|
|
1802
|
+
name: "example-client",
|
|
1803
|
+
version: "1.0.0",
|
|
1804
|
+
},
|
|
1805
|
+
{
|
|
1806
|
+
capabilities: {},
|
|
1807
|
+
},
|
|
1808
|
+
);
|
|
1809
|
+
|
|
1810
|
+
// IMPORTANT: Don't provide sessionId manually with HTTP streaming
|
|
1811
|
+
// The server will generate a session ID automatically
|
|
1812
|
+
const transport = new StreamableHTTPClientTransport(
|
|
1813
|
+
new URL(`http://localhost:${port}/httpStream`),
|
|
1814
|
+
);
|
|
1815
|
+
|
|
1816
|
+
// Connect client to server
|
|
1817
|
+
await client.connect(transport);
|
|
1818
|
+
|
|
1819
|
+
// Wait a bit to ensure connection is established
|
|
1820
|
+
await delay(1000);
|
|
1821
|
+
|
|
1822
|
+
// Call tool
|
|
1823
|
+
const result = await client.callTool({
|
|
1824
|
+
arguments: {
|
|
1825
|
+
a: 1,
|
|
1826
|
+
b: 2,
|
|
1827
|
+
},
|
|
1828
|
+
name: "add",
|
|
1829
|
+
});
|
|
1830
|
+
|
|
1831
|
+
// Check result
|
|
1832
|
+
expect(result).toEqual({
|
|
1833
|
+
content: [{ text: "3", type: "text" }],
|
|
1834
|
+
});
|
|
1835
|
+
|
|
1836
|
+
// Clean up connection
|
|
1837
|
+
await transport.terminateSession();
|
|
1838
|
+
await client.close();
|
|
1839
|
+
} finally {
|
|
1840
|
+
await server.stop();
|
|
1841
|
+
}
|
|
1842
|
+
});
|
package/src/FastMCP.ts
CHANGED
|
@@ -26,7 +26,7 @@ import { fileTypeFromBuffer } from "file-type";
|
|
|
26
26
|
import { readFile } from "fs/promises";
|
|
27
27
|
import Fuse from "fuse.js";
|
|
28
28
|
import http from "http";
|
|
29
|
-
import { startSSEServer } from "mcp-proxy";
|
|
29
|
+
import { startHTTPStreamServer, startSSEServer } from "mcp-proxy";
|
|
30
30
|
import { StrictEventEmitter } from "strict-event-emitter-types";
|
|
31
31
|
import { setTimeout as delay } from "timers/promises";
|
|
32
32
|
import { fetch } from "undici";
|
|
@@ -430,6 +430,24 @@ type ServerOptions<T extends FastMCPSessionAuth> = {
|
|
|
430
430
|
authenticate?: Authenticate<T>;
|
|
431
431
|
instructions?: string;
|
|
432
432
|
name: string;
|
|
433
|
+
ping?: {
|
|
434
|
+
/**
|
|
435
|
+
* Whether ping should be enabled by default.
|
|
436
|
+
* - true for SSE or HTTP Stream
|
|
437
|
+
* - false for stdio
|
|
438
|
+
*/
|
|
439
|
+
enabled?: boolean;
|
|
440
|
+
/**
|
|
441
|
+
* Interval
|
|
442
|
+
* @default 5000 (5s)
|
|
443
|
+
*/
|
|
444
|
+
intervalMs?: number;
|
|
445
|
+
/**
|
|
446
|
+
* Logging level for ping-related messages.
|
|
447
|
+
* @default 'debug'
|
|
448
|
+
*/
|
|
449
|
+
logLevel?: LoggingLevel;
|
|
450
|
+
};
|
|
433
451
|
version: `${number}.${number}.${number}`;
|
|
434
452
|
};
|
|
435
453
|
|
|
@@ -521,6 +539,7 @@ export class FastMCPSession<
|
|
|
521
539
|
#capabilities: ServerCapabilities = {};
|
|
522
540
|
#clientCapabilities?: ClientCapabilities;
|
|
523
541
|
#loggingLevel: LoggingLevel = "info";
|
|
542
|
+
#pingConfig?: ServerOptions<T>["ping"];
|
|
524
543
|
#pingInterval: null | ReturnType<typeof setInterval> = null;
|
|
525
544
|
|
|
526
545
|
#prompts: Prompt[] = [];
|
|
@@ -537,6 +556,7 @@ export class FastMCPSession<
|
|
|
537
556
|
auth,
|
|
538
557
|
instructions,
|
|
539
558
|
name,
|
|
559
|
+
ping,
|
|
540
560
|
prompts,
|
|
541
561
|
resources,
|
|
542
562
|
resourcesTemplates,
|
|
@@ -546,6 +566,7 @@ export class FastMCPSession<
|
|
|
546
566
|
auth?: T;
|
|
547
567
|
instructions?: string;
|
|
548
568
|
name: string;
|
|
569
|
+
ping?: ServerOptions<T>["ping"];
|
|
549
570
|
prompts: Prompt[];
|
|
550
571
|
resources: Resource[];
|
|
551
572
|
resourcesTemplates: InputResourceTemplate[];
|
|
@@ -555,6 +576,7 @@ export class FastMCPSession<
|
|
|
555
576
|
super();
|
|
556
577
|
|
|
557
578
|
this.#auth = auth;
|
|
579
|
+
this.#pingConfig = ping;
|
|
558
580
|
|
|
559
581
|
if (tools.length) {
|
|
560
582
|
this.#capabilities.tools = {};
|
|
@@ -658,16 +680,31 @@ export class FastMCPSession<
|
|
|
658
680
|
}
|
|
659
681
|
|
|
660
682
|
if (this.#clientCapabilities) {
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
683
|
+
const pingConfig = this.#getPingConfig(transport);
|
|
684
|
+
|
|
685
|
+
if (pingConfig.enabled) {
|
|
686
|
+
this.#pingInterval = setInterval(async () => {
|
|
687
|
+
try {
|
|
688
|
+
await this.#server.ping();
|
|
689
|
+
} catch {
|
|
690
|
+
// The reason we are not emitting an error here is because some clients
|
|
691
|
+
// seem to not respond to the ping request, and we don't want to crash the server,
|
|
692
|
+
// e.g., https://github.com/punkpeye/fastmcp/issues/38.
|
|
693
|
+
const logLevel = pingConfig.logLevel;
|
|
694
|
+
if (logLevel === "debug") {
|
|
695
|
+
console.debug("[FastMCP debug] server ping failed");
|
|
696
|
+
} else if (logLevel === "warning") {
|
|
697
|
+
console.warn(
|
|
698
|
+
"[FastMCP warning] server is not responding to ping",
|
|
699
|
+
);
|
|
700
|
+
} else if (logLevel === "error") {
|
|
701
|
+
console.error("[FastMCP error] server is not responding to ping");
|
|
702
|
+
} else {
|
|
703
|
+
console.info("[FastMCP info] server ping failed");
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
}, pingConfig.intervalMs);
|
|
707
|
+
}
|
|
671
708
|
}
|
|
672
709
|
}
|
|
673
710
|
|
|
@@ -677,6 +714,30 @@ export class FastMCPSession<
|
|
|
677
714
|
return this.#server.createMessage(message);
|
|
678
715
|
}
|
|
679
716
|
|
|
717
|
+
#getPingConfig(transport: Transport): {
|
|
718
|
+
enabled: boolean;
|
|
719
|
+
intervalMs: number;
|
|
720
|
+
logLevel: LoggingLevel;
|
|
721
|
+
} {
|
|
722
|
+
const pingConfig = this.#pingConfig || {};
|
|
723
|
+
|
|
724
|
+
let defaultEnabled = false;
|
|
725
|
+
|
|
726
|
+
if ("type" in transport) {
|
|
727
|
+
// Enable by default for SSE and HTTP streaming
|
|
728
|
+
if (transport.type === "sse" || transport.type === "httpStream") {
|
|
729
|
+
defaultEnabled = true;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
return {
|
|
734
|
+
enabled:
|
|
735
|
+
pingConfig.enabled !== undefined ? pingConfig.enabled : defaultEnabled,
|
|
736
|
+
intervalMs: pingConfig.intervalMs || 5000,
|
|
737
|
+
logLevel: pingConfig.logLevel || "debug",
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
|
|
680
741
|
private addPrompt(inputPrompt: InputPrompt) {
|
|
681
742
|
const completers: Record<string, ArgumentValueCompleter> = {};
|
|
682
743
|
const enums: Record<string, string[]> = {};
|
|
@@ -1198,6 +1259,7 @@ export class FastMCP<
|
|
|
1198
1259
|
return this.#sessions;
|
|
1199
1260
|
}
|
|
1200
1261
|
#authenticate: Authenticate<T> | undefined;
|
|
1262
|
+
#httpStreamServer: null | SSEServer = null;
|
|
1201
1263
|
#options: ServerOptions<T>;
|
|
1202
1264
|
#prompts: InputPrompt[] = [];
|
|
1203
1265
|
#resources: Resource[] = [];
|
|
@@ -1251,6 +1313,10 @@ export class FastMCP<
|
|
|
1251
1313
|
*/
|
|
1252
1314
|
public async start(
|
|
1253
1315
|
options:
|
|
1316
|
+
| {
|
|
1317
|
+
httpStream: { endpoint: `/${string}`; port: number };
|
|
1318
|
+
transportType: "httpStream";
|
|
1319
|
+
}
|
|
1254
1320
|
| {
|
|
1255
1321
|
sse: { endpoint: `/${string}`; port: number };
|
|
1256
1322
|
transportType: "sse";
|
|
@@ -1265,6 +1331,7 @@ export class FastMCP<
|
|
|
1265
1331
|
const session = new FastMCPSession<T>({
|
|
1266
1332
|
instructions: this.#options.instructions,
|
|
1267
1333
|
name: this.#options.name,
|
|
1334
|
+
ping: this.#options.ping,
|
|
1268
1335
|
prompts: this.#prompts,
|
|
1269
1336
|
resources: this.#resources,
|
|
1270
1337
|
resourcesTemplates: this.#resourcesTemplates,
|
|
@@ -1291,6 +1358,7 @@ export class FastMCP<
|
|
|
1291
1358
|
return new FastMCPSession<T>({
|
|
1292
1359
|
auth,
|
|
1293
1360
|
name: this.#options.name,
|
|
1361
|
+
ping: this.#options.ping,
|
|
1294
1362
|
prompts: this.#prompts,
|
|
1295
1363
|
resources: this.#resources,
|
|
1296
1364
|
resourcesTemplates: this.#resourcesTemplates,
|
|
@@ -1317,6 +1385,45 @@ export class FastMCP<
|
|
|
1317
1385
|
console.info(
|
|
1318
1386
|
`[FastMCP info] server is running on SSE at http://localhost:${options.sse.port}${options.sse.endpoint}`,
|
|
1319
1387
|
);
|
|
1388
|
+
} else if (options.transportType === "httpStream") {
|
|
1389
|
+
this.#httpStreamServer = await startHTTPStreamServer<FastMCPSession<T>>({
|
|
1390
|
+
createServer: async (request) => {
|
|
1391
|
+
let auth: T | undefined;
|
|
1392
|
+
|
|
1393
|
+
if (this.#authenticate) {
|
|
1394
|
+
auth = await this.#authenticate(request);
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
return new FastMCPSession<T>({
|
|
1398
|
+
auth,
|
|
1399
|
+
name: this.#options.name,
|
|
1400
|
+
ping: this.#options.ping,
|
|
1401
|
+
prompts: this.#prompts,
|
|
1402
|
+
resources: this.#resources,
|
|
1403
|
+
resourcesTemplates: this.#resourcesTemplates,
|
|
1404
|
+
tools: this.#tools,
|
|
1405
|
+
version: this.#options.version,
|
|
1406
|
+
});
|
|
1407
|
+
},
|
|
1408
|
+
endpoint: options.httpStream.endpoint as `/${string}`,
|
|
1409
|
+
onClose: (session) => {
|
|
1410
|
+
this.emit("disconnect", {
|
|
1411
|
+
session,
|
|
1412
|
+
});
|
|
1413
|
+
},
|
|
1414
|
+
onConnect: async (session) => {
|
|
1415
|
+
this.#sessions.push(session);
|
|
1416
|
+
|
|
1417
|
+
this.emit("connect", {
|
|
1418
|
+
session,
|
|
1419
|
+
});
|
|
1420
|
+
},
|
|
1421
|
+
port: options.httpStream.port,
|
|
1422
|
+
});
|
|
1423
|
+
|
|
1424
|
+
console.info(
|
|
1425
|
+
`[FastMCP info] server is running on HTTP Stream at http://localhost:${options.httpStream.port}${options.httpStream.endpoint}`,
|
|
1426
|
+
);
|
|
1320
1427
|
} else {
|
|
1321
1428
|
throw new Error("Invalid transport type");
|
|
1322
1429
|
}
|
|
@@ -1327,7 +1434,10 @@ export class FastMCP<
|
|
|
1327
1434
|
*/
|
|
1328
1435
|
public async stop() {
|
|
1329
1436
|
if (this.#sseServer) {
|
|
1330
|
-
this.#sseServer.close();
|
|
1437
|
+
await this.#sseServer.close();
|
|
1438
|
+
}
|
|
1439
|
+
if (this.#httpStreamServer) {
|
|
1440
|
+
await this.#httpStreamServer.close();
|
|
1331
1441
|
}
|
|
1332
1442
|
}
|
|
1333
1443
|
}
|
package/src/examples/addition.ts
CHANGED
|
@@ -2,13 +2,18 @@ import { type } from "arktype";
|
|
|
2
2
|
import * as v from "valibot";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* This is a complete example of an MCP server.
|
|
7
|
-
*/
|
|
8
5
|
import { FastMCP } from "../FastMCP.js";
|
|
9
6
|
|
|
10
7
|
const server = new FastMCP({
|
|
11
8
|
name: "Addition",
|
|
9
|
+
ping: {
|
|
10
|
+
// enabled: undefined,
|
|
11
|
+
// Automatically enabled/disabled based on transport type
|
|
12
|
+
// Using a longer interval to reduce log noise
|
|
13
|
+
intervalMs: 10000, // default is 5000ms
|
|
14
|
+
// Reduce log verbosity
|
|
15
|
+
logLevel: "debug", // default
|
|
16
|
+
},
|
|
12
17
|
version: "1.0.0",
|
|
13
18
|
});
|
|
14
19
|
|
|
@@ -118,6 +123,61 @@ server.addPrompt({
|
|
|
118
123
|
name: "git-commit",
|
|
119
124
|
});
|
|
120
125
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
126
|
+
// Select transport type based on command line arguments
|
|
127
|
+
const transportType = process.argv.includes("--http-stream")
|
|
128
|
+
? "httpStream"
|
|
129
|
+
: "stdio";
|
|
130
|
+
|
|
131
|
+
if (transportType === "httpStream") {
|
|
132
|
+
// Start with HTTP streaming transport
|
|
133
|
+
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 8080;
|
|
134
|
+
|
|
135
|
+
server.start({
|
|
136
|
+
httpStream: {
|
|
137
|
+
endpoint: "/stream",
|
|
138
|
+
port: PORT,
|
|
139
|
+
},
|
|
140
|
+
transportType: "httpStream",
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
console.log(
|
|
144
|
+
`HTTP Stream MCP server is running at http://localhost:${PORT}/stream`,
|
|
145
|
+
);
|
|
146
|
+
console.log("Use StreamableHTTPClientTransport to connect to this server");
|
|
147
|
+
console.log("For example:");
|
|
148
|
+
console.log(`
|
|
149
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
150
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
151
|
+
|
|
152
|
+
const client = new Client(
|
|
153
|
+
{
|
|
154
|
+
name: "example-client",
|
|
155
|
+
version: "1.0.0",
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
capabilities: {},
|
|
159
|
+
},
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
const transport = new StreamableHTTPClientTransport(
|
|
163
|
+
new URL("http://localhost:${PORT}/stream"),
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
await client.connect(transport);
|
|
167
|
+
`);
|
|
168
|
+
} else if (process.argv.includes("--explicit-ping-config")) {
|
|
169
|
+
server.start({
|
|
170
|
+
transportType: "stdio",
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
console.log(
|
|
174
|
+
"Started stdio transport with explicit ping configuration from server options",
|
|
175
|
+
);
|
|
176
|
+
} else {
|
|
177
|
+
// Disable by default for:
|
|
178
|
+
server.start({
|
|
179
|
+
transportType: "stdio",
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
console.log("Started stdio transport with ping disabled by default");
|
|
183
|
+
}
|