@mastra/mcp 0.10.5 → 0.10.6-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.
- package/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +24 -2
- package/dist/_tsup-dts-rollup.d.cts +7 -0
- package/dist/_tsup-dts-rollup.d.ts +7 -0
- package/dist/index.cjs +25 -17
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +24 -17
- package/integration-tests/node_modules/.bin/vitest +2 -2
- package/integration-tests/package.json +2 -2
- package/package.json +6 -5
- package/src/client/client.ts +12 -10
- package/src/server/server.test.ts +81 -6
- package/src/server/server.ts +14 -6
- package/src/server/types.ts +7 -2
- package/integration-tests/node_modules/.bin/mastra +0 -21
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/mcp@0.10.
|
|
2
|
+
> @mastra/mcp@0.10.6-alpha.1 build /home/runner/work/mastra/mastra/packages/mcp
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
6
6
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
7
|
[34mCLI[39m tsup v8.5.0
|
|
8
8
|
[34mTSC[39m Build start
|
|
9
|
-
[32mTSC[39m ⚡️ Build success in
|
|
9
|
+
[32mTSC[39m ⚡️ Build success in 17864ms
|
|
10
10
|
[34mDTS[39m Build start
|
|
11
11
|
[34mCLI[39m Target: es2022
|
|
12
12
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
13
13
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/mcp/dist/_tsup-dts-rollup.d.ts[39m
|
|
14
14
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
15
15
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/mcp/dist/_tsup-dts-rollup.d.cts[39m
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 29258ms
|
|
17
17
|
[34mCLI[39m Cleaning output folder
|
|
18
18
|
[34mESM[39m Build start
|
|
19
19
|
[34mCJS[39m Build start
|
|
20
|
-
[32mESM[39m [1mdist/index.js [22m[32m88.
|
|
21
|
-
[32mESM[39m ⚡️ Build success in
|
|
22
|
-
[32mCJS[39m [1mdist/index.cjs [22m[32m89.
|
|
23
|
-
[32mCJS[39m ⚡️ Build success in
|
|
20
|
+
[32mESM[39m [1mdist/index.js [22m[32m88.94 KB[39m
|
|
21
|
+
[32mESM[39m ⚡️ Build success in 2045ms
|
|
22
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m89.72 KB[39m
|
|
23
|
+
[32mCJS[39m ⚡️ Build success in 2045ms
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @mastra/mcp
|
|
2
2
|
|
|
3
|
+
## 0.10.6-alpha.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- c0ba5e2: Handle json schema with $defs and $ref property for MCP tool schemas.
|
|
8
|
+
- cb16baf: Fix MCP tool output schema type and return value
|
|
9
|
+
- Updated dependencies [c7bbf1e]
|
|
10
|
+
- Updated dependencies [8722d53]
|
|
11
|
+
- Updated dependencies [132027f]
|
|
12
|
+
- Updated dependencies [0c85311]
|
|
13
|
+
- Updated dependencies [cb16baf]
|
|
14
|
+
- @mastra/core@0.10.11-alpha.3
|
|
15
|
+
|
|
16
|
+
## 0.10.6-alpha.0
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- f5ec3a4: Fix tool calling args for mcp server to ensure validatedArgs are not undefined
|
|
21
|
+
- ab3bbff: Add support for extra arguments in mcp
|
|
22
|
+
- Updated dependencies [2873c7f]
|
|
23
|
+
- Updated dependencies [1c1c6a1]
|
|
24
|
+
- Updated dependencies [565cc0c]
|
|
25
|
+
- @mastra/core@0.10.11-alpha.2
|
|
26
|
+
|
|
3
27
|
## 0.10.5
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
|
@@ -296,7 +320,6 @@
|
|
|
296
320
|
Added a new `getResources()` method to the MCPClient class that allows clients to retrieve resources from connected MCP servers. Resources are data or content exposed by MCP servers that can be accessed by clients.
|
|
297
321
|
|
|
298
322
|
The implementation includes:
|
|
299
|
-
|
|
300
323
|
- Direct access to resources from all connected MCP servers, grouped by server name
|
|
301
324
|
- Robust error handling that allows partial results when some servers fail
|
|
302
325
|
- Comprehensive test coverage with real server implementation
|
|
@@ -375,7 +398,6 @@
|
|
|
375
398
|
Added a new `getResources()` method to the MCPClient class that allows clients to retrieve resources from connected MCP servers. Resources are data or content exposed by MCP servers that can be accessed by clients.
|
|
376
399
|
|
|
377
400
|
The implementation includes:
|
|
378
|
-
|
|
379
401
|
- Direct access to resources from all connected MCP servers, grouped by server name
|
|
380
402
|
- Robust error handling that allows partial results when some servers fail
|
|
381
403
|
- Comprehensive test coverage with real server implementation
|
|
@@ -20,6 +20,7 @@ import { objectOutputType } from 'zod';
|
|
|
20
20
|
import { objectUtil } from 'zod';
|
|
21
21
|
import type { Prompt } from '@modelcontextprotocol/sdk/types.js';
|
|
22
22
|
import type { PromptMessage } from '@modelcontextprotocol/sdk/types.js';
|
|
23
|
+
import type { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
23
24
|
import type { Resource } from '@modelcontextprotocol/sdk/types.js';
|
|
24
25
|
import type { ResourceTemplate } from '@modelcontextprotocol/sdk/types.js';
|
|
25
26
|
import { ResourceUpdatedNotificationSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
@@ -504,6 +505,11 @@ export { MCPConfigurationOptions }
|
|
|
504
505
|
export { MCPConfigurationOptions as MCPConfigurationOptions_alias_1 }
|
|
505
506
|
export { MCPConfigurationOptions as MCPConfigurationOptions_alias_2 }
|
|
506
507
|
|
|
508
|
+
declare type MCPRequestHandlerExtra = RequestHandlerExtra<any, any>;
|
|
509
|
+
export { MCPRequestHandlerExtra }
|
|
510
|
+
export { MCPRequestHandlerExtra as MCPRequestHandlerExtra_alias_1 }
|
|
511
|
+
export { MCPRequestHandlerExtra as MCPRequestHandlerExtra_alias_2 }
|
|
512
|
+
|
|
507
513
|
declare class MCPServer extends MCPServerBase {
|
|
508
514
|
private server;
|
|
509
515
|
private stdioTransport?;
|
|
@@ -740,6 +746,7 @@ declare type MCPTool = {
|
|
|
740
746
|
outputSchema?: InternalCoreTool['outputSchema'];
|
|
741
747
|
execute: (params: any, options: Parameters<NonNullable<InternalCoreTool['execute']>>[1] & {
|
|
742
748
|
elicitation: ElicitationActions;
|
|
749
|
+
extra: MCPRequestHandlerExtra;
|
|
743
750
|
}) => Promise<any>;
|
|
744
751
|
};
|
|
745
752
|
export { MCPTool }
|
|
@@ -20,6 +20,7 @@ import { objectOutputType } from 'zod';
|
|
|
20
20
|
import { objectUtil } from 'zod';
|
|
21
21
|
import type { Prompt } from '@modelcontextprotocol/sdk/types.js';
|
|
22
22
|
import type { PromptMessage } from '@modelcontextprotocol/sdk/types.js';
|
|
23
|
+
import type { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
23
24
|
import type { Resource } from '@modelcontextprotocol/sdk/types.js';
|
|
24
25
|
import type { ResourceTemplate } from '@modelcontextprotocol/sdk/types.js';
|
|
25
26
|
import { ResourceUpdatedNotificationSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
@@ -504,6 +505,11 @@ export { MCPConfigurationOptions }
|
|
|
504
505
|
export { MCPConfigurationOptions as MCPConfigurationOptions_alias_1 }
|
|
505
506
|
export { MCPConfigurationOptions as MCPConfigurationOptions_alias_2 }
|
|
506
507
|
|
|
508
|
+
declare type MCPRequestHandlerExtra = RequestHandlerExtra<any, any>;
|
|
509
|
+
export { MCPRequestHandlerExtra }
|
|
510
|
+
export { MCPRequestHandlerExtra as MCPRequestHandlerExtra_alias_1 }
|
|
511
|
+
export { MCPRequestHandlerExtra as MCPRequestHandlerExtra_alias_2 }
|
|
512
|
+
|
|
507
513
|
declare class MCPServer extends MCPServerBase {
|
|
508
514
|
private server;
|
|
509
515
|
private stdioTransport?;
|
|
@@ -740,6 +746,7 @@ declare type MCPTool = {
|
|
|
740
746
|
outputSchema?: InternalCoreTool['outputSchema'];
|
|
741
747
|
execute: (params: any, options: Parameters<NonNullable<InternalCoreTool['execute']>>[1] & {
|
|
742
748
|
elicitation: ElicitationActions;
|
|
749
|
+
extra: MCPRequestHandlerExtra;
|
|
743
750
|
}) => Promise<any>;
|
|
744
751
|
};
|
|
745
752
|
export { MCPTool }
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var $RefParser = require('@apidevtools/json-schema-ref-parser');
|
|
3
4
|
var base = require('@mastra/core/base');
|
|
4
5
|
var error = require('@mastra/core/error');
|
|
5
6
|
var tools = require('@mastra/core/tools');
|
|
@@ -26,6 +27,7 @@ var streamableHttp_js = require('@modelcontextprotocol/sdk/server/streamableHttp
|
|
|
26
27
|
|
|
27
28
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
28
29
|
|
|
30
|
+
var $RefParser__default = /*#__PURE__*/_interopDefault($RefParser);
|
|
29
31
|
var equal__default = /*#__PURE__*/_interopDefault(equal);
|
|
30
32
|
|
|
31
33
|
// src/client/client.ts
|
|
@@ -504,11 +506,12 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
504
506
|
return handler(request.params);
|
|
505
507
|
});
|
|
506
508
|
}
|
|
507
|
-
convertInputSchema(inputSchema) {
|
|
509
|
+
async convertInputSchema(inputSchema) {
|
|
508
510
|
if (utils.isZodType(inputSchema)) {
|
|
509
511
|
return inputSchema;
|
|
510
512
|
}
|
|
511
513
|
try {
|
|
514
|
+
await $RefParser__default.default.dereference(inputSchema);
|
|
512
515
|
return zodFromJsonSchema.convertJsonSchemaToZod(inputSchema);
|
|
513
516
|
} catch (error$1) {
|
|
514
517
|
let errorDetails;
|
|
@@ -533,12 +536,13 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
533
536
|
});
|
|
534
537
|
}
|
|
535
538
|
}
|
|
536
|
-
convertOutputSchema(outputSchema) {
|
|
539
|
+
async convertOutputSchema(outputSchema) {
|
|
537
540
|
if (!outputSchema) return;
|
|
538
541
|
if (utils.isZodType(outputSchema)) {
|
|
539
542
|
return outputSchema;
|
|
540
543
|
}
|
|
541
544
|
try {
|
|
545
|
+
await $RefParser__default.default.dereference(outputSchema);
|
|
542
546
|
return zodFromJsonSchema.convertJsonSchemaToZod(outputSchema);
|
|
543
547
|
} catch (error$1) {
|
|
544
548
|
let errorDetails;
|
|
@@ -567,14 +571,14 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
567
571
|
this.log("debug", `Requesting tools from MCP server`);
|
|
568
572
|
const { tools: tools$1 } = await this.client.listTools({ timeout: this.timeout });
|
|
569
573
|
const toolsRes = {};
|
|
570
|
-
tools$1
|
|
574
|
+
for (const tool of tools$1) {
|
|
571
575
|
this.log("debug", `Processing tool: ${tool.name}`);
|
|
572
576
|
try {
|
|
573
577
|
const mastraTool = tools.createTool({
|
|
574
578
|
id: `${this.name}_${tool.name}`,
|
|
575
579
|
description: tool.description || "",
|
|
576
|
-
inputSchema: this.convertInputSchema(tool.inputSchema),
|
|
577
|
-
outputSchema: this.convertOutputSchema(tool.outputSchema),
|
|
580
|
+
inputSchema: await this.convertInputSchema(tool.inputSchema),
|
|
581
|
+
outputSchema: await this.convertOutputSchema(tool.outputSchema),
|
|
578
582
|
execute: async ({ context, runtimeContext }) => {
|
|
579
583
|
const previousContext = this.currentOperationContext;
|
|
580
584
|
this.currentOperationContext = runtimeContext || null;
|
|
@@ -612,7 +616,7 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
612
616
|
mcpToolDefinition: tool
|
|
613
617
|
});
|
|
614
618
|
}
|
|
615
|
-
}
|
|
619
|
+
}
|
|
616
620
|
return toolsRes;
|
|
617
621
|
}
|
|
618
622
|
};
|
|
@@ -1018,7 +1022,7 @@ var MCPConfiguration = class extends MCPClient {
|
|
|
1018
1022
|
}
|
|
1019
1023
|
};
|
|
1020
1024
|
|
|
1021
|
-
// ../../node_modules/.pnpm/hono@4.8.
|
|
1025
|
+
// ../../node_modules/.pnpm/hono@4.8.4/node_modules/hono/dist/utils/stream.js
|
|
1022
1026
|
var StreamingApi = class {
|
|
1023
1027
|
writer;
|
|
1024
1028
|
encoder;
|
|
@@ -1085,7 +1089,7 @@ var StreamingApi = class {
|
|
|
1085
1089
|
}
|
|
1086
1090
|
};
|
|
1087
1091
|
|
|
1088
|
-
// ../../node_modules/.pnpm/hono@4.8.
|
|
1092
|
+
// ../../node_modules/.pnpm/hono@4.8.4/node_modules/hono/dist/helper/streaming/utils.js
|
|
1089
1093
|
var isOldBunVersion = () => {
|
|
1090
1094
|
const version = typeof Bun !== "undefined" ? Bun.version : void 0;
|
|
1091
1095
|
if (version === void 0) {
|
|
@@ -1096,7 +1100,7 @@ var isOldBunVersion = () => {
|
|
|
1096
1100
|
return result;
|
|
1097
1101
|
};
|
|
1098
1102
|
|
|
1099
|
-
// ../../node_modules/.pnpm/hono@4.8.
|
|
1103
|
+
// ../../node_modules/.pnpm/hono@4.8.4/node_modules/hono/dist/utils/html.js
|
|
1100
1104
|
var HtmlEscapedCallbackPhase = {
|
|
1101
1105
|
Stringify: 1};
|
|
1102
1106
|
var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
|
|
@@ -1127,7 +1131,7 @@ var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) =>
|
|
|
1127
1131
|
}
|
|
1128
1132
|
};
|
|
1129
1133
|
|
|
1130
|
-
// ../../node_modules/.pnpm/hono@4.8.
|
|
1134
|
+
// ../../node_modules/.pnpm/hono@4.8.4/node_modules/hono/dist/helper/streaming/sse.js
|
|
1131
1135
|
var SSEStreamingApi = class extends StreamingApi {
|
|
1132
1136
|
constructor(writable, readable) {
|
|
1133
1137
|
super(writable, readable);
|
|
@@ -1523,7 +1527,7 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
1523
1527
|
})
|
|
1524
1528
|
};
|
|
1525
1529
|
});
|
|
1526
|
-
serverInstance.setRequestHandler(types_js.CallToolRequestSchema, async (request) => {
|
|
1530
|
+
serverInstance.setRequestHandler(types_js.CallToolRequestSchema, async (request, extra) => {
|
|
1527
1531
|
const startTime = Date.now();
|
|
1528
1532
|
try {
|
|
1529
1533
|
const tool = this.convertedTools[request.params.name];
|
|
@@ -1559,17 +1563,21 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
1559
1563
|
const result = await tool.execute(validation?.value, {
|
|
1560
1564
|
messages: [],
|
|
1561
1565
|
toolCallId: "",
|
|
1562
|
-
elicitation: sessionElicitation
|
|
1566
|
+
elicitation: sessionElicitation,
|
|
1567
|
+
extra
|
|
1563
1568
|
});
|
|
1564
1569
|
this.logger.debug(`CallTool: Tool '${request.params.name}' executed successfully with result:`, result);
|
|
1565
1570
|
const duration = Date.now() - startTime;
|
|
1566
1571
|
this.logger.info(`Tool '${request.params.name}' executed successfully in ${duration}ms.`);
|
|
1567
1572
|
const response = { isError: false, content: [] };
|
|
1568
1573
|
if (tool.outputSchema) {
|
|
1569
|
-
|
|
1570
|
-
|
|
1574
|
+
let structuredContent;
|
|
1575
|
+
if (result && typeof result === "object" && "structuredContent" in result) {
|
|
1576
|
+
structuredContent = result.structuredContent;
|
|
1577
|
+
} else {
|
|
1578
|
+
structuredContent = result;
|
|
1571
1579
|
}
|
|
1572
|
-
const outputValidation = tool.outputSchema.validate?.(
|
|
1580
|
+
const outputValidation = tool.outputSchema.validate?.(structuredContent ?? {});
|
|
1573
1581
|
if (outputValidation && !outputValidation.success) {
|
|
1574
1582
|
this.logger.warn(`CallTool: Invalid structured content for '${request.params.name}'`, {
|
|
1575
1583
|
errors: outputValidation.error
|
|
@@ -1578,7 +1586,7 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
1578
1586
|
`Invalid structured content for tool ${request.params.name}: ${JSON.stringify(outputValidation.error)}`
|
|
1579
1587
|
);
|
|
1580
1588
|
}
|
|
1581
|
-
response.structuredContent =
|
|
1589
|
+
response.structuredContent = structuredContent;
|
|
1582
1590
|
}
|
|
1583
1591
|
if (response.structuredContent) {
|
|
1584
1592
|
response.content = [{ type: "text", text: JSON.stringify(response.structuredContent) }];
|
|
@@ -2480,7 +2488,7 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
2480
2488
|
*/
|
|
2481
2489
|
async executeTool(toolId, args, executionContext) {
|
|
2482
2490
|
const tool = this.convertedTools[toolId];
|
|
2483
|
-
let validatedArgs;
|
|
2491
|
+
let validatedArgs = args;
|
|
2484
2492
|
try {
|
|
2485
2493
|
if (!tool) {
|
|
2486
2494
|
this.logger.warn(`ExecuteTool: Unknown tool '${toolId}' requested on MCPServer '${this.name}'.`);
|
package/dist/index.d.cts
CHANGED
|
@@ -15,6 +15,7 @@ export { MCPServerResources } from './_tsup-dts-rollup.cjs';
|
|
|
15
15
|
export { MCPServerPromptMessagesCallback } from './_tsup-dts-rollup.cjs';
|
|
16
16
|
export { MCPServerPrompts } from './_tsup-dts-rollup.cjs';
|
|
17
17
|
export { ElicitationActions } from './_tsup-dts-rollup.cjs';
|
|
18
|
+
export { MCPRequestHandlerExtra } from './_tsup-dts-rollup.cjs';
|
|
18
19
|
export { MCPTool } from './_tsup-dts-rollup.cjs';
|
|
19
20
|
export { Resource } from './_tsup-dts-rollup.cjs';
|
|
20
21
|
export { ResourceTemplate } from './_tsup-dts-rollup.cjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export { MCPServerResources } from './_tsup-dts-rollup.js';
|
|
|
15
15
|
export { MCPServerPromptMessagesCallback } from './_tsup-dts-rollup.js';
|
|
16
16
|
export { MCPServerPrompts } from './_tsup-dts-rollup.js';
|
|
17
17
|
export { ElicitationActions } from './_tsup-dts-rollup.js';
|
|
18
|
+
export { MCPRequestHandlerExtra } from './_tsup-dts-rollup.js';
|
|
18
19
|
export { MCPTool } from './_tsup-dts-rollup.js';
|
|
19
20
|
export { Resource } from './_tsup-dts-rollup.js';
|
|
20
21
|
export { ResourceTemplate } from './_tsup-dts-rollup.js';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import $RefParser from '@apidevtools/json-schema-ref-parser';
|
|
1
2
|
import { MastraBase } from '@mastra/core/base';
|
|
2
3
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
3
4
|
import { createTool as createTool$1 } from '@mastra/core/tools';
|
|
@@ -498,11 +499,12 @@ var InternalMastraMCPClient = class extends MastraBase {
|
|
|
498
499
|
return handler(request.params);
|
|
499
500
|
});
|
|
500
501
|
}
|
|
501
|
-
convertInputSchema(inputSchema) {
|
|
502
|
+
async convertInputSchema(inputSchema) {
|
|
502
503
|
if (isZodType(inputSchema)) {
|
|
503
504
|
return inputSchema;
|
|
504
505
|
}
|
|
505
506
|
try {
|
|
507
|
+
await $RefParser.dereference(inputSchema);
|
|
506
508
|
return convertJsonSchemaToZod(inputSchema);
|
|
507
509
|
} catch (error) {
|
|
508
510
|
let errorDetails;
|
|
@@ -527,12 +529,13 @@ var InternalMastraMCPClient = class extends MastraBase {
|
|
|
527
529
|
});
|
|
528
530
|
}
|
|
529
531
|
}
|
|
530
|
-
convertOutputSchema(outputSchema) {
|
|
532
|
+
async convertOutputSchema(outputSchema) {
|
|
531
533
|
if (!outputSchema) return;
|
|
532
534
|
if (isZodType(outputSchema)) {
|
|
533
535
|
return outputSchema;
|
|
534
536
|
}
|
|
535
537
|
try {
|
|
538
|
+
await $RefParser.dereference(outputSchema);
|
|
536
539
|
return convertJsonSchemaToZod(outputSchema);
|
|
537
540
|
} catch (error) {
|
|
538
541
|
let errorDetails;
|
|
@@ -561,14 +564,14 @@ var InternalMastraMCPClient = class extends MastraBase {
|
|
|
561
564
|
this.log("debug", `Requesting tools from MCP server`);
|
|
562
565
|
const { tools } = await this.client.listTools({ timeout: this.timeout });
|
|
563
566
|
const toolsRes = {};
|
|
564
|
-
|
|
567
|
+
for (const tool of tools) {
|
|
565
568
|
this.log("debug", `Processing tool: ${tool.name}`);
|
|
566
569
|
try {
|
|
567
570
|
const mastraTool = createTool$1({
|
|
568
571
|
id: `${this.name}_${tool.name}`,
|
|
569
572
|
description: tool.description || "",
|
|
570
|
-
inputSchema: this.convertInputSchema(tool.inputSchema),
|
|
571
|
-
outputSchema: this.convertOutputSchema(tool.outputSchema),
|
|
573
|
+
inputSchema: await this.convertInputSchema(tool.inputSchema),
|
|
574
|
+
outputSchema: await this.convertOutputSchema(tool.outputSchema),
|
|
572
575
|
execute: async ({ context, runtimeContext }) => {
|
|
573
576
|
const previousContext = this.currentOperationContext;
|
|
574
577
|
this.currentOperationContext = runtimeContext || null;
|
|
@@ -606,7 +609,7 @@ var InternalMastraMCPClient = class extends MastraBase {
|
|
|
606
609
|
mcpToolDefinition: tool
|
|
607
610
|
});
|
|
608
611
|
}
|
|
609
|
-
}
|
|
612
|
+
}
|
|
610
613
|
return toolsRes;
|
|
611
614
|
}
|
|
612
615
|
};
|
|
@@ -1012,7 +1015,7 @@ var MCPConfiguration = class extends MCPClient {
|
|
|
1012
1015
|
}
|
|
1013
1016
|
};
|
|
1014
1017
|
|
|
1015
|
-
// ../../node_modules/.pnpm/hono@4.8.
|
|
1018
|
+
// ../../node_modules/.pnpm/hono@4.8.4/node_modules/hono/dist/utils/stream.js
|
|
1016
1019
|
var StreamingApi = class {
|
|
1017
1020
|
writer;
|
|
1018
1021
|
encoder;
|
|
@@ -1079,7 +1082,7 @@ var StreamingApi = class {
|
|
|
1079
1082
|
}
|
|
1080
1083
|
};
|
|
1081
1084
|
|
|
1082
|
-
// ../../node_modules/.pnpm/hono@4.8.
|
|
1085
|
+
// ../../node_modules/.pnpm/hono@4.8.4/node_modules/hono/dist/helper/streaming/utils.js
|
|
1083
1086
|
var isOldBunVersion = () => {
|
|
1084
1087
|
const version = typeof Bun !== "undefined" ? Bun.version : void 0;
|
|
1085
1088
|
if (version === void 0) {
|
|
@@ -1090,7 +1093,7 @@ var isOldBunVersion = () => {
|
|
|
1090
1093
|
return result;
|
|
1091
1094
|
};
|
|
1092
1095
|
|
|
1093
|
-
// ../../node_modules/.pnpm/hono@4.8.
|
|
1096
|
+
// ../../node_modules/.pnpm/hono@4.8.4/node_modules/hono/dist/utils/html.js
|
|
1094
1097
|
var HtmlEscapedCallbackPhase = {
|
|
1095
1098
|
Stringify: 1};
|
|
1096
1099
|
var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
|
|
@@ -1121,7 +1124,7 @@ var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) =>
|
|
|
1121
1124
|
}
|
|
1122
1125
|
};
|
|
1123
1126
|
|
|
1124
|
-
// ../../node_modules/.pnpm/hono@4.8.
|
|
1127
|
+
// ../../node_modules/.pnpm/hono@4.8.4/node_modules/hono/dist/helper/streaming/sse.js
|
|
1125
1128
|
var SSEStreamingApi = class extends StreamingApi {
|
|
1126
1129
|
constructor(writable, readable) {
|
|
1127
1130
|
super(writable, readable);
|
|
@@ -1517,7 +1520,7 @@ var MCPServer = class extends MCPServerBase {
|
|
|
1517
1520
|
})
|
|
1518
1521
|
};
|
|
1519
1522
|
});
|
|
1520
|
-
serverInstance.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
1523
|
+
serverInstance.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
|
1521
1524
|
const startTime = Date.now();
|
|
1522
1525
|
try {
|
|
1523
1526
|
const tool = this.convertedTools[request.params.name];
|
|
@@ -1553,17 +1556,21 @@ var MCPServer = class extends MCPServerBase {
|
|
|
1553
1556
|
const result = await tool.execute(validation?.value, {
|
|
1554
1557
|
messages: [],
|
|
1555
1558
|
toolCallId: "",
|
|
1556
|
-
elicitation: sessionElicitation
|
|
1559
|
+
elicitation: sessionElicitation,
|
|
1560
|
+
extra
|
|
1557
1561
|
});
|
|
1558
1562
|
this.logger.debug(`CallTool: Tool '${request.params.name}' executed successfully with result:`, result);
|
|
1559
1563
|
const duration = Date.now() - startTime;
|
|
1560
1564
|
this.logger.info(`Tool '${request.params.name}' executed successfully in ${duration}ms.`);
|
|
1561
1565
|
const response = { isError: false, content: [] };
|
|
1562
1566
|
if (tool.outputSchema) {
|
|
1563
|
-
|
|
1564
|
-
|
|
1567
|
+
let structuredContent;
|
|
1568
|
+
if (result && typeof result === "object" && "structuredContent" in result) {
|
|
1569
|
+
structuredContent = result.structuredContent;
|
|
1570
|
+
} else {
|
|
1571
|
+
structuredContent = result;
|
|
1565
1572
|
}
|
|
1566
|
-
const outputValidation = tool.outputSchema.validate?.(
|
|
1573
|
+
const outputValidation = tool.outputSchema.validate?.(structuredContent ?? {});
|
|
1567
1574
|
if (outputValidation && !outputValidation.success) {
|
|
1568
1575
|
this.logger.warn(`CallTool: Invalid structured content for '${request.params.name}'`, {
|
|
1569
1576
|
errors: outputValidation.error
|
|
@@ -1572,7 +1579,7 @@ var MCPServer = class extends MCPServerBase {
|
|
|
1572
1579
|
`Invalid structured content for tool ${request.params.name}: ${JSON.stringify(outputValidation.error)}`
|
|
1573
1580
|
);
|
|
1574
1581
|
}
|
|
1575
|
-
response.structuredContent =
|
|
1582
|
+
response.structuredContent = structuredContent;
|
|
1576
1583
|
}
|
|
1577
1584
|
if (response.structuredContent) {
|
|
1578
1585
|
response.content = [{ type: "text", text: JSON.stringify(response.structuredContent) }];
|
|
@@ -2474,7 +2481,7 @@ var MCPServer = class extends MCPServerBase {
|
|
|
2474
2481
|
*/
|
|
2475
2482
|
async executeTool(toolId, args, executionContext) {
|
|
2476
2483
|
const tool = this.convertedTools[toolId];
|
|
2477
|
-
let validatedArgs;
|
|
2484
|
+
let validatedArgs = args;
|
|
2478
2485
|
try {
|
|
2479
2486
|
if (!tool) {
|
|
2480
2487
|
this.logger.warn(`ExecuteTool: Unknown tool '${toolId}' requested on MCPServer '${this.name}'.`);
|
|
@@ -10,9 +10,9 @@ case `uname` in
|
|
|
10
10
|
esac
|
|
11
11
|
|
|
12
12
|
if [ -z "$NODE_PATH" ]; then
|
|
13
|
-
export NODE_PATH="/home/runner/work/mastra/mastra/node_modules/.pnpm/vitest@3.2.4_@types+debug@4.1.12_@types+node@20.19.
|
|
13
|
+
export NODE_PATH="/home/runner/work/mastra/mastra/node_modules/.pnpm/vitest@3.2.4_@types+debug@4.1.12_@types+node@20.19.2_@vitest+ui@3.2.3_jiti@2.4.2_jsdom@_bcb3b08cf6e48f0a4d90e52d1b155bc5/node_modules/vitest/node_modules:/home/runner/work/mastra/mastra/node_modules/.pnpm/vitest@3.2.4_@types+debug@4.1.12_@types+node@20.19.2_@vitest+ui@3.2.3_jiti@2.4.2_jsdom@_bcb3b08cf6e48f0a4d90e52d1b155bc5/node_modules:/home/runner/work/mastra/mastra/node_modules/.pnpm/node_modules"
|
|
14
14
|
else
|
|
15
|
-
export NODE_PATH="/home/runner/work/mastra/mastra/node_modules/.pnpm/vitest@3.2.4_@types+debug@4.1.12_@types+node@20.19.
|
|
15
|
+
export NODE_PATH="/home/runner/work/mastra/mastra/node_modules/.pnpm/vitest@3.2.4_@types+debug@4.1.12_@types+node@20.19.2_@vitest+ui@3.2.3_jiti@2.4.2_jsdom@_bcb3b08cf6e48f0a4d90e52d1b155bc5/node_modules/vitest/node_modules:/home/runner/work/mastra/mastra/node_modules/.pnpm/vitest@3.2.4_@types+debug@4.1.12_@types+node@20.19.2_@vitest+ui@3.2.3_jiti@2.4.2_jsdom@_bcb3b08cf6e48f0a4d90e52d1b155bc5/node_modules:/home/runner/work/mastra/mastra/node_modules/.pnpm/node_modules:$NODE_PATH"
|
|
16
16
|
fi
|
|
17
17
|
if [ -x "$basedir/node" ]; then
|
|
18
18
|
exec "$basedir/node" "$basedir/../vitest/vitest.mjs" "$@"
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"@ai-sdk/react": "^1.2.12",
|
|
12
12
|
"@mastra/client-js": "workspace:*",
|
|
13
13
|
"@mastra/mcp": "workspace:*",
|
|
14
|
-
"dotenv": "^16.
|
|
14
|
+
"dotenv": "^16.6.1",
|
|
15
15
|
"zod": "^3.25.67"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"get-port": "^7.1.0",
|
|
22
22
|
"mastra": "workspace:*",
|
|
23
23
|
"typescript": "^5.8.2",
|
|
24
|
-
"vitest": "^3.2.
|
|
24
|
+
"vitest": "^3.2.4"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"@mastra/core": "^0.10.0-alpha.0"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/mcp",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.6-alpha.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"author": "",
|
|
23
23
|
"license": "Elastic-2.0",
|
|
24
24
|
"dependencies": {
|
|
25
|
+
"@apidevtools/json-schema-ref-parser": "^14.1.0",
|
|
25
26
|
"@modelcontextprotocol/sdk": "^1.13.0",
|
|
26
27
|
"date-fns": "^4.1.0",
|
|
27
28
|
"exit-hook": "^4.0.0",
|
|
@@ -43,15 +44,15 @@
|
|
|
43
44
|
"ai": "4.3.16",
|
|
44
45
|
"eslint": "^9.29.0",
|
|
45
46
|
"hono-mcp-server-sse-transport": "0.0.7",
|
|
46
|
-
"hono": "^4.
|
|
47
|
+
"hono": "^4.8.4",
|
|
47
48
|
"tsup": "^8.5.0",
|
|
48
49
|
"tsx": "^4.19.4",
|
|
49
50
|
"typescript": "^5.8.3",
|
|
50
|
-
"vitest": "^3.2.
|
|
51
|
+
"vitest": "^3.2.4",
|
|
51
52
|
"zod": "^3.25.67",
|
|
52
53
|
"zod-to-json-schema": "^3.24.5",
|
|
53
|
-
"@internal/lint": "0.0.
|
|
54
|
-
"@mastra/core": "0.10.
|
|
54
|
+
"@internal/lint": "0.0.17",
|
|
55
|
+
"@mastra/core": "0.10.11-alpha.3"
|
|
55
56
|
},
|
|
56
57
|
"scripts": {
|
|
57
58
|
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|
package/src/client/client.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import $RefParser from '@apidevtools/json-schema-ref-parser';
|
|
1
2
|
import { MastraBase } from '@mastra/core/base';
|
|
2
|
-
|
|
3
3
|
import type { RuntimeContext } from '@mastra/core/di';
|
|
4
4
|
import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
|
|
5
5
|
import { createTool } from '@mastra/core/tools';
|
|
@@ -468,15 +468,16 @@ export class InternalMastraMCPClient extends MastraBase {
|
|
|
468
468
|
return handler(request.params);
|
|
469
469
|
});
|
|
470
470
|
}
|
|
471
|
-
|
|
472
|
-
private convertInputSchema(
|
|
471
|
+
|
|
472
|
+
private async convertInputSchema(
|
|
473
473
|
inputSchema: Awaited<ReturnType<Client['listTools']>>['tools'][0]['inputSchema'] | JSONSchema,
|
|
474
|
-
): z.ZodType {
|
|
474
|
+
): Promise<z.ZodType> {
|
|
475
475
|
if (isZodType(inputSchema)) {
|
|
476
476
|
return inputSchema;
|
|
477
477
|
}
|
|
478
478
|
|
|
479
479
|
try {
|
|
480
|
+
await $RefParser.dereference(inputSchema)
|
|
480
481
|
return convertJsonSchemaToZod(inputSchema as JSONSchema);
|
|
481
482
|
} catch (error: unknown) {
|
|
482
483
|
let errorDetails: string | undefined;
|
|
@@ -504,15 +505,16 @@ export class InternalMastraMCPClient extends MastraBase {
|
|
|
504
505
|
}
|
|
505
506
|
}
|
|
506
507
|
|
|
507
|
-
private convertOutputSchema(
|
|
508
|
+
private async convertOutputSchema(
|
|
508
509
|
outputSchema: Awaited<ReturnType<Client['listTools']>>['tools'][0]['outputSchema'] | JSONSchema,
|
|
509
|
-
): z.ZodType | undefined {
|
|
510
|
+
): Promise<z.ZodType | undefined> {
|
|
510
511
|
if (!outputSchema) return
|
|
511
512
|
if (isZodType(outputSchema)) {
|
|
512
513
|
return outputSchema;
|
|
513
514
|
}
|
|
514
515
|
|
|
515
516
|
try {
|
|
517
|
+
await $RefParser.dereference(outputSchema)
|
|
516
518
|
return convertJsonSchemaToZod(outputSchema as JSONSchema);
|
|
517
519
|
} catch (error: unknown) {
|
|
518
520
|
let errorDetails: string | undefined;
|
|
@@ -544,14 +546,14 @@ export class InternalMastraMCPClient extends MastraBase {
|
|
|
544
546
|
this.log('debug', `Requesting tools from MCP server`);
|
|
545
547
|
const { tools } = await this.client.listTools({ timeout: this.timeout });
|
|
546
548
|
const toolsRes: Record<string, any> = {};
|
|
547
|
-
|
|
549
|
+
for (const tool of tools) {
|
|
548
550
|
this.log('debug', `Processing tool: ${tool.name}`);
|
|
549
551
|
try {
|
|
550
552
|
const mastraTool = createTool({
|
|
551
553
|
id: `${this.name}_${tool.name}`,
|
|
552
554
|
description: tool.description || '',
|
|
553
|
-
inputSchema: this.convertInputSchema(tool.inputSchema),
|
|
554
|
-
outputSchema: this.convertOutputSchema(tool.outputSchema),
|
|
555
|
+
inputSchema: await this.convertInputSchema(tool.inputSchema),
|
|
556
|
+
outputSchema: await this.convertOutputSchema(tool.outputSchema),
|
|
555
557
|
execute: async ({ context, runtimeContext }: { context: any; runtimeContext?: RuntimeContext | null }) => {
|
|
556
558
|
const previousContext = this.currentOperationContext;
|
|
557
559
|
this.currentOperationContext = runtimeContext || null; // Set current context
|
|
@@ -592,7 +594,7 @@ export class InternalMastraMCPClient extends MastraBase {
|
|
|
592
594
|
mcpToolDefinition: tool,
|
|
593
595
|
});
|
|
594
596
|
}
|
|
595
|
-
}
|
|
597
|
+
}
|
|
596
598
|
|
|
597
599
|
return toolsRes;
|
|
598
600
|
}
|
|
@@ -24,7 +24,7 @@ import { weatherTool } from '../__fixtures__/tools';
|
|
|
24
24
|
import { InternalMastraMCPClient } from '../client/client';
|
|
25
25
|
import { MCPClient } from '../client/configuration';
|
|
26
26
|
import { MCPServer } from './server';
|
|
27
|
-
import type { MCPServerResources, MCPServerResourceContent } from './types';
|
|
27
|
+
import type { MCPServerResources, MCPServerResourceContent, MCPRequestHandlerExtra } from './types';
|
|
28
28
|
|
|
29
29
|
const PORT = 9100 + Math.floor(Math.random() * 1000);
|
|
30
30
|
let server: MCPServer;
|
|
@@ -937,12 +937,32 @@ describe('MCPServer', () => {
|
|
|
937
937
|
let server: MCPServer;
|
|
938
938
|
let client: MCPClient;
|
|
939
939
|
const PORT = 9200 + Math.floor(Math.random() * 1000);
|
|
940
|
+
const TOKEN = `<random-token>`;
|
|
940
941
|
|
|
941
942
|
beforeAll(async () => {
|
|
942
943
|
server = new MCPServer({
|
|
943
944
|
name: 'Test MCP Server',
|
|
944
945
|
version: '0.1.0',
|
|
945
|
-
tools: {
|
|
946
|
+
tools: {
|
|
947
|
+
weatherTool,
|
|
948
|
+
testAuthTool: {
|
|
949
|
+
description: 'Test tool to validate auth information from extra params',
|
|
950
|
+
parameters: z.object({
|
|
951
|
+
message: z.string().describe('Message to show to user'),
|
|
952
|
+
}),
|
|
953
|
+
execute: async (context, options) => {
|
|
954
|
+
const extra = options.extra as MCPRequestHandlerExtra;
|
|
955
|
+
|
|
956
|
+
return {
|
|
957
|
+
message: context.message,
|
|
958
|
+
sessionId: extra?.sessionId || null,
|
|
959
|
+
authInfo: extra?.authInfo || null,
|
|
960
|
+
requestId: extra?.requestId || null,
|
|
961
|
+
hasExtra: !!extra,
|
|
962
|
+
};
|
|
963
|
+
},
|
|
964
|
+
},
|
|
965
|
+
},
|
|
946
966
|
});
|
|
947
967
|
|
|
948
968
|
httpServer = http.createServer(async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
|
@@ -964,6 +984,9 @@ describe('MCPServer', () => {
|
|
|
964
984
|
servers: {
|
|
965
985
|
local: {
|
|
966
986
|
url: new URL(`http://localhost:${PORT}/http`),
|
|
987
|
+
requestInit: {
|
|
988
|
+
headers: { Authorization: `Bearer ${TOKEN}` },
|
|
989
|
+
},
|
|
967
990
|
},
|
|
968
991
|
},
|
|
969
992
|
});
|
|
@@ -1008,6 +1031,60 @@ describe('MCPServer', () => {
|
|
|
1008
1031
|
expect(toolResult).toHaveProperty('windSpeed');
|
|
1009
1032
|
expect(toolResult).toHaveProperty('windGust');
|
|
1010
1033
|
});
|
|
1034
|
+
|
|
1035
|
+
it('should pass auth information through extra parameter', async () => {
|
|
1036
|
+
const mockExtra: MCPRequestHandlerExtra = {
|
|
1037
|
+
signal: new AbortController().signal,
|
|
1038
|
+
sessionId: 'test-session-id',
|
|
1039
|
+
authInfo: {
|
|
1040
|
+
token: TOKEN,
|
|
1041
|
+
clientId: 'test-client-id',
|
|
1042
|
+
scopes: ['read'],
|
|
1043
|
+
},
|
|
1044
|
+
requestId: 'test-request-id',
|
|
1045
|
+
sendNotification: vi.fn(),
|
|
1046
|
+
sendRequest: vi.fn(),
|
|
1047
|
+
};
|
|
1048
|
+
|
|
1049
|
+
const mockRequest = {
|
|
1050
|
+
jsonrpc: '2.0' as const,
|
|
1051
|
+
id: 'test-request-1',
|
|
1052
|
+
method: 'tools/call' as const,
|
|
1053
|
+
params: {
|
|
1054
|
+
name: 'testAuthTool',
|
|
1055
|
+
arguments: {
|
|
1056
|
+
message: 'test auth',
|
|
1057
|
+
},
|
|
1058
|
+
},
|
|
1059
|
+
};
|
|
1060
|
+
|
|
1061
|
+
const serverInstance = server.getServer();
|
|
1062
|
+
|
|
1063
|
+
// @ts-ignore - this is a private property, but we need to access it to test the request handler
|
|
1064
|
+
const requestHandlers = serverInstance._requestHandlers;
|
|
1065
|
+
const callToolHandler = requestHandlers.get('tools/call');
|
|
1066
|
+
|
|
1067
|
+
expect(callToolHandler).toBeDefined();
|
|
1068
|
+
|
|
1069
|
+
const result = await callToolHandler(mockRequest, mockExtra);
|
|
1070
|
+
|
|
1071
|
+
expect(result).toBeDefined();
|
|
1072
|
+
expect(result.isError).toBe(false);
|
|
1073
|
+
expect(result.content).toBeInstanceOf(Array);
|
|
1074
|
+
expect(result.content.length).toBeGreaterThan(0);
|
|
1075
|
+
|
|
1076
|
+
const toolOutput = result.content[0];
|
|
1077
|
+
expect(toolOutput.type).toBe('text');
|
|
1078
|
+
const toolResult = JSON.parse(toolOutput.text);
|
|
1079
|
+
|
|
1080
|
+
expect(toolResult.message).toBe('test auth');
|
|
1081
|
+
expect(toolResult.hasExtra).toBe(true);
|
|
1082
|
+
expect(toolResult.sessionId).toBe('test-session-id');
|
|
1083
|
+
expect(toolResult.authInfo).toBeDefined();
|
|
1084
|
+
expect(toolResult.authInfo.token).toBe(TOKEN);
|
|
1085
|
+
expect(toolResult.authInfo.clientId).toBe('test-client-id');
|
|
1086
|
+
expect(toolResult.requestId).toBe('test-request-id');
|
|
1087
|
+
});
|
|
1011
1088
|
});
|
|
1012
1089
|
|
|
1013
1090
|
describe('MCPServer Hono SSE Transport', () => {
|
|
@@ -1712,10 +1789,8 @@ describe('MCPServer with Tool Output Schema', () => {
|
|
|
1712
1789
|
timestamp: z.string(),
|
|
1713
1790
|
}),
|
|
1714
1791
|
execute: async ({ input }: { input: string }) => ({
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
timestamp: mockDateISO,
|
|
1718
|
-
},
|
|
1792
|
+
processedInput: `processed: ${input}`,
|
|
1793
|
+
timestamp: mockDateISO,
|
|
1719
1794
|
}),
|
|
1720
1795
|
},
|
|
1721
1796
|
};
|
package/src/server/server.ts
CHANGED
|
@@ -231,7 +231,7 @@ export class MCPServer extends MCPServerBase {
|
|
|
231
231
|
});
|
|
232
232
|
|
|
233
233
|
// Call tool handler
|
|
234
|
-
serverInstance.setRequestHandler(CallToolRequestSchema, async request => {
|
|
234
|
+
serverInstance.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
|
235
235
|
const startTime = Date.now();
|
|
236
236
|
try {
|
|
237
237
|
const tool = this.convertedTools[request.params.name] as MCPTool;
|
|
@@ -272,6 +272,7 @@ export class MCPServer extends MCPServerBase {
|
|
|
272
272
|
messages: [],
|
|
273
273
|
toolCallId: '',
|
|
274
274
|
elicitation: sessionElicitation,
|
|
275
|
+
extra,
|
|
275
276
|
});
|
|
276
277
|
|
|
277
278
|
this.logger.debug(`CallTool: Tool '${request.params.name}' executed successfully with result:`, result);
|
|
@@ -281,10 +282,17 @@ export class MCPServer extends MCPServerBase {
|
|
|
281
282
|
const response: CallToolResult = { isError: false, content: [] };
|
|
282
283
|
|
|
283
284
|
if (tool.outputSchema) {
|
|
284
|
-
|
|
285
|
-
|
|
285
|
+
// Handle both cases: tools that return { structuredContent: ... } and tools that return the plain object
|
|
286
|
+
let structuredContent;
|
|
287
|
+
if (result && typeof result === 'object' && 'structuredContent' in result) {
|
|
288
|
+
// Tool returned { structuredContent: ... } format (MCP-aware tool)
|
|
289
|
+
structuredContent = result.structuredContent;
|
|
290
|
+
} else {
|
|
291
|
+
// Tool returned plain object, wrap it automatically for backward compatibility
|
|
292
|
+
structuredContent = result;
|
|
286
293
|
}
|
|
287
|
-
|
|
294
|
+
|
|
295
|
+
const outputValidation = tool.outputSchema.validate?.(structuredContent ?? {});
|
|
288
296
|
if (outputValidation && !outputValidation.success) {
|
|
289
297
|
this.logger.warn(`CallTool: Invalid structured content for '${request.params.name}'`, {
|
|
290
298
|
errors: outputValidation.error,
|
|
@@ -293,7 +301,7 @@ export class MCPServer extends MCPServerBase {
|
|
|
293
301
|
`Invalid structured content for tool ${request.params.name}: ${JSON.stringify(outputValidation.error)}`,
|
|
294
302
|
);
|
|
295
303
|
}
|
|
296
|
-
response.structuredContent =
|
|
304
|
+
response.structuredContent = structuredContent;
|
|
297
305
|
}
|
|
298
306
|
|
|
299
307
|
if (response.structuredContent) {
|
|
@@ -1335,7 +1343,7 @@ export class MCPServer extends MCPServerBase {
|
|
|
1335
1343
|
executionContext?: { messages?: any[]; toolCallId?: string },
|
|
1336
1344
|
): Promise<any> {
|
|
1337
1345
|
const tool = this.convertedTools[toolId];
|
|
1338
|
-
let validatedArgs
|
|
1346
|
+
let validatedArgs = args;
|
|
1339
1347
|
try {
|
|
1340
1348
|
if (!tool) {
|
|
1341
1349
|
this.logger.warn(`ExecuteTool: Unknown tool '${toolId}' requested on MCPServer '${this.name}'.`);
|
package/src/server/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { InternalCoreTool } from '@mastra/core';
|
|
2
|
+
import type { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
2
3
|
import type {
|
|
3
4
|
ElicitRequest,
|
|
4
5
|
ElicitResult,
|
|
@@ -39,15 +40,19 @@ export type ElicitationActions = {
|
|
|
39
40
|
sendRequest: (request: ElicitRequest['params']) => Promise<ElicitResult>;
|
|
40
41
|
};
|
|
41
42
|
|
|
43
|
+
export type MCPRequestHandlerExtra = RequestHandlerExtra<any, any>;
|
|
44
|
+
|
|
42
45
|
export type MCPTool = {
|
|
43
46
|
id?: InternalCoreTool['id'];
|
|
44
47
|
description?: InternalCoreTool['description'];
|
|
45
48
|
parameters: InternalCoreTool['parameters'];
|
|
46
49
|
outputSchema?: InternalCoreTool['outputSchema'];
|
|
47
|
-
// Patch type to include elicitation in execute options
|
|
48
50
|
execute: (
|
|
49
51
|
params: any,
|
|
50
|
-
options: Parameters<NonNullable<InternalCoreTool['execute']>>[1] & {
|
|
52
|
+
options: Parameters<NonNullable<InternalCoreTool['execute']>>[1] & {
|
|
53
|
+
elicitation: ElicitationActions;
|
|
54
|
+
extra: MCPRequestHandlerExtra;
|
|
55
|
+
},
|
|
51
56
|
) => Promise<any>;
|
|
52
57
|
};
|
|
53
58
|
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
#!/bin/sh
|
|
2
|
-
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
|
|
3
|
-
|
|
4
|
-
case `uname` in
|
|
5
|
-
*CYGWIN*|*MINGW*|*MSYS*)
|
|
6
|
-
if command -v cygpath > /dev/null 2>&1; then
|
|
7
|
-
basedir=`cygpath -w "$basedir"`
|
|
8
|
-
fi
|
|
9
|
-
;;
|
|
10
|
-
esac
|
|
11
|
-
|
|
12
|
-
if [ -z "$NODE_PATH" ]; then
|
|
13
|
-
export NODE_PATH="/home/runner/work/mastra/mastra/packages/cli/dist/node_modules:/home/runner/work/mastra/mastra/packages/cli/node_modules:/home/runner/work/mastra/mastra/packages/node_modules:/home/runner/work/mastra/mastra/node_modules:/home/runner/work/mastra/node_modules:/home/runner/work/node_modules:/home/runner/node_modules:/home/node_modules:/node_modules:/home/runner/work/mastra/mastra/node_modules/.pnpm/node_modules"
|
|
14
|
-
else
|
|
15
|
-
export NODE_PATH="/home/runner/work/mastra/mastra/packages/cli/dist/node_modules:/home/runner/work/mastra/mastra/packages/cli/node_modules:/home/runner/work/mastra/mastra/packages/node_modules:/home/runner/work/mastra/mastra/node_modules:/home/runner/work/mastra/node_modules:/home/runner/work/node_modules:/home/runner/node_modules:/home/node_modules:/node_modules:/home/runner/work/mastra/mastra/node_modules/.pnpm/node_modules:$NODE_PATH"
|
|
16
|
-
fi
|
|
17
|
-
if [ -x "$basedir/node" ]; then
|
|
18
|
-
exec "$basedir/node" "$basedir/../mastra/dist/index.js" "$@"
|
|
19
|
-
else
|
|
20
|
-
exec node "$basedir/../mastra/dist/index.js" "$@"
|
|
21
|
-
fi
|