mcp-use 0.2.0 → 1.0.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/dist/chunk-2HFIPY7C.js +429 -0
- package/dist/chunk-4DEFXVWT.js +680 -0
- package/dist/chunk-JXLQRAW2.js +532 -0
- package/dist/chunk-SHUYVCID.js +6 -0
- package/dist/chunk-YUSC6R6V.js +299 -0
- package/dist/index.cjs +5762 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3767 -22
- package/dist/langfuse-YA2S23SM.js +13 -0
- package/dist/src/agents/remote.d.ts.map +1 -1
- package/dist/src/agents/utils/ai_sdk.d.ts.map +1 -1
- package/dist/src/auth/browser-provider.d.ts +52 -0
- package/dist/src/auth/browser-provider.d.ts.map +1 -0
- package/dist/src/auth/callback.d.ts +6 -0
- package/dist/src/auth/callback.d.ts.map +1 -0
- package/dist/src/auth/index.d.ts +7 -0
- package/dist/src/auth/index.d.ts.map +1 -0
- package/dist/src/auth/types.d.ts +18 -0
- package/dist/src/auth/types.d.ts.map +1 -0
- package/dist/src/browser.cjs +323 -0
- package/dist/src/browser.d.ts +5 -46
- package/dist/src/browser.d.ts.map +1 -1
- package/dist/src/browser.js +9 -75
- package/dist/src/oauth-helper.d.ts +2 -12
- package/dist/src/oauth-helper.d.ts.map +1 -1
- package/dist/src/react/index.cjs +986 -0
- package/dist/src/react/index.d.ts +9 -0
- package/dist/src/react/index.d.ts.map +1 -0
- package/dist/src/react/index.js +11 -0
- package/dist/src/react/types.d.ts +139 -0
- package/dist/src/react/types.d.ts.map +1 -0
- package/dist/src/react/useMcp.d.ts +3 -0
- package/dist/src/react/useMcp.d.ts.map +1 -0
- package/dist/src/server/index.cjs +566 -0
- package/dist/src/server/index.d.ts +3 -0
- package/dist/src/server/index.d.ts.map +1 -0
- package/dist/src/server/index.js +9 -0
- package/dist/src/server/logging.d.ts +16 -0
- package/dist/src/server/logging.d.ts.map +1 -0
- package/dist/src/server/mcp-server.d.ts +282 -0
- package/dist/src/server/mcp-server.d.ts.map +1 -0
- package/dist/src/server/types.d.ts +47 -0
- package/dist/src/server/types.d.ts.map +1 -0
- package/dist/src/utils/assert.d.ts +8 -0
- package/dist/src/utils/assert.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +67 -40
- package/dist/src/adapters/base.js +0 -124
- package/dist/src/adapters/index.js +0 -2
- package/dist/src/adapters/langchain_adapter.js +0 -49
- package/dist/src/agents/base.js +0 -9
- package/dist/src/agents/index.js +0 -3
- package/dist/src/agents/mcp_agent.js +0 -1002
- package/dist/src/agents/prompts/system_prompt_builder.js +0 -40
- package/dist/src/agents/prompts/templates.js +0 -39
- package/dist/src/agents/remote.js +0 -264
- package/dist/src/agents/utils/ai_sdk.js +0 -62
- package/dist/src/agents/utils/index.js +0 -1
- package/dist/src/client/base.js +0 -119
- package/dist/src/client.js +0 -50
- package/dist/src/config.js +0 -34
- package/dist/src/connectors/base.js +0 -143
- package/dist/src/connectors/http.js +0 -150
- package/dist/src/connectors/index.js +0 -4
- package/dist/src/connectors/stdio.js +0 -68
- package/dist/src/connectors/websocket.js +0 -157
- package/dist/src/logging.js +0 -232
- package/dist/src/managers/index.js +0 -2
- package/dist/src/managers/server_manager.js +0 -106
- package/dist/src/managers/tools/acquire_active_mcp_server.js +0 -17
- package/dist/src/managers/tools/add_server_from_config.js +0 -40
- package/dist/src/managers/tools/base.js +0 -17
- package/dist/src/managers/tools/connect_mcp_server.js +0 -46
- package/dist/src/managers/tools/index.js +0 -5
- package/dist/src/managers/tools/list_mcp_servers.js +0 -33
- package/dist/src/managers/tools/release_mcp_server_connection.js +0 -19
- package/dist/src/oauth-helper.js +0 -427
- package/dist/src/observability/index.js +0 -12
- package/dist/src/observability/langfuse.js +0 -211
- package/dist/src/observability/manager.js +0 -199
- package/dist/src/observability/types.js +0 -4
- package/dist/src/session.js +0 -23
- package/dist/src/task_managers/base.js +0 -127
- package/dist/src/task_managers/index.js +0 -5
- package/dist/src/task_managers/sse.js +0 -43
- package/dist/src/task_managers/stdio.js +0 -51
- package/dist/src/task_managers/streamable_http.js +0 -50
- package/dist/src/task_managers/websocket.js +0 -67
- package/dist/src/telemetry/events.js +0 -44
- package/dist/src/telemetry/index.js +0 -8
- package/dist/src/telemetry/telemetry.js +0 -324
- package/dist/src/telemetry/utils.js +0 -39
- package/dist/tests/ai_sdk_compatibility.test.js +0 -214
- package/dist/tests/stream_events.test.js +0 -307
- package/dist/tests/stream_events_simple.test.js +0 -179
- package/dist/vitest.config.js +0 -21
@@ -0,0 +1,532 @@
|
|
1
|
+
import {
|
2
|
+
__name
|
3
|
+
} from "./chunk-SHUYVCID.js";
|
4
|
+
|
5
|
+
// src/server/mcp-server.ts
|
6
|
+
import { McpServer as OfficialMcpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
7
|
+
import { z } from "zod";
|
8
|
+
import express from "express";
|
9
|
+
import { existsSync, readdirSync } from "fs";
|
10
|
+
import { join } from "path";
|
11
|
+
|
12
|
+
// src/server/logging.ts
|
13
|
+
function requestLogger(req, res, next) {
|
14
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString().substring(11, 23);
|
15
|
+
const method = req.method;
|
16
|
+
const url = req.url;
|
17
|
+
const originalEnd = res.end.bind(res);
|
18
|
+
res.end = function(chunk, encoding, cb) {
|
19
|
+
const statusCode = res.statusCode;
|
20
|
+
let statusColor = "";
|
21
|
+
if (statusCode >= 200 && statusCode < 300) {
|
22
|
+
statusColor = "\x1B[32m";
|
23
|
+
} else if (statusCode >= 300 && statusCode < 400) {
|
24
|
+
statusColor = "\x1B[33m";
|
25
|
+
} else if (statusCode >= 400 && statusCode < 500) {
|
26
|
+
statusColor = "\x1B[31m";
|
27
|
+
} else if (statusCode >= 500) {
|
28
|
+
statusColor = "\x1B[35m";
|
29
|
+
}
|
30
|
+
let logMessage = `[${timestamp}] ${method} \x1B[1m${url}\x1B[0m`;
|
31
|
+
if (method === "POST" && url === "/mcp" && req.body?.method) {
|
32
|
+
logMessage += ` \x1B[1m[${req.body.method}]\x1B[0m`;
|
33
|
+
}
|
34
|
+
logMessage += ` ${statusColor}${statusCode}\x1B[0m`;
|
35
|
+
console.log(logMessage);
|
36
|
+
return originalEnd(chunk, encoding, cb);
|
37
|
+
};
|
38
|
+
next();
|
39
|
+
}
|
40
|
+
__name(requestLogger, "requestLogger");
|
41
|
+
|
42
|
+
// src/server/mcp-server.ts
|
43
|
+
var McpServer = class {
|
44
|
+
static {
|
45
|
+
__name(this, "McpServer");
|
46
|
+
}
|
47
|
+
server;
|
48
|
+
config;
|
49
|
+
app;
|
50
|
+
mcpMounted = false;
|
51
|
+
inspectorMounted = false;
|
52
|
+
serverPort;
|
53
|
+
/**
|
54
|
+
* Creates a new MCP server instance with Express integration
|
55
|
+
*
|
56
|
+
* Initializes the server with the provided configuration, sets up CORS headers,
|
57
|
+
* configures widget serving routes, and creates a proxy that allows direct
|
58
|
+
* access to Express methods while preserving MCP server functionality.
|
59
|
+
*
|
60
|
+
* @param config - Server configuration including name, version, and description
|
61
|
+
* @returns A proxied McpServer instance that supports both MCP and Express methods
|
62
|
+
*/
|
63
|
+
constructor(config) {
|
64
|
+
this.config = config;
|
65
|
+
this.server = new OfficialMcpServer({
|
66
|
+
name: config.name,
|
67
|
+
version: config.version
|
68
|
+
});
|
69
|
+
this.app = express();
|
70
|
+
this.app.use((req, res, next) => {
|
71
|
+
res.header("Access-Control-Allow-Origin", "*");
|
72
|
+
res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
|
73
|
+
res.header("Access-Control-Allow-Headers", "Content-Type");
|
74
|
+
next();
|
75
|
+
});
|
76
|
+
this.app.use(requestLogger);
|
77
|
+
this.setupWidgetRoutes();
|
78
|
+
return new Proxy(this, {
|
79
|
+
get(target, prop) {
|
80
|
+
if (prop in target) {
|
81
|
+
return target[prop];
|
82
|
+
}
|
83
|
+
const value = target.app[prop];
|
84
|
+
return typeof value === "function" ? value.bind(target.app) : value;
|
85
|
+
}
|
86
|
+
});
|
87
|
+
}
|
88
|
+
/**
|
89
|
+
* Define a static resource that can be accessed by clients
|
90
|
+
*
|
91
|
+
* Registers a resource with the MCP server that clients can access via HTTP.
|
92
|
+
* Resources are static content like files, data, or pre-computed results that
|
93
|
+
* can be retrieved by clients without requiring parameters.
|
94
|
+
*
|
95
|
+
* @param resourceDefinition - Configuration object containing resource metadata and handler function
|
96
|
+
* @param resourceDefinition.name - Unique identifier for the resource
|
97
|
+
* @param resourceDefinition.uri - URI pattern for accessing the resource
|
98
|
+
* @param resourceDefinition.resource - Resource metadata (mime type, description, etc.)
|
99
|
+
* @param resourceDefinition.fn - Async function that returns the resource content
|
100
|
+
* @returns The server instance for method chaining
|
101
|
+
*
|
102
|
+
* @example
|
103
|
+
* ```typescript
|
104
|
+
* server.resource({
|
105
|
+
* name: 'config',
|
106
|
+
* uri: 'config://app-settings',
|
107
|
+
* resource: { mimeType: 'application/json' },
|
108
|
+
* fn: async () => ({ theme: 'dark', language: 'en' })
|
109
|
+
* })
|
110
|
+
* ```
|
111
|
+
*/
|
112
|
+
resource(resourceDefinition) {
|
113
|
+
this.server.resource(
|
114
|
+
resourceDefinition.name,
|
115
|
+
resourceDefinition.uri,
|
116
|
+
resourceDefinition.resource,
|
117
|
+
async () => {
|
118
|
+
return await resourceDefinition.fn();
|
119
|
+
}
|
120
|
+
);
|
121
|
+
return this;
|
122
|
+
}
|
123
|
+
/**
|
124
|
+
* Define a dynamic resource template with parameters
|
125
|
+
*/
|
126
|
+
// TODO implement, for some freaky reason this give errors
|
127
|
+
// resourceTemplate(resourceTemplateDefinition: ResourceTemplateDefinition): this {
|
128
|
+
// this.server.resource(
|
129
|
+
// resourceTemplateDefinition.name,
|
130
|
+
// resourceTemplateDefinition.resourceTemplate,
|
131
|
+
// async (uri, params) => {
|
132
|
+
// return await resourceTemplateDefinition.fn(uri, params)
|
133
|
+
// },
|
134
|
+
// )
|
135
|
+
// return this
|
136
|
+
// }
|
137
|
+
/**
|
138
|
+
* Define a tool that can be called by clients
|
139
|
+
*
|
140
|
+
* Registers a tool with the MCP server that clients can invoke with parameters.
|
141
|
+
* Tools are functions that perform actions, computations, or operations and
|
142
|
+
* return results. They accept structured input parameters and return structured output.
|
143
|
+
*
|
144
|
+
* @param toolDefinition - Configuration object containing tool metadata and handler function
|
145
|
+
* @param toolDefinition.name - Unique identifier for the tool
|
146
|
+
* @param toolDefinition.description - Human-readable description of what the tool does
|
147
|
+
* @param toolDefinition.inputs - Array of input parameter definitions with types and validation
|
148
|
+
* @param toolDefinition.fn - Async function that executes the tool logic with provided parameters
|
149
|
+
* @returns The server instance for method chaining
|
150
|
+
*
|
151
|
+
* @example
|
152
|
+
* ```typescript
|
153
|
+
* server.tool({
|
154
|
+
* name: 'calculate',
|
155
|
+
* description: 'Performs mathematical calculations',
|
156
|
+
* inputs: [
|
157
|
+
* { name: 'expression', type: 'string', required: true },
|
158
|
+
* { name: 'precision', type: 'number', required: false }
|
159
|
+
* ],
|
160
|
+
* fn: async ({ expression, precision = 2 }) => {
|
161
|
+
* const result = eval(expression)
|
162
|
+
* return { result: Number(result.toFixed(precision)) }
|
163
|
+
* }
|
164
|
+
* })
|
165
|
+
* ```
|
166
|
+
*/
|
167
|
+
tool(toolDefinition) {
|
168
|
+
const inputSchema = this.createToolInputSchema(toolDefinition.inputs || []);
|
169
|
+
this.server.tool(
|
170
|
+
toolDefinition.name,
|
171
|
+
toolDefinition.description ?? "",
|
172
|
+
inputSchema,
|
173
|
+
async (params) => {
|
174
|
+
return await toolDefinition.fn(params);
|
175
|
+
}
|
176
|
+
);
|
177
|
+
return this;
|
178
|
+
}
|
179
|
+
/**
|
180
|
+
* Define a prompt template
|
181
|
+
*
|
182
|
+
* Registers a prompt template with the MCP server that clients can use to generate
|
183
|
+
* structured prompts for AI models. Prompt templates accept parameters and return
|
184
|
+
* formatted text that can be used as input to language models or other AI systems.
|
185
|
+
*
|
186
|
+
* @param promptDefinition - Configuration object containing prompt metadata and handler function
|
187
|
+
* @param promptDefinition.name - Unique identifier for the prompt template
|
188
|
+
* @param promptDefinition.description - Human-readable description of the prompt's purpose
|
189
|
+
* @param promptDefinition.args - Array of argument definitions with types and validation
|
190
|
+
* @param promptDefinition.fn - Async function that generates the prompt from provided arguments
|
191
|
+
* @returns The server instance for method chaining
|
192
|
+
*
|
193
|
+
* @example
|
194
|
+
* ```typescript
|
195
|
+
* server.prompt({
|
196
|
+
* name: 'code-review',
|
197
|
+
* description: 'Generates a code review prompt',
|
198
|
+
* args: [
|
199
|
+
* { name: 'language', type: 'string', required: true },
|
200
|
+
* { name: 'focus', type: 'string', required: false }
|
201
|
+
* ],
|
202
|
+
* fn: async ({ language, focus = 'general' }) => {
|
203
|
+
* return {
|
204
|
+
* messages: [{
|
205
|
+
* role: 'user',
|
206
|
+
* content: `Please review this ${language} code with focus on ${focus}...`
|
207
|
+
* }]
|
208
|
+
* }
|
209
|
+
* }
|
210
|
+
* })
|
211
|
+
* ```
|
212
|
+
*/
|
213
|
+
prompt(promptDefinition) {
|
214
|
+
const argsSchema = this.createPromptArgsSchema(promptDefinition.args || []);
|
215
|
+
this.server.prompt(
|
216
|
+
promptDefinition.name,
|
217
|
+
promptDefinition.description ?? "",
|
218
|
+
argsSchema,
|
219
|
+
async (params) => {
|
220
|
+
return await promptDefinition.fn(params);
|
221
|
+
}
|
222
|
+
);
|
223
|
+
return this;
|
224
|
+
}
|
225
|
+
/**
|
226
|
+
* Mount MCP server endpoints at /mcp
|
227
|
+
*
|
228
|
+
* Sets up the HTTP transport layer for the MCP server, creating endpoints for
|
229
|
+
* Server-Sent Events (SSE) streaming, POST message handling, and DELETE session cleanup.
|
230
|
+
* Uses stateless mode for session management, making it suitable for stateless deployments.
|
231
|
+
*
|
232
|
+
* This method is called automatically when the server starts listening and ensures
|
233
|
+
* that MCP clients can communicate with the server over HTTP.
|
234
|
+
*
|
235
|
+
* @private
|
236
|
+
* @returns Promise that resolves when MCP endpoints are successfully mounted
|
237
|
+
*
|
238
|
+
* @example
|
239
|
+
* Endpoints created:
|
240
|
+
* - GET /mcp - SSE streaming endpoint for real-time communication
|
241
|
+
* - POST /mcp - Message handling endpoint for MCP protocol messages
|
242
|
+
* - DELETE /mcp - Session cleanup endpoint
|
243
|
+
*/
|
244
|
+
async mountMcp() {
|
245
|
+
if (this.mcpMounted) return;
|
246
|
+
const { StreamableHTTPServerTransport } = await import("@modelcontextprotocol/sdk/server/streamableHttp.js");
|
247
|
+
const httpTransport = new StreamableHTTPServerTransport({
|
248
|
+
sessionIdGenerator: void 0
|
249
|
+
// Stateless mode
|
250
|
+
});
|
251
|
+
await this.server.connect(httpTransport);
|
252
|
+
const endpoint = "/mcp";
|
253
|
+
this.app.get(endpoint, async (req, res) => {
|
254
|
+
await httpTransport.handleRequest(req, res);
|
255
|
+
});
|
256
|
+
this.app.post(endpoint, express.json(), async (req, res) => {
|
257
|
+
await httpTransport.handleRequest(req, res, req.body);
|
258
|
+
});
|
259
|
+
this.app.delete(endpoint, async (req, res) => {
|
260
|
+
await httpTransport.handleRequest(req, res);
|
261
|
+
});
|
262
|
+
this.mcpMounted = true;
|
263
|
+
console.log(`[MCP] Server mounted at ${endpoint}`);
|
264
|
+
}
|
265
|
+
/**
|
266
|
+
* Start the Express server with MCP endpoints
|
267
|
+
*
|
268
|
+
* Initiates the server startup process by mounting MCP endpoints, configuring
|
269
|
+
* the inspector UI (if available), and starting the Express server to listen
|
270
|
+
* for incoming connections. This is the main entry point for running the server.
|
271
|
+
*
|
272
|
+
* The server will be accessible at the specified port with MCP endpoints at /mcp
|
273
|
+
* and inspector UI at /inspector (if the inspector package is installed).
|
274
|
+
*
|
275
|
+
* @param port - Port number to listen on (defaults to 3001 if not specified)
|
276
|
+
* @returns Promise that resolves when the server is successfully listening
|
277
|
+
*
|
278
|
+
* @example
|
279
|
+
* ```typescript
|
280
|
+
* await server.listen(8080)
|
281
|
+
* // Server now running at http://localhost:8080
|
282
|
+
* // MCP endpoints: http://localhost:8080/mcp
|
283
|
+
* // Inspector UI: http://localhost:8080/inspector
|
284
|
+
* ```
|
285
|
+
*/
|
286
|
+
async listen(port) {
|
287
|
+
await this.mountMcp();
|
288
|
+
this.serverPort = port || 3001;
|
289
|
+
this.mountInspector();
|
290
|
+
this.app.listen(this.serverPort, () => {
|
291
|
+
console.log(`[SERVER] Listening on http://localhost:${this.serverPort}`);
|
292
|
+
console.log(`[MCP] Endpoints: http://localhost:${this.serverPort}/mcp`);
|
293
|
+
});
|
294
|
+
}
|
295
|
+
/**
|
296
|
+
* Mount MCP Inspector UI at /inspector
|
297
|
+
*
|
298
|
+
* Dynamically loads and mounts the MCP Inspector UI package if available, providing
|
299
|
+
* a web-based interface for testing and debugging MCP servers. The inspector
|
300
|
+
* automatically connects to the local MCP server endpoints.
|
301
|
+
*
|
302
|
+
* This method gracefully handles cases where the inspector package is not installed,
|
303
|
+
* allowing the server to function without the inspector in production environments.
|
304
|
+
*
|
305
|
+
* @private
|
306
|
+
* @returns void
|
307
|
+
*
|
308
|
+
* @example
|
309
|
+
* If @mcp-use/inspector is installed:
|
310
|
+
* - Inspector UI available at http://localhost:PORT/inspector
|
311
|
+
* - Automatically connects to http://localhost:PORT/mcp
|
312
|
+
*
|
313
|
+
* If not installed:
|
314
|
+
* - Server continues to function normally
|
315
|
+
* - No inspector UI available
|
316
|
+
*/
|
317
|
+
mountInspector() {
|
318
|
+
if (this.inspectorMounted) return;
|
319
|
+
import("@mcp-use/inspector").then(({ mountInspector }) => {
|
320
|
+
const mcpServerUrl = `http://localhost:${this.serverPort}/mcp`;
|
321
|
+
mountInspector(this.app, "/inspector", mcpServerUrl);
|
322
|
+
this.inspectorMounted = true;
|
323
|
+
console.log(`[INSPECTOR] UI available at http://localhost:${this.serverPort}/inspector`);
|
324
|
+
}).catch(() => {
|
325
|
+
});
|
326
|
+
}
|
327
|
+
/**
|
328
|
+
* Setup default widget serving routes
|
329
|
+
*
|
330
|
+
* Configures Express routes to serve MCP UI widgets and their static assets.
|
331
|
+
* Widgets are served from the dist/resources/mcp-use/widgets directory and can
|
332
|
+
* be accessed via HTTP endpoints for embedding in web applications.
|
333
|
+
*
|
334
|
+
* Routes created:
|
335
|
+
* - GET /mcp-use/widgets/:widget - Serves widget's index.html
|
336
|
+
* - GET /mcp-use/widgets/:widget/assets/* - Serves widget-specific assets
|
337
|
+
* - GET /mcp-use/widgets/assets/* - Fallback asset serving with auto-discovery
|
338
|
+
*
|
339
|
+
* @private
|
340
|
+
* @returns void
|
341
|
+
*
|
342
|
+
* @example
|
343
|
+
* Widget routes:
|
344
|
+
* - http://localhost:3001/mcp-use/widgets/kanban-board
|
345
|
+
* - http://localhost:3001/mcp-use/widgets/todo-list/assets/style.css
|
346
|
+
* - http://localhost:3001/mcp-use/widgets/assets/script.js (auto-discovered)
|
347
|
+
*/
|
348
|
+
setupWidgetRoutes() {
|
349
|
+
this.app.get("/mcp-use/widgets/:widget/assets/*", (req, res, next) => {
|
350
|
+
const widget = req.params.widget;
|
351
|
+
const assetFile = req.params[0];
|
352
|
+
const assetPath = join(process.cwd(), "dist", "resources", "mcp-use", "widgets", widget, "assets", assetFile);
|
353
|
+
res.sendFile(assetPath, (err) => err ? next() : void 0);
|
354
|
+
});
|
355
|
+
this.app.get("/mcp-use/widgets/assets/*", (req, res, next) => {
|
356
|
+
const assetFile = req.params[0];
|
357
|
+
const widgetsDir = join(process.cwd(), "dist", "resources", "mcp-use", "widgets");
|
358
|
+
try {
|
359
|
+
const widgets = readdirSync(widgetsDir);
|
360
|
+
for (const widget of widgets) {
|
361
|
+
const assetPath = join(widgetsDir, widget, "assets", assetFile);
|
362
|
+
if (existsSync(assetPath)) {
|
363
|
+
return res.sendFile(assetPath);
|
364
|
+
}
|
365
|
+
}
|
366
|
+
next();
|
367
|
+
} catch {
|
368
|
+
next();
|
369
|
+
}
|
370
|
+
});
|
371
|
+
this.app.get("/mcp-use/widgets/:widget", (req, res, next) => {
|
372
|
+
const filePath = join(process.cwd(), "dist", "resources", "mcp-use", "widgets", req.params.widget, "index.html");
|
373
|
+
res.sendFile(filePath, (err) => err ? next() : void 0);
|
374
|
+
});
|
375
|
+
}
|
376
|
+
/**
|
377
|
+
* Create input schema for resource templates
|
378
|
+
*
|
379
|
+
* Parses a URI template string to extract parameter names and generates a Zod
|
380
|
+
* validation schema for those parameters. Used internally for validating resource
|
381
|
+
* template parameters before processing requests.
|
382
|
+
*
|
383
|
+
* @param uriTemplate - URI template string with parameter placeholders (e.g., "/users/{id}/posts/{postId}")
|
384
|
+
* @returns Object mapping parameter names to Zod string schemas
|
385
|
+
*
|
386
|
+
* @example
|
387
|
+
* ```typescript
|
388
|
+
* const schema = this.createInputSchema("/users/{id}/posts/{postId}")
|
389
|
+
* // Returns: { id: z.string(), postId: z.string() }
|
390
|
+
* ```
|
391
|
+
*/
|
392
|
+
createInputSchema(uriTemplate) {
|
393
|
+
const params = this.extractTemplateParams(uriTemplate);
|
394
|
+
const schema = {};
|
395
|
+
params.forEach((param) => {
|
396
|
+
schema[param] = z.string();
|
397
|
+
});
|
398
|
+
return schema;
|
399
|
+
}
|
400
|
+
/**
|
401
|
+
* Create input schema for tools
|
402
|
+
*
|
403
|
+
* Converts tool input definitions into Zod validation schemas for runtime validation.
|
404
|
+
* Supports common data types (string, number, boolean, object, array) and optional
|
405
|
+
* parameters. Used internally when registering tools with the MCP server.
|
406
|
+
*
|
407
|
+
* @param inputs - Array of input parameter definitions with name, type, and optional flag
|
408
|
+
* @returns Object mapping parameter names to Zod validation schemas
|
409
|
+
*
|
410
|
+
* @example
|
411
|
+
* ```typescript
|
412
|
+
* const schema = this.createToolInputSchema([
|
413
|
+
* { name: 'query', type: 'string', required: true },
|
414
|
+
* { name: 'limit', type: 'number', required: false }
|
415
|
+
* ])
|
416
|
+
* // Returns: { query: z.string(), limit: z.number().optional() }
|
417
|
+
* ```
|
418
|
+
*/
|
419
|
+
createToolInputSchema(inputs) {
|
420
|
+
const schema = {};
|
421
|
+
inputs.forEach((input) => {
|
422
|
+
let zodType;
|
423
|
+
switch (input.type) {
|
424
|
+
case "string":
|
425
|
+
zodType = z.string();
|
426
|
+
break;
|
427
|
+
case "number":
|
428
|
+
zodType = z.number();
|
429
|
+
break;
|
430
|
+
case "boolean":
|
431
|
+
zodType = z.boolean();
|
432
|
+
break;
|
433
|
+
case "object":
|
434
|
+
zodType = z.object({});
|
435
|
+
break;
|
436
|
+
case "array":
|
437
|
+
zodType = z.array(z.any());
|
438
|
+
break;
|
439
|
+
default:
|
440
|
+
zodType = z.any();
|
441
|
+
}
|
442
|
+
if (!input.required) {
|
443
|
+
zodType = zodType.optional();
|
444
|
+
}
|
445
|
+
schema[input.name] = zodType;
|
446
|
+
});
|
447
|
+
return schema;
|
448
|
+
}
|
449
|
+
/**
|
450
|
+
* Create arguments schema for prompts
|
451
|
+
*
|
452
|
+
* Converts prompt argument definitions into Zod validation schemas for runtime validation.
|
453
|
+
* Supports common data types (string, number, boolean, object, array) and optional
|
454
|
+
* parameters. Used internally when registering prompt templates with the MCP server.
|
455
|
+
*
|
456
|
+
* @param inputs - Array of argument definitions with name, type, and optional flag
|
457
|
+
* @returns Object mapping argument names to Zod validation schemas
|
458
|
+
*
|
459
|
+
* @example
|
460
|
+
* ```typescript
|
461
|
+
* const schema = this.createPromptArgsSchema([
|
462
|
+
* { name: 'topic', type: 'string', required: true },
|
463
|
+
* { name: 'style', type: 'string', required: false }
|
464
|
+
* ])
|
465
|
+
* // Returns: { topic: z.string(), style: z.string().optional() }
|
466
|
+
* ```
|
467
|
+
*/
|
468
|
+
createPromptArgsSchema(inputs) {
|
469
|
+
const schema = {};
|
470
|
+
inputs.forEach((input) => {
|
471
|
+
let zodType;
|
472
|
+
switch (input.type) {
|
473
|
+
case "string":
|
474
|
+
zodType = z.string();
|
475
|
+
break;
|
476
|
+
case "number":
|
477
|
+
zodType = z.number();
|
478
|
+
break;
|
479
|
+
case "boolean":
|
480
|
+
zodType = z.boolean();
|
481
|
+
break;
|
482
|
+
case "object":
|
483
|
+
zodType = z.object({});
|
484
|
+
break;
|
485
|
+
case "array":
|
486
|
+
zodType = z.array(z.any());
|
487
|
+
break;
|
488
|
+
default:
|
489
|
+
zodType = z.any();
|
490
|
+
}
|
491
|
+
if (!input.required) {
|
492
|
+
zodType = zodType.optional();
|
493
|
+
}
|
494
|
+
schema[input.name] = zodType;
|
495
|
+
});
|
496
|
+
return schema;
|
497
|
+
}
|
498
|
+
/**
|
499
|
+
* Extract parameter names from URI template
|
500
|
+
*
|
501
|
+
* Parses a URI template string to extract parameter names enclosed in curly braces.
|
502
|
+
* Used internally to identify dynamic parameters in resource templates and generate
|
503
|
+
* appropriate validation schemas.
|
504
|
+
*
|
505
|
+
* @param uriTemplate - URI template string with parameter placeholders (e.g., "/users/{id}/posts/{postId}")
|
506
|
+
* @returns Array of parameter names found in the template
|
507
|
+
*
|
508
|
+
* @example
|
509
|
+
* ```typescript
|
510
|
+
* const params = this.extractTemplateParams("/users/{id}/posts/{postId}")
|
511
|
+
* // Returns: ["id", "postId"]
|
512
|
+
* ```
|
513
|
+
*/
|
514
|
+
extractTemplateParams(uriTemplate) {
|
515
|
+
const matches = uriTemplate.match(/\{([^}]+)\}/g);
|
516
|
+
return matches ? matches.map((match) => match.slice(1, -1)) : [];
|
517
|
+
}
|
518
|
+
};
|
519
|
+
function createMCPServer(name, config = {}) {
|
520
|
+
const instance = new McpServer({
|
521
|
+
name,
|
522
|
+
version: config.version || "1.0.0",
|
523
|
+
description: config.description
|
524
|
+
});
|
525
|
+
return instance;
|
526
|
+
}
|
527
|
+
__name(createMCPServer, "createMCPServer");
|
528
|
+
|
529
|
+
export {
|
530
|
+
McpServer,
|
531
|
+
createMCPServer
|
532
|
+
};
|