forked from punkpeye/fastmcp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddition.ts
More file actions
333 lines (296 loc) · 9.06 KB
/
Copy pathaddition.ts
File metadata and controls
333 lines (296 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* Example FastMCP server demonstrating core functionality plus streaming output.
*
* Features demonstrated:
* - Basic tool with type-safe parameters
* - Streaming-enabled tool for incremental output
* - Advanced tool annotations
*
* For a complete project template, see https://github.com/punkpeye/fastmcp-boilerplate
*/
import { type } from "arktype";
import * as v from "valibot";
import { z } from "zod";
import { FastMCP } from "../FastMCP.js";
const server = new FastMCP({
name: "Addition",
ping: {
// enabled: undefined,
// Automatically enabled/disabled based on transport type
// Using a longer interval to reduce log noise
intervalMs: 10000, // default is 5000ms
// Reduce log verbosity
logLevel: "debug", // default
},
roots: {
// You can explicitly disable roots support if needed
// enabled: false,
},
version: "1.0.0",
});
// --- Zod Example ---
const AddParamsZod = z.object({
a: z.number().describe("The first number"),
b: z.number().describe("The second number"),
});
server.addTool({
annotations: {
openWorldHint: false, // This tool doesn't interact with external systems
readOnlyHint: true, // This tool doesn't modify anything
title: "Addition (Zod)",
},
description: "Add two numbers (using Zod schema)",
execute: async (args) => {
// args is typed as { a: number, b: number }
console.log(`[Zod] Adding ${args.a} and ${args.b}`);
return String(args.a + args.b);
},
name: "add-zod",
parameters: AddParamsZod,
});
// --- ArkType Example ---
const AddParamsArkType = type({
a: "number",
b: "number",
});
server.addTool({
annotations: {
destructiveHint: true, // This would perform destructive operations
idempotentHint: true, // But operations can be repeated safely
openWorldHint: true, // Interacts with external systems
readOnlyHint: false, // Example showing a modifying tool
title: "Addition (ArkType)",
},
description: "Add two numbers (using ArkType schema)",
execute: async (args, { log }) => {
// args is typed as { a: number, b: number } based on AddParamsArkType.infer
console.log(`[ArkType] Adding ${args.a} and ${args.b}`);
// Demonstrate long-running operation that might need a timeout
log.info("Starting calculation with potential delay...");
// Simulate a complex calculation process
if (args.a > 1000 || args.b > 1000) {
log.warn("Large numbers detected, operation might take longer");
// In a real implementation, this delay might be a slow operation
await new Promise((resolve) => setTimeout(resolve, 3000));
}
return String(args.a + args.b);
},
name: "add-arktype",
parameters: AddParamsArkType,
// Will abort execution after 2s
timeoutMs: 2000,
});
// --- Valibot Example ---
const AddParamsValibot = v.object({
a: v.number("The first number"),
b: v.number("The second number"),
});
server.addTool({
annotations: {
openWorldHint: false,
readOnlyHint: true,
title: "Addition (Valibot)",
},
description: "Add two numbers (using Valibot schema)",
execute: async (args) => {
console.log(`[Valibot] Adding ${args.a} and ${args.b}`);
return String(args.a + args.b);
},
name: "add-valibot",
parameters: AddParamsValibot,
});
server.addResource({
async load() {
return {
text: "Example log content",
};
},
mimeType: "text/plain",
name: "Application Logs",
uri: "file:///logs/app.log",
});
server.addTool({
annotations: {
openWorldHint: false,
readOnlyHint: true,
streamingHint: true,
},
description: "Generate a poem line by line with streaming output",
execute: async (args, context) => {
const { theme } = args;
const lines = [
`Poem about ${theme} - line 1`,
`Poem about ${theme} - line 2`,
`Poem about ${theme} - line 3`,
`Poem about ${theme} - line 4`,
];
for (const line of lines) {
await context.streamContent({
text: line,
type: "text",
});
await new Promise((resolve) => setTimeout(resolve, 1000));
}
return;
},
name: "stream-poem",
parameters: z.object({
theme: z.string().describe("Theme for the poem"),
}),
});
server.addTool({
annotations: {
openWorldHint: false,
readOnlyHint: false,
},
description: "Test progress reporting without buffering delays",
execute: async (args, { reportProgress }) => {
console.log("Testing progress reporting fix for HTTP Stream buffering...");
await reportProgress({ progress: 0, total: 100 });
await new Promise((resolve) => setTimeout(resolve, 500));
await reportProgress({ progress: 25, total: 100 });
await new Promise((resolve) => setTimeout(resolve, 500));
await reportProgress({ progress: 75, total: 100 });
await new Promise((resolve) => setTimeout(resolve, 500));
// This progress should be received immediately
await reportProgress({ progress: 100, total: 100 });
return `Buffering test completed for ${args.testCase}`;
},
name: "test-buffering-fix",
parameters: z.object({
testCase: z.string().describe("Test case description"),
}),
});
server.addPrompt({
arguments: [
{
description: "Git diff or description of changes",
name: "changes",
required: true,
},
],
description: "Generate a Git commit message",
load: async (args) => {
return `Generate a concise but descriptive commit message for these changes:\n\n${args.changes}`;
},
name: "git-commit",
});
server.addResourceTemplate({
arguments: [
{
description: "Documentation section to retrieve",
name: "section",
required: true,
},
],
description: "Get project documentation",
load: async (args) => {
const docs = {
"api-reference":
"# API Reference\n\n## Authentication\nAll API requests require a valid API key in the Authorization header.\n\n## Endpoints\n- GET /users - List all users\n- POST /users - Create new user",
deployment:
"# Deployment Guide\n\nTo deploy this application:\n\n1. Build the project: `npm run build`\n2. Set environment variables\n3. Deploy to your hosting platform",
"getting-started":
"# Getting Started\n\nWelcome to our project! Follow these steps to set up your development environment:\n\n1. Clone the repository\n2. Install dependencies with `npm install`\n3. Run `npm start` to begin",
};
return {
text:
docs[args.section as keyof typeof docs] ||
"Documentation section not found",
};
},
mimeType: "text/markdown",
name: "Project Documentation",
uriTemplate: "docs://project/{section}",
});
server.addTool({
annotations: {
openWorldHint: false,
readOnlyHint: true,
title: "Get Documentation (Embedded)",
},
description:
"Retrieve project documentation using embedded resources - demonstrates the new embedded() feature",
execute: async (args) => {
return {
content: [
{
resource: await server.embedded(`docs://project/${args.section}`),
type: "resource",
},
],
};
},
name: "get-documentation",
parameters: z.object({
section: z
.enum(["getting-started", "api-reference", "deployment"])
.describe("Documentation section to retrieve"),
}),
});
// Select transport type based on command line arguments
const transportType = process.argv.includes("--http-stream")
? "httpStream"
: "stdio";
if (transportType === "httpStream") {
// Start with HTTP streaming transport
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 8080;
server.start({
httpStream: {
port: PORT,
},
transportType: "httpStream",
});
console.log(
`HTTP Stream MCP server is running at http://localhost:${PORT}/mcp`,
);
console.log("Use StreamableHTTPClientTransport to connect to this server");
console.log("For example:");
console.log(`
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
const client = new Client(
{
name: "example-client",
version: "1.0.0",
},
{
capabilities: {},
},
);
const transport = new StreamableHTTPClientTransport(
new URL("http://localhost:${PORT}/mcp"),
);
await client.connect(transport);
`);
} else if (process.argv.includes("--explicit-ping-config")) {
server.start({
transportType: "stdio",
});
console.log(
"Started stdio transport with explicit ping configuration from server options",
);
} else if (process.argv.includes("--disable-roots")) {
// Example of disabling roots at runtime
const serverWithDisabledRoots = new FastMCP({
name: "Addition (No Roots)",
ping: {
intervalMs: 10000,
logLevel: "debug",
},
roots: {
enabled: false,
},
version: "1.0.0",
});
serverWithDisabledRoots.start({
transportType: "stdio",
});
console.log("Started stdio transport with roots support disabled");
} else {
// Disable by default for:
server.start({
transportType: "stdio",
});
console.log("Started stdio transport with ping disabled by default");
}