mcp-use 1.0.3 → 1.0.4

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/index.cjs CHANGED
@@ -496,6 +496,7 @@ __export(index_exports, {
496
496
  Telemetry: () => Telemetry,
497
497
  ToolMessage: () => import_messages3.ToolMessage,
498
498
  WebSocketConnector: () => WebSocketConnector,
499
+ createMCPServer: () => createMCPServer,
499
500
  createOAuthMCPConfig: () => createOAuthMCPConfig,
500
501
  createReadableStreamFromGenerator: () => createReadableStreamFromGenerator,
501
502
  loadConfigFile: () => loadConfigFile,
@@ -2585,6 +2586,7 @@ var MCPAgent = class {
2585
2586
  totalResponseLength += event.data.chunk.content.length;
2586
2587
  }
2587
2588
  yield event;
2589
+ await new Promise((resolve) => setTimeout(resolve, 0));
2588
2590
  if (event.event === "on_chain_end" && event.data?.output) {
2589
2591
  const output = event.data.output;
2590
2592
  if (Array.isArray(output) && output.length > 0 && output[0]?.text) {
@@ -3999,6 +4001,643 @@ async function* streamEventsToAISDKWithTools(streamEvents) {
3999
4001
  }
4000
4002
  __name(streamEventsToAISDKWithTools, "streamEventsToAISDKWithTools");
4001
4003
 
4004
+ // src/server/mcp-server.ts
4005
+ var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
4006
+ var import_zod7 = require("zod");
4007
+ var import_express = __toESM(require("express"), 1);
4008
+ var import_node_fs3 = require("fs");
4009
+ var import_node_path2 = require("path");
4010
+
4011
+ // src/server/logging.ts
4012
+ function requestLogger(req, res, next) {
4013
+ const timestamp2 = (/* @__PURE__ */ new Date()).toISOString().substring(11, 23);
4014
+ const method = req.method;
4015
+ const url = req.url;
4016
+ const originalEnd = res.end.bind(res);
4017
+ res.end = function(chunk, encoding, cb) {
4018
+ const statusCode = res.statusCode;
4019
+ let statusColor = "";
4020
+ if (statusCode >= 200 && statusCode < 300) {
4021
+ statusColor = "\x1B[32m";
4022
+ } else if (statusCode >= 300 && statusCode < 400) {
4023
+ statusColor = "\x1B[33m";
4024
+ } else if (statusCode >= 400 && statusCode < 500) {
4025
+ statusColor = "\x1B[31m";
4026
+ } else if (statusCode >= 500) {
4027
+ statusColor = "\x1B[35m";
4028
+ }
4029
+ let logMessage = `[${timestamp2}] ${method} \x1B[1m${url}\x1B[0m`;
4030
+ if (method === "POST" && url === "/mcp" && req.body?.method) {
4031
+ logMessage += ` \x1B[1m[${req.body.method}]\x1B[0m`;
4032
+ }
4033
+ logMessage += ` ${statusColor}${statusCode}\x1B[0m`;
4034
+ console.log(logMessage);
4035
+ return originalEnd(chunk, encoding, cb);
4036
+ };
4037
+ next();
4038
+ }
4039
+ __name(requestLogger, "requestLogger");
4040
+
4041
+ // src/server/mcp-server.ts
4042
+ var McpServer = class {
4043
+ static {
4044
+ __name(this, "McpServer");
4045
+ }
4046
+ server;
4047
+ config;
4048
+ app;
4049
+ mcpMounted = false;
4050
+ inspectorMounted = false;
4051
+ serverPort;
4052
+ /**
4053
+ * Creates a new MCP server instance with Express integration
4054
+ *
4055
+ * Initializes the server with the provided configuration, sets up CORS headers,
4056
+ * configures widget serving routes, and creates a proxy that allows direct
4057
+ * access to Express methods while preserving MCP server functionality.
4058
+ *
4059
+ * @param config - Server configuration including name, version, and description
4060
+ * @returns A proxied McpServer instance that supports both MCP and Express methods
4061
+ */
4062
+ constructor(config2) {
4063
+ this.config = config2;
4064
+ this.server = new import_mcp.McpServer({
4065
+ name: config2.name,
4066
+ version: config2.version
4067
+ });
4068
+ this.app = (0, import_express.default)();
4069
+ this.app.use(import_express.default.json());
4070
+ this.app.use((req, res, next) => {
4071
+ res.header("Access-Control-Allow-Origin", "*");
4072
+ res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
4073
+ res.header("Access-Control-Allow-Headers", "Content-Type");
4074
+ next();
4075
+ });
4076
+ this.app.use(requestLogger);
4077
+ this.setupWidgetRoutes();
4078
+ return new Proxy(this, {
4079
+ get(target, prop) {
4080
+ if (prop in target) {
4081
+ return target[prop];
4082
+ }
4083
+ const value = target.app[prop];
4084
+ return typeof value === "function" ? value.bind(target.app) : value;
4085
+ }
4086
+ });
4087
+ }
4088
+ /**
4089
+ * Define a static resource that can be accessed by clients
4090
+ *
4091
+ * Registers a resource with the MCP server that clients can access via HTTP.
4092
+ * Resources are static content like files, data, or pre-computed results that
4093
+ * can be retrieved by clients without requiring parameters.
4094
+ *
4095
+ * @param resourceDefinition - Configuration object containing resource metadata and handler function
4096
+ * @param resourceDefinition.name - Unique identifier for the resource
4097
+ * @param resourceDefinition.uri - URI pattern for accessing the resource
4098
+ * @param resourceDefinition.title - Optional human-readable title for the resource
4099
+ * @param resourceDefinition.description - Optional description of the resource
4100
+ * @param resourceDefinition.mimeType - MIME type of the resource content
4101
+ * @param resourceDefinition.annotations - Optional annotations (audience, priority, lastModified)
4102
+ * @param resourceDefinition.fn - Async function that returns the resource content
4103
+ * @returns The server instance for method chaining
4104
+ *
4105
+ * @example
4106
+ * ```typescript
4107
+ * server.resource({
4108
+ * name: 'config',
4109
+ * uri: 'config://app-settings',
4110
+ * title: 'Application Settings',
4111
+ * mimeType: 'application/json',
4112
+ * description: 'Current application configuration',
4113
+ * annotations: {
4114
+ * audience: ['user'],
4115
+ * priority: 0.8
4116
+ * },
4117
+ * fn: async () => ({
4118
+ * contents: [{
4119
+ * uri: 'config://app-settings',
4120
+ * mimeType: 'application/json',
4121
+ * text: JSON.stringify({ theme: 'dark', language: 'en' })
4122
+ * }]
4123
+ * })
4124
+ * })
4125
+ * ```
4126
+ */
4127
+ resource(resourceDefinition) {
4128
+ this.server.resource(
4129
+ resourceDefinition.name,
4130
+ resourceDefinition.uri,
4131
+ {
4132
+ name: resourceDefinition.name,
4133
+ title: resourceDefinition.title,
4134
+ description: resourceDefinition.description,
4135
+ mimeType: resourceDefinition.mimeType,
4136
+ annotations: resourceDefinition.annotations
4137
+ },
4138
+ async () => {
4139
+ return await resourceDefinition.fn();
4140
+ }
4141
+ );
4142
+ return this;
4143
+ }
4144
+ /**
4145
+ * Define a dynamic resource template with parameters
4146
+ *
4147
+ * Registers a parameterized resource template with the MCP server. Templates use URI
4148
+ * patterns with placeholders that can be filled in at request time, allowing dynamic
4149
+ * resource generation based on parameters.
4150
+ *
4151
+ * @param resourceTemplateDefinition - Configuration object for the resource template
4152
+ * @param resourceTemplateDefinition.name - Unique identifier for the template
4153
+ * @param resourceTemplateDefinition.resourceTemplate - ResourceTemplate object with uriTemplate and metadata
4154
+ * @param resourceTemplateDefinition.fn - Async function that generates resource content from URI and params
4155
+ * @returns The server instance for method chaining
4156
+ *
4157
+ * @example
4158
+ * ```typescript
4159
+ * server.resourceTemplate({
4160
+ * name: 'user-profile',
4161
+ * resourceTemplate: {
4162
+ * uriTemplate: 'user://{userId}/profile',
4163
+ * name: 'User Profile',
4164
+ * mimeType: 'application/json'
4165
+ * },
4166
+ * fn: async (uri, params) => ({
4167
+ * contents: [{
4168
+ * uri: uri.toString(),
4169
+ * mimeType: 'application/json',
4170
+ * text: JSON.stringify({ userId: params.userId, name: 'John Doe' })
4171
+ * }]
4172
+ * })
4173
+ * })
4174
+ * ```
4175
+ */
4176
+ resourceTemplate(resourceTemplateDefinition) {
4177
+ const template = new import_mcp.ResourceTemplate(
4178
+ resourceTemplateDefinition.resourceTemplate.uriTemplate,
4179
+ {
4180
+ list: void 0,
4181
+ // Optional: callback to list all matching resources
4182
+ complete: void 0
4183
+ // Optional: callback for auto-completion
4184
+ }
4185
+ );
4186
+ const metadata = {};
4187
+ if (resourceTemplateDefinition.resourceTemplate.name) {
4188
+ metadata.name = resourceTemplateDefinition.resourceTemplate.name;
4189
+ }
4190
+ if (resourceTemplateDefinition.title) {
4191
+ metadata.title = resourceTemplateDefinition.title;
4192
+ }
4193
+ if (resourceTemplateDefinition.description || resourceTemplateDefinition.resourceTemplate.description) {
4194
+ metadata.description = resourceTemplateDefinition.description || resourceTemplateDefinition.resourceTemplate.description;
4195
+ }
4196
+ if (resourceTemplateDefinition.resourceTemplate.mimeType) {
4197
+ metadata.mimeType = resourceTemplateDefinition.resourceTemplate.mimeType;
4198
+ }
4199
+ if (resourceTemplateDefinition.annotations) {
4200
+ metadata.annotations = resourceTemplateDefinition.annotations;
4201
+ }
4202
+ this.server.resource(
4203
+ resourceTemplateDefinition.name,
4204
+ template,
4205
+ metadata,
4206
+ async (uri) => {
4207
+ const params = this.parseTemplateUri(
4208
+ resourceTemplateDefinition.resourceTemplate.uriTemplate,
4209
+ uri.toString()
4210
+ );
4211
+ return await resourceTemplateDefinition.fn(uri, params);
4212
+ }
4213
+ );
4214
+ return this;
4215
+ }
4216
+ /**
4217
+ * Define a tool that can be called by clients
4218
+ *
4219
+ * Registers a tool with the MCP server that clients can invoke with parameters.
4220
+ * Tools are functions that perform actions, computations, or operations and
4221
+ * return results. They accept structured input parameters and return structured output.
4222
+ *
4223
+ * @param toolDefinition - Configuration object containing tool metadata and handler function
4224
+ * @param toolDefinition.name - Unique identifier for the tool
4225
+ * @param toolDefinition.description - Human-readable description of what the tool does
4226
+ * @param toolDefinition.inputs - Array of input parameter definitions with types and validation
4227
+ * @param toolDefinition.fn - Async function that executes the tool logic with provided parameters
4228
+ * @returns The server instance for method chaining
4229
+ *
4230
+ * @example
4231
+ * ```typescript
4232
+ * server.tool({
4233
+ * name: 'calculate',
4234
+ * description: 'Performs mathematical calculations',
4235
+ * inputs: [
4236
+ * { name: 'expression', type: 'string', required: true },
4237
+ * { name: 'precision', type: 'number', required: false }
4238
+ * ],
4239
+ * fn: async ({ expression, precision = 2 }) => {
4240
+ * const result = eval(expression)
4241
+ * return { result: Number(result.toFixed(precision)) }
4242
+ * }
4243
+ * })
4244
+ * ```
4245
+ */
4246
+ tool(toolDefinition) {
4247
+ const inputSchema = this.createToolInputSchema(toolDefinition.inputs || []);
4248
+ this.server.tool(
4249
+ toolDefinition.name,
4250
+ toolDefinition.description ?? "",
4251
+ inputSchema,
4252
+ async (params) => {
4253
+ return await toolDefinition.fn(params);
4254
+ }
4255
+ );
4256
+ return this;
4257
+ }
4258
+ /**
4259
+ * Define a prompt template
4260
+ *
4261
+ * Registers a prompt template with the MCP server that clients can use to generate
4262
+ * structured prompts for AI models. Prompt templates accept parameters and return
4263
+ * formatted text that can be used as input to language models or other AI systems.
4264
+ *
4265
+ * @param promptDefinition - Configuration object containing prompt metadata and handler function
4266
+ * @param promptDefinition.name - Unique identifier for the prompt template
4267
+ * @param promptDefinition.description - Human-readable description of the prompt's purpose
4268
+ * @param promptDefinition.args - Array of argument definitions with types and validation
4269
+ * @param promptDefinition.fn - Async function that generates the prompt from provided arguments
4270
+ * @returns The server instance for method chaining
4271
+ *
4272
+ * @example
4273
+ * ```typescript
4274
+ * server.prompt({
4275
+ * name: 'code-review',
4276
+ * description: 'Generates a code review prompt',
4277
+ * args: [
4278
+ * { name: 'language', type: 'string', required: true },
4279
+ * { name: 'focus', type: 'string', required: false }
4280
+ * ],
4281
+ * fn: async ({ language, focus = 'general' }) => {
4282
+ * return {
4283
+ * messages: [{
4284
+ * role: 'user',
4285
+ * content: `Please review this ${language} code with focus on ${focus}...`
4286
+ * }]
4287
+ * }
4288
+ * }
4289
+ * })
4290
+ * ```
4291
+ */
4292
+ prompt(promptDefinition) {
4293
+ const argsSchema = this.createPromptArgsSchema(promptDefinition.args || []);
4294
+ this.server.prompt(
4295
+ promptDefinition.name,
4296
+ promptDefinition.description ?? "",
4297
+ argsSchema,
4298
+ async (params) => {
4299
+ return await promptDefinition.fn(params);
4300
+ }
4301
+ );
4302
+ return this;
4303
+ }
4304
+ /**
4305
+ * Mount MCP server endpoints at /mcp
4306
+ *
4307
+ * Sets up the HTTP transport layer for the MCP server, creating endpoints for
4308
+ * Server-Sent Events (SSE) streaming, POST message handling, and DELETE session cleanup.
4309
+ * Uses stateless mode for session management, making it suitable for stateless deployments.
4310
+ *
4311
+ * This method is called automatically when the server starts listening and ensures
4312
+ * that MCP clients can communicate with the server over HTTP.
4313
+ *
4314
+ * @private
4315
+ * @returns Promise that resolves when MCP endpoints are successfully mounted
4316
+ *
4317
+ * @example
4318
+ * Endpoints created:
4319
+ * - GET /mcp - SSE streaming endpoint for real-time communication
4320
+ * - POST /mcp - Message handling endpoint for MCP protocol messages
4321
+ * - DELETE /mcp - Session cleanup endpoint
4322
+ */
4323
+ async mountMcp() {
4324
+ if (this.mcpMounted) return;
4325
+ const { StreamableHTTPServerTransport } = await import("@modelcontextprotocol/sdk/server/streamableHttp.js");
4326
+ const httpTransport = new StreamableHTTPServerTransport({
4327
+ sessionIdGenerator: void 0
4328
+ // Stateless mode
4329
+ });
4330
+ await this.server.connect(httpTransport);
4331
+ const endpoint = "/mcp";
4332
+ this.app.get(endpoint, async (req, res) => {
4333
+ await httpTransport.handleRequest(req, res);
4334
+ });
4335
+ this.app.post(endpoint, import_express.default.json(), async (req, res) => {
4336
+ await httpTransport.handleRequest(req, res, req.body);
4337
+ });
4338
+ this.app.delete(endpoint, async (req, res) => {
4339
+ await httpTransport.handleRequest(req, res);
4340
+ });
4341
+ this.mcpMounted = true;
4342
+ console.log(`[MCP] Server mounted at ${endpoint}`);
4343
+ }
4344
+ /**
4345
+ * Start the Express server with MCP endpoints
4346
+ *
4347
+ * Initiates the server startup process by mounting MCP endpoints, configuring
4348
+ * the inspector UI (if available), and starting the Express server to listen
4349
+ * for incoming connections. This is the main entry point for running the server.
4350
+ *
4351
+ * The server will be accessible at the specified port with MCP endpoints at /mcp
4352
+ * and inspector UI at /inspector (if the inspector package is installed).
4353
+ *
4354
+ * @param port - Port number to listen on (defaults to 3001 if not specified)
4355
+ * @returns Promise that resolves when the server is successfully listening
4356
+ *
4357
+ * @example
4358
+ * ```typescript
4359
+ * await server.listen(8080)
4360
+ * // Server now running at http://localhost:8080
4361
+ * // MCP endpoints: http://localhost:8080/mcp
4362
+ * // Inspector UI: http://localhost:8080/inspector
4363
+ * ```
4364
+ */
4365
+ async listen(port) {
4366
+ await this.mountMcp();
4367
+ this.serverPort = port || 3001;
4368
+ this.mountInspector();
4369
+ this.app.listen(this.serverPort, () => {
4370
+ console.log(`[SERVER] Listening on http://localhost:${this.serverPort}`);
4371
+ console.log(`[MCP] Endpoints: http://localhost:${this.serverPort}/mcp`);
4372
+ });
4373
+ }
4374
+ /**
4375
+ * Mount MCP Inspector UI at /inspector
4376
+ *
4377
+ * Dynamically loads and mounts the MCP Inspector UI package if available, providing
4378
+ * a web-based interface for testing and debugging MCP servers. The inspector
4379
+ * automatically connects to the local MCP server endpoints.
4380
+ *
4381
+ * This method gracefully handles cases where the inspector package is not installed,
4382
+ * allowing the server to function without the inspector in production environments.
4383
+ *
4384
+ * @private
4385
+ * @returns void
4386
+ *
4387
+ * @example
4388
+ * If @mcp-use/inspector is installed:
4389
+ * - Inspector UI available at http://localhost:PORT/inspector
4390
+ * - Automatically connects to http://localhost:PORT/mcp
4391
+ *
4392
+ * If not installed:
4393
+ * - Server continues to function normally
4394
+ * - No inspector UI available
4395
+ */
4396
+ mountInspector() {
4397
+ if (this.inspectorMounted) return;
4398
+ import("@mcp-use/inspector").then(({ mountInspector }) => {
4399
+ const mcpServerUrl = `http://localhost:${this.serverPort}/mcp`;
4400
+ mountInspector(this.app, "/inspector", mcpServerUrl);
4401
+ this.inspectorMounted = true;
4402
+ console.log(`[INSPECTOR] UI available at http://localhost:${this.serverPort}/inspector`);
4403
+ }).catch(() => {
4404
+ });
4405
+ }
4406
+ /**
4407
+ * Setup default widget serving routes
4408
+ *
4409
+ * Configures Express routes to serve MCP UI widgets and their static assets.
4410
+ * Widgets are served from the dist/resources/mcp-use/widgets directory and can
4411
+ * be accessed via HTTP endpoints for embedding in web applications.
4412
+ *
4413
+ * Routes created:
4414
+ * - GET /mcp-use/widgets/:widget - Serves widget's index.html
4415
+ * - GET /mcp-use/widgets/:widget/assets/* - Serves widget-specific assets
4416
+ * - GET /mcp-use/widgets/assets/* - Fallback asset serving with auto-discovery
4417
+ *
4418
+ * @private
4419
+ * @returns void
4420
+ *
4421
+ * @example
4422
+ * Widget routes:
4423
+ * - http://localhost:3001/mcp-use/widgets/kanban-board
4424
+ * - http://localhost:3001/mcp-use/widgets/todo-list/assets/style.css
4425
+ * - http://localhost:3001/mcp-use/widgets/assets/script.js (auto-discovered)
4426
+ */
4427
+ setupWidgetRoutes() {
4428
+ this.app.get("/mcp-use/widgets/:widget/assets/*", (req, res, next) => {
4429
+ const widget = req.params.widget;
4430
+ const assetFile = req.params[0];
4431
+ const assetPath = (0, import_node_path2.join)(process.cwd(), "dist", "resources", "mcp-use", "widgets", widget, "assets", assetFile);
4432
+ res.sendFile(assetPath, (err) => err ? next() : void 0);
4433
+ });
4434
+ this.app.get("/mcp-use/widgets/assets/*", (req, res, next) => {
4435
+ const assetFile = req.params[0];
4436
+ const widgetsDir = (0, import_node_path2.join)(process.cwd(), "dist", "resources", "mcp-use", "widgets");
4437
+ try {
4438
+ const widgets = (0, import_node_fs3.readdirSync)(widgetsDir);
4439
+ for (const widget of widgets) {
4440
+ const assetPath = (0, import_node_path2.join)(widgetsDir, widget, "assets", assetFile);
4441
+ if ((0, import_node_fs3.existsSync)(assetPath)) {
4442
+ return res.sendFile(assetPath);
4443
+ }
4444
+ }
4445
+ next();
4446
+ } catch {
4447
+ next();
4448
+ }
4449
+ });
4450
+ this.app.get("/mcp-use/widgets/:widget", (req, res, next) => {
4451
+ const filePath = (0, import_node_path2.join)(process.cwd(), "dist", "resources", "mcp-use", "widgets", req.params.widget, "index.html");
4452
+ res.sendFile(filePath, (err) => err ? next() : void 0);
4453
+ });
4454
+ }
4455
+ /**
4456
+ * Create input schema for resource templates
4457
+ *
4458
+ * Parses a URI template string to extract parameter names and generates a Zod
4459
+ * validation schema for those parameters. Used internally for validating resource
4460
+ * template parameters before processing requests.
4461
+ *
4462
+ * @param uriTemplate - URI template string with parameter placeholders (e.g., "/users/{id}/posts/{postId}")
4463
+ * @returns Object mapping parameter names to Zod string schemas
4464
+ *
4465
+ * @example
4466
+ * ```typescript
4467
+ * const schema = this.createInputSchema("/users/{id}/posts/{postId}")
4468
+ * // Returns: { id: z.string(), postId: z.string() }
4469
+ * ```
4470
+ */
4471
+ createInputSchema(uriTemplate) {
4472
+ const params = this.extractTemplateParams(uriTemplate);
4473
+ const schema = {};
4474
+ params.forEach((param) => {
4475
+ schema[param] = import_zod7.z.string();
4476
+ });
4477
+ return schema;
4478
+ }
4479
+ /**
4480
+ * Create input schema for tools
4481
+ *
4482
+ * Converts tool input definitions into Zod validation schemas for runtime validation.
4483
+ * Supports common data types (string, number, boolean, object, array) and optional
4484
+ * parameters. Used internally when registering tools with the MCP server.
4485
+ *
4486
+ * @param inputs - Array of input parameter definitions with name, type, and optional flag
4487
+ * @returns Object mapping parameter names to Zod validation schemas
4488
+ *
4489
+ * @example
4490
+ * ```typescript
4491
+ * const schema = this.createToolInputSchema([
4492
+ * { name: 'query', type: 'string', required: true },
4493
+ * { name: 'limit', type: 'number', required: false }
4494
+ * ])
4495
+ * // Returns: { query: z.string(), limit: z.number().optional() }
4496
+ * ```
4497
+ */
4498
+ createToolInputSchema(inputs) {
4499
+ const schema = {};
4500
+ inputs.forEach((input) => {
4501
+ let zodType;
4502
+ switch (input.type) {
4503
+ case "string":
4504
+ zodType = import_zod7.z.string();
4505
+ break;
4506
+ case "number":
4507
+ zodType = import_zod7.z.number();
4508
+ break;
4509
+ case "boolean":
4510
+ zodType = import_zod7.z.boolean();
4511
+ break;
4512
+ case "object":
4513
+ zodType = import_zod7.z.object({});
4514
+ break;
4515
+ case "array":
4516
+ zodType = import_zod7.z.array(import_zod7.z.any());
4517
+ break;
4518
+ default:
4519
+ zodType = import_zod7.z.any();
4520
+ }
4521
+ if (!input.required) {
4522
+ zodType = zodType.optional();
4523
+ }
4524
+ schema[input.name] = zodType;
4525
+ });
4526
+ return schema;
4527
+ }
4528
+ /**
4529
+ * Create arguments schema for prompts
4530
+ *
4531
+ * Converts prompt argument definitions into Zod validation schemas for runtime validation.
4532
+ * Supports common data types (string, number, boolean, object, array) and optional
4533
+ * parameters. Used internally when registering prompt templates with the MCP server.
4534
+ *
4535
+ * @param inputs - Array of argument definitions with name, type, and optional flag
4536
+ * @returns Object mapping argument names to Zod validation schemas
4537
+ *
4538
+ * @example
4539
+ * ```typescript
4540
+ * const schema = this.createPromptArgsSchema([
4541
+ * { name: 'topic', type: 'string', required: true },
4542
+ * { name: 'style', type: 'string', required: false }
4543
+ * ])
4544
+ * // Returns: { topic: z.string(), style: z.string().optional() }
4545
+ * ```
4546
+ */
4547
+ createPromptArgsSchema(inputs) {
4548
+ const schema = {};
4549
+ inputs.forEach((input) => {
4550
+ let zodType;
4551
+ switch (input.type) {
4552
+ case "string":
4553
+ zodType = import_zod7.z.string();
4554
+ break;
4555
+ case "number":
4556
+ zodType = import_zod7.z.number();
4557
+ break;
4558
+ case "boolean":
4559
+ zodType = import_zod7.z.boolean();
4560
+ break;
4561
+ case "object":
4562
+ zodType = import_zod7.z.object({});
4563
+ break;
4564
+ case "array":
4565
+ zodType = import_zod7.z.array(import_zod7.z.any());
4566
+ break;
4567
+ default:
4568
+ zodType = import_zod7.z.any();
4569
+ }
4570
+ if (!input.required) {
4571
+ zodType = zodType.optional();
4572
+ }
4573
+ schema[input.name] = zodType;
4574
+ });
4575
+ return schema;
4576
+ }
4577
+ /**
4578
+ * Extract parameter names from URI template
4579
+ *
4580
+ * Parses a URI template string to extract parameter names enclosed in curly braces.
4581
+ * Used internally to identify dynamic parameters in resource templates and generate
4582
+ * appropriate validation schemas.
4583
+ *
4584
+ * @param uriTemplate - URI template string with parameter placeholders (e.g., "/users/{id}/posts/{postId}")
4585
+ * @returns Array of parameter names found in the template
4586
+ *
4587
+ * @example
4588
+ * ```typescript
4589
+ * const params = this.extractTemplateParams("/users/{id}/posts/{postId}")
4590
+ * // Returns: ["id", "postId"]
4591
+ * ```
4592
+ */
4593
+ extractTemplateParams(uriTemplate) {
4594
+ const matches = uriTemplate.match(/\{([^}]+)\}/g);
4595
+ return matches ? matches.map((match) => match.slice(1, -1)) : [];
4596
+ }
4597
+ /**
4598
+ * Parse parameter values from a URI based on a template
4599
+ *
4600
+ * Extracts parameter values from an actual URI by matching it against a URI template.
4601
+ * The template contains placeholders like {param} which are extracted as key-value pairs.
4602
+ *
4603
+ * @param template - URI template with placeholders (e.g., "user://{userId}/posts/{postId}")
4604
+ * @param uri - Actual URI to parse (e.g., "user://123/posts/456")
4605
+ * @returns Object mapping parameter names to their values
4606
+ *
4607
+ * @example
4608
+ * ```typescript
4609
+ * const params = this.parseTemplateUri("user://{userId}/posts/{postId}", "user://123/posts/456")
4610
+ * // Returns: { userId: "123", postId: "456" }
4611
+ * ```
4612
+ */
4613
+ parseTemplateUri(template, uri) {
4614
+ const params = {};
4615
+ let regexPattern = template.replace(/[.*+?^$()[\]\\|]/g, "\\$&");
4616
+ const paramNames = [];
4617
+ regexPattern = regexPattern.replace(/\\\{([^}]+)\\\}/g, (_, paramName) => {
4618
+ paramNames.push(paramName);
4619
+ return "([^/]+)";
4620
+ });
4621
+ const regex = new RegExp(`^${regexPattern}$`);
4622
+ const match = uri.match(regex);
4623
+ if (match) {
4624
+ paramNames.forEach((paramName, index) => {
4625
+ params[paramName] = match[index + 1];
4626
+ });
4627
+ }
4628
+ return params;
4629
+ }
4630
+ };
4631
+ function createMCPServer(name, config2 = {}) {
4632
+ const instance = new McpServer({
4633
+ name,
4634
+ version: config2.version || "1.0.0",
4635
+ description: config2.description
4636
+ });
4637
+ return instance;
4638
+ }
4639
+ __name(createMCPServer, "createMCPServer");
4640
+
4002
4641
  // src/oauth-helper.ts
4003
4642
  var OAuthHelper = class {
4004
4643
  static {
@@ -5347,6 +5986,7 @@ var import_messages3 = require("@langchain/core/messages");
5347
5986
  Telemetry,
5348
5987
  ToolMessage,
5349
5988
  WebSocketConnector,
5989
+ createMCPServer,
5350
5990
  createOAuthMCPConfig,
5351
5991
  createReadableStreamFromGenerator,
5352
5992
  loadConfigFile,
package/dist/index.d.ts CHANGED
@@ -13,6 +13,8 @@ export * from './src/agents/utils/index.js';
13
13
  export { ServerManager } from './src/managers/server_manager.js';
14
14
  export * from './src/managers/tools/index.js';
15
15
  export { type ObservabilityConfig, ObservabilityManager } from './src/observability/index.js';
16
+ export { createMCPServer } from './src/server/index.js';
17
+ export type { InputDefinition, PromptDefinition, PromptHandler, ResourceDefinition, ResourceHandler, ServerConfig, ToolDefinition, ToolHandler, } from './src/server/types.js';
16
18
  export { setTelemetrySource, Telemetry } from './src/telemetry/index.js';
17
19
  export { OAuthHelper, LINEAR_OAUTH_CONFIG, createOAuthMCPConfig } from './src/oauth-helper.js';
18
20
  export type { OAuthConfig, OAuthDiscovery, ClientRegistration, OAuthResult, OAuthState } from './src/oauth-helper.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAA;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAA;AAElE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAA;AAEvE,cAAc,6BAA6B,CAAA;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAA;AAEhE,cAAc,+BAA+B,CAAA;AAG7C,OAAO,EAAE,KAAK,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAA;AAG7F,OAAO,EAAE,kBA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