@mastra/mcp 0.10.5-alpha.0 → 0.10.5-alpha.1

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.
@@ -39,6 +39,7 @@ import type {
39
39
  ResourceTemplate,
40
40
  ServerCapabilities,
41
41
  Prompt,
42
+ CallToolResult,
42
43
  } from '@modelcontextprotocol/sdk/types.js';
43
44
  import type { SSEStreamingApi } from 'hono/streaming';
44
45
  import { streamSSE } from 'hono/streaming';
@@ -320,6 +321,7 @@ export class MCPServer extends MCPServerBase {
320
321
  name: workflowToolName,
321
322
  description: coreTool.description,
322
323
  parameters: coreTool.parameters,
324
+ outputSchema: coreTool.outputSchema,
323
325
  execute: coreTool.execute!,
324
326
  toolType: 'workflow',
325
327
  };
@@ -369,6 +371,7 @@ export class MCPServer extends MCPServerBase {
369
371
  name: toolName,
370
372
  description: coreTool.description,
371
373
  parameters: coreTool.parameters,
374
+ outputSchema: coreTool.outputSchema,
372
375
  execute: coreTool.execute!,
373
376
  };
374
377
  this.logger.info(`Registered explicit tool: '${toolName}'`);
@@ -420,11 +423,17 @@ export class MCPServer extends MCPServerBase {
420
423
  this.server.setRequestHandler(ListToolsRequestSchema, async () => {
421
424
  this.logger.debug('Handling ListTools request');
422
425
  return {
423
- tools: Object.values(this.convertedTools).map(tool => ({
424
- name: tool.name,
425
- description: tool.description,
426
- inputSchema: tool.parameters.jsonSchema,
427
- })),
426
+ tools: Object.values(this.convertedTools).map(tool => {
427
+ const toolSpec: any = {
428
+ name: tool.name,
429
+ description: tool.description,
430
+ inputSchema: tool.parameters.jsonSchema,
431
+ };
432
+ if (tool.outputSchema) {
433
+ toolSpec.outputSchema = tool.outputSchema.jsonSchema;
434
+ }
435
+ return toolSpec;
436
+ }),
428
437
  };
429
438
  });
430
439
  }
@@ -449,8 +458,6 @@ export class MCPServer extends MCPServerBase {
449
458
  };
450
459
  }
451
460
 
452
- this.logger.debug(`CallTool: Invoking '${request.params.name}' with arguments:`, request.params.arguments);
453
-
454
461
  const validation = tool.parameters.validate?.(request.params.arguments ?? {});
455
462
  if (validation && !validation.success) {
456
463
  this.logger.warn(`CallTool: Invalid tool arguments for '${request.params.name}'`, {
@@ -470,17 +477,43 @@ export class MCPServer extends MCPServerBase {
470
477
  }
471
478
 
472
479
  const result = await tool.execute(validation?.value, { messages: [], toolCallId: '' });
480
+
481
+ this.logger.debug(`CallTool: Tool '${request.params.name}' executed successfully with result:`, result);
473
482
  const duration = Date.now() - startTime;
474
483
  this.logger.info(`Tool '${request.params.name}' executed successfully in ${duration}ms.`);
475
- return {
476
- content: [
484
+
485
+ const response: CallToolResult = { isError: false, content: [] };
486
+
487
+ if (tool.outputSchema) {
488
+ if (!result.structuredContent) {
489
+ throw new Error(`Tool ${request.params.name} has an output schema but no structured content was provided.`);
490
+ }
491
+ const outputValidation = tool.outputSchema.validate?.(result.structuredContent ?? {});
492
+ if (outputValidation && !outputValidation.success) {
493
+ this.logger.warn(`CallTool: Invalid structured content for '${request.params.name}'`, {
494
+ errors: outputValidation.error,
495
+ });
496
+ throw new Error(
497
+ `Invalid structured content for tool ${request.params.name}: ${JSON.stringify(outputValidation.error)}`,
498
+ );
499
+ }
500
+ response.structuredContent = result.structuredContent;
501
+ }
502
+
503
+ if (result.content) {
504
+ response.content = result.content;
505
+ } else if (response.structuredContent) {
506
+ response.content = [{ type: 'text', text: JSON.stringify(response.structuredContent) }];
507
+ } else {
508
+ response.content = [
477
509
  {
478
510
  type: 'text',
479
511
  text: typeof result === 'string' ? result : JSON.stringify(result),
480
512
  },
481
- ],
482
- isError: false,
483
- };
513
+ ];
514
+ }
515
+
516
+ return response;
484
517
  } catch (error) {
485
518
  const duration = Date.now() - startTime;
486
519
  if (error instanceof z.ZodError) {
@@ -664,7 +697,7 @@ export class MCPServer extends MCPServerBase {
664
697
  return;
665
698
  }
666
699
  this.subscribeResourceHandlerIsRegistered = true;
667
- this.server.setRequestHandler(SubscribeRequestSchema as any, async (request: { params: { uri: string } }) => {
700
+ this.server.setRequestHandler(SubscribeRequestSchema, async (request: { params: { uri: string } }) => {
668
701
  const uri = request.params.uri;
669
702
  this.logger.info(`Received resources/subscribe request for URI: ${uri}`);
670
703
  this.subscriptions.add(uri);
@@ -681,7 +714,7 @@ export class MCPServer extends MCPServerBase {
681
714
  }
682
715
  this.unsubscribeResourceHandlerIsRegistered = true;
683
716
 
684
- this.server.setRequestHandler(UnsubscribeRequestSchema as any, async (request: { params: { uri: string } }) => {
717
+ this.server.setRequestHandler(UnsubscribeRequestSchema, async (request: { params: { uri: string } }) => {
685
718
  const uri = request.params.uri;
686
719
  this.logger.info(`Received resources/unsubscribe request for URI: ${uri}`);
687
720
  this.subscriptions.delete(uri);
@@ -1283,7 +1316,7 @@ export class MCPServer extends MCPServerBase {
1283
1316
  * @returns An object containing an array of tool information.
1284
1317
  */
1285
1318
  public getToolListInfo(): {
1286
- tools: Array<{ name: string; description?: string; inputSchema: any; toolType?: MCPToolType }>;
1319
+ tools: Array<{ name: string; description?: string; inputSchema: any; outputSchema?: any; toolType?: MCPToolType }>;
1287
1320
  } {
1288
1321
  this.logger.debug(`Getting tool list information for MCPServer '${this.name}'`);
1289
1322
  return {
@@ -1292,6 +1325,7 @@ export class MCPServer extends MCPServerBase {
1292
1325
  name: tool.name,
1293
1326
  description: tool.description,
1294
1327
  inputSchema: tool.parameters?.jsonSchema || tool.parameters,
1328
+ outputSchema: tool.outputSchema?.jsonSchema || tool.outputSchema,
1295
1329
  toolType: tool.toolType,
1296
1330
  })),
1297
1331
  };
@@ -1304,7 +1338,7 @@ export class MCPServer extends MCPServerBase {
1304
1338
  */
1305
1339
  public getToolInfo(
1306
1340
  toolId: string,
1307
- ): { name: string; description?: string; inputSchema: any; toolType?: MCPToolType } | undefined {
1341
+ ): { name: string; description?: string; inputSchema: any; outputSchema?: any; toolType?: MCPToolType } | undefined {
1308
1342
  const tool = this.convertedTools[toolId];
1309
1343
  if (!tool) {
1310
1344
  this.logger.debug(`Tool '${toolId}' not found on MCPServer '${this.name}'`);
@@ -1315,6 +1349,7 @@ export class MCPServer extends MCPServerBase {
1315
1349
  name: tool.name,
1316
1350
  description: tool.description,
1317
1351
  inputSchema: tool.parameters?.jsonSchema || tool.parameters,
1352
+ outputSchema: tool.outputSchema?.jsonSchema || tool.outputSchema,
1318
1353
  toolType: tool.toolType,
1319
1354
  };
1320
1355
  }