@mastra/mcp 1.4.0 → 1.4.1-alpha.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/index.js CHANGED
@@ -127,7 +127,8 @@ var PromptClientActions = class {
127
127
  if (response && response.prompts && Array.isArray(response.prompts)) {
128
128
  return response.prompts.map((prompt) => ({ ...prompt }));
129
129
  } else {
130
- this.logger.warn(`Prompts response from server ${this.client.name} did not have expected structure.`, {
130
+ this.logger.warn("Prompts response did not have expected structure", {
131
+ server: this.client.name,
131
132
  response
132
133
  });
133
134
  return [];
@@ -136,7 +137,8 @@ var PromptClientActions = class {
136
137
  if (e.code === ErrorCode.MethodNotFound) {
137
138
  return [];
138
139
  }
139
- this.logger.error(`Error getting prompts from server ${this.client.name}`, {
140
+ this.logger.error("Error getting prompts from server", {
141
+ server: this.client.name,
140
142
  error: e instanceof Error ? e.message : String(e)
141
143
  });
142
144
  throw new Error(
@@ -225,7 +227,8 @@ var ResourceClientActions = class {
225
227
  if (response && response.resources && Array.isArray(response.resources)) {
226
228
  return response.resources;
227
229
  } else {
228
- this.logger.warn(`Resources response from server ${this.client.name} did not have expected structure.`, {
230
+ this.logger.warn("Resources response did not have expected structure", {
231
+ server: this.client.name,
229
232
  response
230
233
  });
231
234
  return [];
@@ -234,7 +237,8 @@ var ResourceClientActions = class {
234
237
  if (e.code === ErrorCode.MethodNotFound) {
235
238
  return [];
236
239
  }
237
- this.logger.error(`Error getting resources from server ${this.client.name}`, {
240
+ this.logger.error("Error getting resources from server", {
241
+ server: this.client.name,
238
242
  error: e instanceof Error ? e.message : String(e)
239
243
  });
240
244
  throw new Error(
@@ -265,17 +269,18 @@ var ResourceClientActions = class {
265
269
  if (response && response.resourceTemplates && Array.isArray(response.resourceTemplates)) {
266
270
  return response.resourceTemplates;
267
271
  } else {
268
- this.logger.warn(
269
- `Resource templates response from server ${this.client.name} did not have expected structure.`,
270
- { response }
271
- );
272
+ this.logger.warn("Resource templates response did not have expected structure", {
273
+ server: this.client.name,
274
+ response
275
+ });
272
276
  return [];
273
277
  }
274
278
  } catch (e) {
275
279
  if (e.code === ErrorCode.MethodNotFound) {
276
280
  return [];
277
281
  }
278
- this.logger.error(`Error getting resource templates from server ${this.client.name}`, {
282
+ this.logger.error("Error getting resource templates from server", {
283
+ server: this.client.name,
279
284
  error: e instanceof Error ? e.message : String(e)
280
285
  });
281
286
  throw new Error(
@@ -1794,7 +1799,7 @@ To fix this you have three different options:
1794
1799
  }
1795
1800
  const exists = this.mcpClientsById.has(name);
1796
1801
  const existingClient = this.mcpClientsById.get(name);
1797
- this.logger.debug(`getConnectedClient ${name} exists: ${exists}`);
1802
+ this.logger.debug("Checking connected client", { name, exists });
1798
1803
  if (exists) {
1799
1804
  if (!existingClient) {
1800
1805
  throw new Error(`Client ${name} exists but is undefined`);
@@ -1802,7 +1807,7 @@ To fix this you have three different options:
1802
1807
  await existingClient.connect();
1803
1808
  return existingClient;
1804
1809
  }
1805
- this.logger.debug(`Connecting to ${name} MCP server`);
1810
+ this.logger.debug("Connecting to MCP server", { name });
1806
1811
  const mcpClient = new InternalMastraMCPClient({
1807
1812
  name,
1808
1813
  server: config,
@@ -1830,7 +1835,7 @@ To fix this you have three different options:
1830
1835
  this.mcpClientsById.delete(name);
1831
1836
  throw mastraError;
1832
1837
  }
1833
- this.logger.debug(`Connected to ${name} MCP server`);
1838
+ this.logger.debug("Connected to MCP server", { name });
1834
1839
  return mcpClient;
1835
1840
  }
1836
1841
  async getConnectedClientForServer(serverName) {
@@ -2607,9 +2612,13 @@ var MCPServer = class extends MCPServerBase {
2607
2612
  ...this.instructions ? { instructions: this.instructions } : {}
2608
2613
  }
2609
2614
  );
2610
- this.logger.info(
2611
- `Initialized MCPServer '${this.name}' v${this.version} (ID: ${this.id}) with tools: ${Object.keys(this.convertedTools).join(", ")} and resources. Capabilities: ${JSON.stringify(capabilities)}`
2612
- );
2615
+ this.logger.info("Initialized MCPServer", {
2616
+ name: this.name,
2617
+ version: this.version,
2618
+ id: this.id,
2619
+ tools: Object.keys(this.convertedTools),
2620
+ capabilities
2621
+ });
2613
2622
  this.sseHonoTransports = /* @__PURE__ */ new Map();
2614
2623
  this.registerHandlersOnServer(this.server);
2615
2624
  this.resources = new ServerResourceActions({
@@ -2646,10 +2655,10 @@ var MCPServer = class extends MCPServerBase {
2646
2655
  * @returns Promise that resolves to the client's response
2647
2656
  */
2648
2657
  async handleElicitationRequest(request, serverInstance, options) {
2649
- this.logger.debug(`Sending elicitation request: ${request.message}`);
2658
+ this.logger.debug("Sending elicitation request", { message: request.message });
2650
2659
  const server = serverInstance || this.server;
2651
2660
  const response = await server.elicitInput(request, options);
2652
- this.logger.debug(`Received elicitation response: ${JSON.stringify(response)}`);
2661
+ this.logger.debug("Received elicitation response", { response });
2653
2662
  return response;
2654
2663
  }
2655
2664
  /**
@@ -2721,7 +2730,6 @@ var MCPServer = class extends MCPServerBase {
2721
2730
  */
2722
2731
  registerHandlersOnServer(serverInstance) {
2723
2732
  serverInstance.setRequestHandler(ListToolsRequestSchema, async () => {
2724
- this.logger.debug("Handling ListTools request");
2725
2733
  return {
2726
2734
  tools: Object.values(this.convertedTools).map((tool) => {
2727
2735
  const toolSpec = {
@@ -2747,7 +2755,7 @@ var MCPServer = class extends MCPServerBase {
2747
2755
  try {
2748
2756
  const tool = this.convertedTools[request.params.name];
2749
2757
  if (!tool) {
2750
- this.logger.warn(`CallTool: Unknown tool '${request.params.name}' requested.`);
2758
+ this.logger.warn("Unknown tool requested", { tool: request.params.name });
2751
2759
  return {
2752
2760
  content: [{ type: "text", text: `Unknown tool: ${request.params.name}` }],
2753
2761
  isError: true
@@ -2755,7 +2763,8 @@ var MCPServer = class extends MCPServerBase {
2755
2763
  }
2756
2764
  const validation = tool.parameters.validate?.(request.params.arguments ?? {});
2757
2765
  if (validation && !validation.success) {
2758
- this.logger.warn(`CallTool: Invalid tool arguments for '${request.params.name}'`, {
2766
+ this.logger.warn("Invalid tool arguments", {
2767
+ tool: request.params.name,
2759
2768
  errors: validation.error
2760
2769
  });
2761
2770
  let errorMessages = "Validation failed";
@@ -2779,7 +2788,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2779
2788
  };
2780
2789
  }
2781
2790
  if (!tool.execute) {
2782
- this.logger.warn(`CallTool: Tool '${request.params.name}' does not have an execute function.`);
2791
+ this.logger.warn("Tool does not have an execute function", { tool: request.params.name });
2783
2792
  return {
2784
2793
  content: [{ type: "text", text: `Tool '${request.params.name}' does not have an execute function.` }],
2785
2794
  isError: true
@@ -2836,7 +2845,8 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2836
2845
  }
2837
2846
  const outputValidation = tool.outputSchema.validate?.(structuredContent ?? {});
2838
2847
  if (outputValidation && !outputValidation.success) {
2839
- this.logger.warn(`CallTool: Invalid structured content for '${request.params.name}'`, {
2848
+ this.logger.warn("Invalid structured content", {
2849
+ tool: request.params.name,
2840
2850
  errors: outputValidation.error
2841
2851
  });
2842
2852
  throw new Error(
@@ -2875,7 +2885,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2875
2885
  isError: true
2876
2886
  };
2877
2887
  }
2878
- this.logger.error(`Tool execution failed: ${request.params.name}`, { error });
2888
+ this.logger.error("Tool execution failed", { tool: request.params.name, error });
2879
2889
  if (error instanceof MastraError) {
2880
2890
  return {
2881
2891
  content: [{ type: "text", text: JSON.stringify(error.toJSON()) }],
@@ -2890,7 +2900,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2890
2900
  });
2891
2901
  serverInstance.setRequestHandler(SetLevelRequestSchema, async (request) => {
2892
2902
  this.currentLoggingLevel = request.params.level;
2893
- this.logger.debug(`Logging level set to: ${request.params.level}`);
2903
+ this.logger.debug("Logging level set", { level: request.params.level });
2894
2904
  return {};
2895
2905
  });
2896
2906
  if (this.resourceOptions) {
@@ -2908,17 +2918,16 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2908
2918
  if (!capturedResourceOptions) return;
2909
2919
  if (capturedResourceOptions.listResources) {
2910
2920
  serverInstance.setRequestHandler(ListResourcesRequestSchema, async (_request, extra) => {
2911
- this.logger.debug("Handling ListResources request");
2912
2921
  if (this.definedResources) {
2913
2922
  return { resources: this.definedResources };
2914
2923
  } else {
2915
2924
  try {
2916
2925
  const resources = await capturedResourceOptions.listResources({ extra });
2917
2926
  this.definedResources = resources;
2918
- this.logger.debug(`Fetched and cached ${this.definedResources.length} resources.`);
2927
+ this.logger.debug("Fetched and cached resources", { count: this.definedResources.length });
2919
2928
  return { resources: this.definedResources };
2920
2929
  } catch (error) {
2921
- this.logger.error("Error fetching resources via listResources():", { error });
2930
+ this.logger.error("Error fetching resources", { error });
2922
2931
  throw error;
2923
2932
  }
2924
2933
  }
@@ -2928,7 +2937,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2928
2937
  serverInstance.setRequestHandler(ReadResourceRequestSchema, async (request, extra) => {
2929
2938
  const startTime = Date.now();
2930
2939
  const uri = request.params.uri;
2931
- this.logger.debug(`Handling ReadResource request for URI: ${uri}`);
2940
+ this.logger.debug("Handling ReadResource request", { uri });
2932
2941
  if (!this.definedResources) {
2933
2942
  const resources = await this.resourceOptions?.listResources?.({ extra });
2934
2943
  if (!resources) throw new Error("Failed to load resources");
@@ -2936,7 +2945,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2936
2945
  }
2937
2946
  const resource = this.definedResources?.find((r) => r.uri === uri);
2938
2947
  if (!resource) {
2939
- this.logger.warn(`ReadResource: Unknown resource URI '${uri}' requested.`);
2948
+ this.logger.warn("Unknown resource URI requested", { uri });
2940
2949
  throw new Error(`Resource not found: ${uri}`);
2941
2950
  }
2942
2951
  try {
@@ -2961,27 +2970,26 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2961
2970
  };
2962
2971
  });
2963
2972
  const duration = Date.now() - startTime;
2964
- this.logger.info(`Resource '${uri}' read successfully in ${duration}ms.`);
2973
+ this.logger.info("Resource read successfully", { uri, duration });
2965
2974
  return {
2966
2975
  contents
2967
2976
  };
2968
2977
  } catch (error) {
2969
2978
  const duration = Date.now() - startTime;
2970
- this.logger.error(`Failed to get content for resource URI '${uri}' in ${duration}ms`, { error });
2979
+ this.logger.error("Failed to get content for resource", { uri, duration, error });
2971
2980
  throw error;
2972
2981
  }
2973
2982
  });
2974
2983
  }
2975
2984
  if (capturedResourceOptions.resourceTemplates) {
2976
2985
  serverInstance.setRequestHandler(ListResourceTemplatesRequestSchema, async (_request, extra) => {
2977
- this.logger.debug("Handling ListResourceTemplates request");
2978
2986
  if (this.definedResourceTemplates) {
2979
2987
  return { resourceTemplates: this.definedResourceTemplates };
2980
2988
  } else {
2981
2989
  try {
2982
2990
  const templates = await capturedResourceOptions.resourceTemplates({ extra });
2983
2991
  this.definedResourceTemplates = templates;
2984
- this.logger.debug(`Fetched and cached ${this.definedResourceTemplates.length} resource templates.`);
2992
+ this.logger.debug("Fetched and cached resource templates", { count: this.definedResourceTemplates.length });
2985
2993
  return { resourceTemplates: this.definedResourceTemplates };
2986
2994
  } catch (error) {
2987
2995
  this.logger.error("Error fetching resource templates via resourceTemplates():", { error });
@@ -2992,13 +3000,13 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
2992
3000
  }
2993
3001
  serverInstance.setRequestHandler(SubscribeRequestSchema, async (request) => {
2994
3002
  const uri = request.params.uri;
2995
- this.logger.info(`Received resources/subscribe request for URI: ${uri}`);
3003
+ this.logger.info("Received resources/subscribe request", { uri });
2996
3004
  this.subscriptions.add(uri);
2997
3005
  return {};
2998
3006
  });
2999
3007
  serverInstance.setRequestHandler(UnsubscribeRequestSchema, async (request) => {
3000
3008
  const uri = request.params.uri;
3001
- this.logger.info(`Received resources/unsubscribe request for URI: ${uri}`);
3009
+ this.logger.info("Received resources/unsubscribe request", { uri });
3002
3010
  this.subscriptions.delete(uri);
3003
3011
  return {};
3004
3012
  });
@@ -3023,7 +3031,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3023
3031
  PromptSchema.parse(prompt);
3024
3032
  }
3025
3033
  this.definedPrompts = prompts;
3026
- this.logger.debug(`Fetched and cached ${this.definedPrompts.length} prompts.`);
3034
+ this.logger.debug("Fetched and cached prompts", { count: this.definedPrompts.length });
3027
3035
  return {
3028
3036
  prompts: this.definedPrompts
3029
3037
  };
@@ -3062,11 +3070,11 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3062
3070
  messages = await capturedPromptOptions.getPromptMessages({ name, version: prompt.version, args, extra });
3063
3071
  }
3064
3072
  const duration = Date.now() - startTime;
3065
- this.logger.info(`Prompt '${name}' retrieved successfully in ${duration}ms.`);
3073
+ this.logger.info("Prompt retrieved successfully", { prompt: name, duration });
3066
3074
  return { description: prompt.description, messages };
3067
3075
  } catch (error) {
3068
3076
  const duration = Date.now() - startTime;
3069
- this.logger.error(`Failed to get content for prompt '${name}' in ${duration}ms`, { error });
3077
+ this.logger.error("Failed to get prompt content", { prompt: name, duration, error });
3070
3078
  throw error;
3071
3079
  }
3072
3080
  }
@@ -3081,7 +3089,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3081
3089
  for (const agentKey in agentsConfig) {
3082
3090
  const agent = agentsConfig[agentKey];
3083
3091
  if (!agent || !("generate" in agent)) {
3084
- this.logger.warn(`Agent instance for '${agentKey}' is invalid or missing a generate function. Skipping.`);
3092
+ this.logger.warn("Invalid agent instance, skipping", { agentKey });
3085
3093
  continue;
3086
3094
  }
3087
3095
  const agentDescription = agent.getDescription();
@@ -3092,9 +3100,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3092
3100
  }
3093
3101
  const agentToolName = `ask_${agentKey}`;
3094
3102
  if (definedConvertedTools?.[agentToolName] || agentTools[agentToolName]) {
3095
- this.logger.warn(
3096
- `Tool with name '${agentToolName}' already exists. Agent '${agentKey}' will not be added as a duplicate tool.`
3097
- );
3103
+ this.logger.warn("Duplicate tool name, skipping agent", { tool: agentToolName, agentKey });
3098
3104
  continue;
3099
3105
  }
3100
3106
  const agentToolDefinition = createTool({
@@ -3110,9 +3116,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3110
3116
  },
3111
3117
  execute: async (inputData, context) => {
3112
3118
  const { message } = inputData;
3113
- this.logger.debug(
3114
- `Executing agent tool '${agentToolName}' for agent '${agent.name}' with message: "${message}"`
3115
- );
3119
+ this.logger.debug("Executing agent tool", { tool: agentToolName, agent: agent.name, message });
3116
3120
  try {
3117
3121
  const proxiedContext = context?.requestContext || new RequestContext();
3118
3122
  if (context?.mcp?.extra) {
@@ -3126,7 +3130,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3126
3130
  });
3127
3131
  return response;
3128
3132
  } catch (error) {
3129
- this.logger.error(`Error executing agent tool '${agentToolName}' for agent '${agent.name}':`, error);
3133
+ this.logger.error("Error executing agent tool", { tool: agentToolName, agent: agent.name, error });
3130
3134
  throw error;
3131
3135
  }
3132
3136
  }
@@ -3147,7 +3151,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3147
3151
  toolType: "agent"
3148
3152
  }
3149
3153
  };
3150
- this.logger.info(`Registered agent '${agent.name}' (key: '${agentKey}') as tool: '${agentToolName}'`);
3154
+ this.logger.info("Registered agent as tool", { agent: agent.name, key: agentKey, tool: agentToolName });
3151
3155
  }
3152
3156
  return agentTools;
3153
3157
  }
@@ -3225,7 +3229,11 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3225
3229
  toolType: "workflow"
3226
3230
  }
3227
3231
  };
3228
- this.logger.info(`Registered workflow '${workflow.id}' (key: '${workflowKey}') as tool: '${workflowToolName}'`);
3232
+ this.logger.info("Registered workflow as tool", {
3233
+ workflow: workflow.id,
3234
+ key: workflowKey,
3235
+ tool: workflowToolName
3236
+ });
3229
3237
  }
3230
3238
  return workflowTools;
3231
3239
  }
@@ -3242,11 +3250,11 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3242
3250
  for (const toolName of Object.keys(tools)) {
3243
3251
  const toolInstance = tools[toolName];
3244
3252
  if (!toolInstance) {
3245
- this.logger.warn(`Tool instance for '${toolName}' is undefined. Skipping.`);
3253
+ this.logger.warn("Tool instance is undefined, skipping", { tool: toolName });
3246
3254
  continue;
3247
3255
  }
3248
3256
  if (typeof toolInstance.execute !== "function") {
3249
- this.logger.warn(`Tool '${toolName}' does not have a valid execute function. Skipping.`);
3257
+ this.logger.warn("Tool has no execute function, skipping", { tool: toolName });
3250
3258
  continue;
3251
3259
  }
3252
3260
  const options = {
@@ -3262,9 +3270,9 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3262
3270
  ...coreTool,
3263
3271
  id: toolName
3264
3272
  };
3265
- this.logger.info(`Registered explicit tool: '${toolName}'`);
3273
+ this.logger.info("Registered explicit tool", { tool: toolName });
3266
3274
  }
3267
- this.logger.info(`Total defined tools registered: ${Object.keys(definedConvertedTools).length}`);
3275
+ this.logger.info("Total defined tools registered", { count: Object.keys(definedConvertedTools).length });
3268
3276
  let agentDerivedTools = {};
3269
3277
  let workflowDerivedTools = {};
3270
3278
  try {
@@ -3558,16 +3566,16 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3558
3566
  res,
3559
3567
  options
3560
3568
  }) {
3561
- this.logger.debug(`startHTTP: Received ${req.method} request to ${url.pathname}`);
3569
+ this.logger.debug("Received HTTP request", { method: req.method, path: url.pathname });
3562
3570
  if (url.pathname !== httpPath) {
3563
- this.logger.debug(`startHTTP: Pathname ${url.pathname} does not match httpPath ${httpPath}. Returning 404.`);
3571
+ this.logger.debug("Pathname does not match httpPath, returning 404", { path: url.pathname, httpPath });
3564
3572
  res.writeHead(404);
3565
3573
  res.end();
3566
3574
  return;
3567
3575
  }
3568
3576
  const isStatelessMode = options?.serverless || options && "sessionIdGenerator" in options && options.sessionIdGenerator === void 0;
3569
3577
  if (isStatelessMode) {
3570
- this.logger.debug("startHTTP: Running in stateless mode (serverless or sessionIdGenerator: undefined)");
3578
+ this.logger.debug("Running in stateless mode");
3571
3579
  await this.handleServerlessRequest(req, res);
3572
3580
  return;
3573
3581
  }
@@ -3579,27 +3587,26 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3579
3587
  };
3580
3588
  const sessionId = req.headers["mcp-session-id"];
3581
3589
  let transport;
3582
- this.logger.debug(
3583
- `startHTTP: Session ID from headers: ${sessionId}. Active transports: ${Array.from(this.streamableHTTPTransports.keys()).join(", ")}`
3584
- );
3590
+ this.logger.debug("Session ID from headers", {
3591
+ sessionId,
3592
+ activeTransports: Array.from(this.streamableHTTPTransports.keys())
3593
+ });
3585
3594
  try {
3586
3595
  if (sessionId && this.streamableHTTPTransports.has(sessionId)) {
3587
3596
  transport = this.streamableHTTPTransports.get(sessionId);
3588
- this.logger.debug(`startHTTP: Using existing Streamable HTTP transport for session ID: ${sessionId}`);
3597
+ this.logger.debug("Using existing transport for session", { sessionId });
3589
3598
  if (req.method === "GET") {
3590
- this.logger.debug(
3591
- `startHTTP: Handling GET request for existing session ${sessionId}. Calling transport.handleRequest.`
3592
- );
3599
+ this.logger.debug("Handling GET request for existing session", { sessionId });
3593
3600
  }
3594
3601
  const body = req.method === "POST" ? await this.readJsonBody(req) : void 0;
3595
3602
  await transport.handleRequest(req, res, body);
3596
3603
  } else {
3597
- this.logger.debug(`startHTTP: No existing Streamable HTTP session ID found. ${req.method}`);
3604
+ this.logger.debug("No existing session found", { method: req.method });
3598
3605
  if (req.method === "POST") {
3599
3606
  const body = await this.readJsonBody(req);
3600
3607
  const { isInitializeRequest } = await import('@modelcontextprotocol/sdk/types.js');
3601
3608
  if (isInitializeRequest(body)) {
3602
- this.logger.debug("startHTTP: Received Streamable HTTP initialize request, creating new transport.");
3609
+ this.logger.debug("Received initialize request, creating new transport");
3603
3610
  transport = new StreamableHTTPServerTransport({
3604
3611
  ...mergedOptions,
3605
3612
  sessionIdGenerator: mergedOptions.sessionIdGenerator,
@@ -3610,13 +3617,11 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3610
3617
  transport.onclose = () => {
3611
3618
  const closedSessionId = transport?.sessionId;
3612
3619
  if (closedSessionId && this.streamableHTTPTransports.has(closedSessionId)) {
3613
- this.logger.debug(
3614
- `startHTTP: Streamable HTTP transport closed for session ${closedSessionId}, removing from map.`
3615
- );
3620
+ this.logger.debug("Transport closed for session, removing from map", { sessionId: closedSessionId });
3616
3621
  this.streamableHTTPTransports.delete(closedSessionId);
3617
3622
  if (this.httpServerInstances.has(closedSessionId)) {
3618
3623
  this.httpServerInstances.delete(closedSessionId);
3619
- this.logger.debug(`startHTTP: Cleaned up server instance for closed session ${closedSessionId}`);
3624
+ this.logger.debug("Cleaned up server instance for closed session", { sessionId: closedSessionId });
3620
3625
  }
3621
3626
  }
3622
3627
  };
@@ -3625,15 +3630,13 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3625
3630
  if (transport.sessionId) {
3626
3631
  this.streamableHTTPTransports.set(transport.sessionId, transport);
3627
3632
  this.httpServerInstances.set(transport.sessionId, sessionServerInstance);
3628
- this.logger.debug(
3629
- `startHTTP: Streamable HTTP session initialized and stored with ID: ${transport.sessionId}`
3630
- );
3633
+ this.logger.debug("Session initialized and stored", { sessionId: transport.sessionId });
3631
3634
  } else {
3632
- this.logger.warn("startHTTP: Streamable HTTP transport initialized without a session ID.");
3635
+ this.logger.warn("Transport initialized without a session ID");
3633
3636
  }
3634
3637
  return await transport.handleRequest(req, res, body);
3635
3638
  } else {
3636
- this.logger.warn("startHTTP: Received non-initialize POST request without a session ID.");
3639
+ this.logger.warn("Received non-initialize POST request without session ID");
3637
3640
  res.writeHead(400, { "Content-Type": "application/json" });
3638
3641
  res.end(
3639
3642
  JSON.stringify({
@@ -3648,7 +3651,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3648
3651
  );
3649
3652
  }
3650
3653
  } else {
3651
- this.logger.warn(`startHTTP: Received ${req.method} request without a session ID.`);
3654
+ this.logger.warn("Received request without session ID", { method: req.method });
3652
3655
  res.writeHead(400, { "Content-Type": "application/json" });
3653
3656
  res.end(
3654
3657
  JSON.stringify({
@@ -3673,7 +3676,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3673
3676
  error
3674
3677
  );
3675
3678
  this.logger.trackException(mastraError);
3676
- this.logger.error("startHTTP: Error handling Streamable HTTP request:", { error: mastraError });
3679
+ this.logger.error("Error handling HTTP request", { error: mastraError });
3677
3680
  if (!res.headersSent) {
3678
3681
  res.writeHead(500, { "Content-Type": "application/json" });
3679
3682
  res.end(
@@ -3705,10 +3708,11 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3705
3708
  */
3706
3709
  async handleServerlessRequest(req, res) {
3707
3710
  try {
3708
- this.logger.debug(`handleServerlessRequest: Received ${req.method} request`);
3711
+ this.logger.debug("Received serverless request", { method: req.method });
3709
3712
  const body = req.method === "POST" ? await this.readJsonBody(req) : void 0;
3710
- this.logger.debug(`handleServerlessRequest: Processing ${req.method} request`, {
3711
- method: body?.method,
3713
+ this.logger.debug("Processing serverless request", {
3714
+ method: req.method,
3715
+ bodyMethod: body?.method,
3712
3716
  id: body?.id
3713
3717
  });
3714
3718
  const transientServer = this.createServerInstance();
@@ -3718,7 +3722,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3718
3722
  });
3719
3723
  await transientServer.connect(tempTransport);
3720
3724
  await tempTransport.handleRequest(req, res, body);
3721
- this.logger.debug(`handleServerlessRequest: Completed ${body?.method} request`, { id: body?.id });
3725
+ this.logger.debug("Completed serverless request", { method: body?.method, id: body?.id });
3722
3726
  } catch (error) {
3723
3727
  const mastraError = new MastraError(
3724
3728
  {
@@ -3730,7 +3734,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3730
3734
  error
3731
3735
  );
3732
3736
  this.logger.trackException(mastraError);
3733
- this.logger.error("handleServerlessRequest: Error handling request:", { error: mastraError });
3737
+ this.logger.error("Error handling serverless request", { error: mastraError });
3734
3738
  if (!res.headersSent) {
3735
3739
  res.writeHead(500, { "Content-Type": "application/json" });
3736
3740
  res.end(
@@ -3997,7 +4001,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3997
4001
  * ```
3998
4002
  */
3999
4003
  getToolListInfo() {
4000
- this.logger.debug(`Getting tool list information for MCPServer '${this.name}'`);
4004
+ this.logger.debug("Getting tool list", { server: this.name });
4001
4005
  return {
4002
4006
  tools: Object.entries(this.convertedTools).map(([toolId, tool]) => ({
4003
4007
  id: toolId,
@@ -4030,10 +4034,10 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
4030
4034
  getToolInfo(toolId) {
4031
4035
  const tool = this.convertedTools[toolId];
4032
4036
  if (!tool) {
4033
- this.logger.debug(`Tool '${toolId}' not found on MCPServer '${this.name}'`);
4037
+ this.logger.debug("Tool not found", { tool: toolId, server: this.name });
4034
4038
  return void 0;
4035
4039
  }
4036
- this.logger.debug(`Getting info for tool '${toolId}' on MCPServer '${this.name}'`);
4040
+ this.logger.debug("Getting tool info", { tool: toolId, server: this.name });
4037
4041
  return {
4038
4042
  name: tool.id || toolId,
4039
4043
  description: tool.description,
@@ -4069,10 +4073,10 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
4069
4073
  let validatedArgs = args;
4070
4074
  try {
4071
4075
  if (!tool) {
4072
- this.logger.warn(`ExecuteTool: Unknown tool '${toolId}' requested on MCPServer '${this.name}'.`);
4076
+ this.logger.warn("Unknown tool requested", { tool: toolId, server: this.name });
4073
4077
  throw new Error(`Unknown tool: ${toolId}`);
4074
4078
  }
4075
- this.logger.debug(`ExecuteTool: Invoking '${toolId}' with arguments:`, args);
4079
+ this.logger.debug("Invoking tool", { tool: toolId, args });
4076
4080
  const paramsSchema = tool.parameters;
4077
4081
  const validation = typeof paramsSchema?.validate === "function" ? paramsSchema.validate(args ?? {}) : typeof paramsSchema?.safeParse === "function" ? paramsSchema.safeParse(args ?? {}) : null;
4078
4082
  if (validation) {
@@ -4083,7 +4087,9 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
4083
4087
  (e) => `- ${e.path?.join(".") || "root"}: ${e.message}`
4084
4088
  ).join("\n");
4085
4089
  const validationErrors = validation.error?.format?.() ?? validation.error ?? validation.issues;
4086
- this.logger.warn(`ExecuteTool: Invalid tool arguments for '${toolId}': ${errorMessages}`, {
4090
+ this.logger.warn("Invalid tool arguments", {
4091
+ tool: toolId,
4092
+ errorMessages,
4087
4093
  errors: validationErrors
4088
4094
  });
4089
4095
  return {
@@ -4097,12 +4103,10 @@ Provided arguments: ${JSON.stringify(args, null, 2)}`,
4097
4103
  }
4098
4104
  validatedArgs = validation.data ?? validation.value ?? args;
4099
4105
  } else {
4100
- this.logger.debug(
4101
- `ExecuteTool: Tool '${toolId}' parameters is not a Zod schema with safeParse or is undefined. Skipping validation.`
4102
- );
4106
+ this.logger.debug("Tool parameters missing schema, skipping validation", { tool: toolId });
4103
4107
  }
4104
4108
  if (!tool.execute) {
4105
- this.logger.error(`ExecuteTool: Tool '${toolId}' does not have an execute function.`);
4109
+ this.logger.error("Tool does not have an execute function", { tool: toolId });
4106
4110
  throw new Error(`Tool '${toolId}' cannot be executed.`);
4107
4111
  }
4108
4112
  } catch (error) {
@@ -4127,7 +4131,7 @@ Provided arguments: ${JSON.stringify(args, null, 2)}`,
4127
4131
  toolCallId: executionContext?.toolCallId || randomUUID()
4128
4132
  };
4129
4133
  const result = await tool.execute(validatedArgs, finalExecutionContext);
4130
- this.logger.info(`ExecuteTool: Tool '${toolId}' executed successfully.`);
4134
+ this.logger.info("Tool executed successfully", { tool: toolId });
4131
4135
  return result;
4132
4136
  } catch (error) {
4133
4137
  const mastraError = new MastraError(
@@ -4143,7 +4147,6 @@ Provided arguments: ${JSON.stringify(args, null, 2)}`,
4143
4147
  error
4144
4148
  );
4145
4149
  this.logger.trackException(mastraError);
4146
- this.logger.error(`ExecuteTool: Tool execution failed for '${toolId}':`, { error });
4147
4150
  throw mastraError;
4148
4151
  }
4149
4152
  }