@ai-sdk/mcp 1.0.19 → 1.0.21

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/mcp",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -34,7 +34,7 @@
34
34
  "dependencies": {
35
35
  "pkce-challenge": "^5.0.0",
36
36
  "@ai-sdk/provider": "3.0.8",
37
- "@ai-sdk/provider-utils": "4.0.14"
37
+ "@ai-sdk/provider-utils": "4.0.15"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@types/node": "20.17.24",
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ export { ElicitationRequestSchema, ElicitResultSchema } from './tool/types';
16
16
  export type {
17
17
  ElicitationRequest,
18
18
  ElicitResult,
19
+ ListToolsResult,
19
20
  ClientCapabilities as MCPClientCapabilities,
20
21
  } from './tool/types';
21
22
  export { auth, UnauthorizedError } from './tool/oauth';
@@ -123,6 +123,22 @@ export interface MCPClient {
123
123
  schemas?: TOOL_SCHEMAS;
124
124
  }): Promise<McpToolSet<TOOL_SCHEMAS>>;
125
125
 
126
+ /**
127
+ * Lists available tools from the MCP server.
128
+ */
129
+ listTools(options?: {
130
+ params?: PaginatedRequest['params'];
131
+ options?: RequestOptions;
132
+ }): Promise<ListToolsResult>;
133
+
134
+ /**
135
+ * Creates AI SDK tools from tool definitions.
136
+ */
137
+ toolsFromDefinitions<TOOL_SCHEMAS extends ToolSchemas = 'automatic'>(
138
+ definitions: ListToolsResult,
139
+ options?: { schemas?: TOOL_SCHEMAS },
140
+ ): McpToolSet<TOOL_SCHEMAS>;
141
+
126
142
  listResources(options?: {
127
143
  params?: PaginatedRequest['params'];
128
144
  options?: RequestOptions;
@@ -383,22 +399,18 @@ class DefaultMCPClient implements MCPClient {
383
399
  });
384
400
  }
385
401
 
386
- private async listTools({
402
+ async listTools({
387
403
  params,
388
404
  options,
389
405
  }: {
390
406
  params?: PaginatedRequest['params'];
391
407
  options?: RequestOptions;
392
408
  } = {}): Promise<ListToolsResult> {
393
- try {
394
- return this.request({
395
- request: { method: 'tools/list', params },
396
- resultSchema: ListToolsResultSchema,
397
- options,
398
- });
399
- } catch (error) {
400
- throw error;
401
- }
409
+ return this.request({
410
+ request: { method: 'tools/list', params },
411
+ resultSchema: ListToolsResultSchema,
412
+ options,
413
+ });
402
414
  }
403
415
 
404
416
  private async callTool({
@@ -522,7 +534,8 @@ class DefaultMCPClient implements MCPClient {
522
534
  }
523
535
 
524
536
  /**
525
- * Returns a set of AI SDK tools from the MCP server
537
+ * Returns a set of AI SDK tools from the MCP server.
538
+ * This fetches tool definitions and wraps them with execute functions.
526
539
  * @returns A record of tool names to their implementations
527
540
  */
528
541
  async tools<TOOL_SCHEMAS extends ToolSchemas = 'automatic'>({
@@ -530,70 +543,80 @@ class DefaultMCPClient implements MCPClient {
530
543
  }: {
531
544
  schemas?: TOOL_SCHEMAS;
532
545
  } = {}): Promise<McpToolSet<TOOL_SCHEMAS>> {
533
- const tools: Record<string, Tool & { _meta?: ToolMeta }> = {};
546
+ const definitions = await this.listTools();
547
+ return this.toolsFromDefinitions(definitions, {
548
+ schemas,
549
+ } as { schemas?: TOOL_SCHEMAS });
550
+ }
534
551
 
535
- try {
536
- const listToolsResult = await this.listTools();
537
- for (const {
538
- name,
539
- title,
540
- description,
541
- inputSchema,
542
- annotations,
543
- _meta,
544
- } of listToolsResult.tools) {
545
- const resolvedTitle = title ?? annotations?.title;
546
- if (schemas !== 'automatic' && !(name in schemas)) {
547
- continue;
548
- }
552
+ /**
553
+ * Creates AI SDK tools from tool definitions without fetching from the server.
554
+ */
555
+ toolsFromDefinitions<TOOL_SCHEMAS extends ToolSchemas = 'automatic'>(
556
+ definitions: ListToolsResult,
557
+ { schemas = 'automatic' }: { schemas?: TOOL_SCHEMAS } = {} as {
558
+ schemas?: TOOL_SCHEMAS;
559
+ },
560
+ ): McpToolSet<TOOL_SCHEMAS> {
561
+ const tools: Record<string, Tool & { _meta?: ToolMeta }> = {};
549
562
 
550
- const self = this;
551
- const outputSchema =
552
- schemas !== 'automatic' ? schemas[name]?.outputSchema : undefined;
563
+ for (const {
564
+ name,
565
+ title,
566
+ description,
567
+ inputSchema,
568
+ annotations,
569
+ _meta,
570
+ } of definitions.tools) {
571
+ const resolvedTitle = title ?? annotations?.title;
572
+ if (schemas !== 'automatic' && !(name in schemas)) {
573
+ continue;
574
+ }
553
575
 
554
- const execute = async (
555
- args: any,
556
- options: ToolExecutionOptions,
557
- ): Promise<unknown> => {
558
- options?.abortSignal?.throwIfAborted();
559
- const result = await self.callTool({ name, args, options });
576
+ const self = this;
577
+ const outputSchema =
578
+ schemas !== 'automatic' ? schemas[name]?.outputSchema : undefined;
560
579
 
561
- if (outputSchema != null) {
562
- return self.extractStructuredContent(result, outputSchema, name);
563
- }
580
+ const execute = async (
581
+ args: any,
582
+ options: ToolExecutionOptions,
583
+ ): Promise<unknown> => {
584
+ options?.abortSignal?.throwIfAborted();
585
+ const result = await self.callTool({ name, args, options });
564
586
 
565
- return result;
566
- };
587
+ if (outputSchema != null) {
588
+ return self.extractStructuredContent(result, outputSchema, name);
589
+ }
567
590
 
568
- const toolWithExecute =
569
- schemas === 'automatic'
570
- ? dynamicTool({
571
- description,
572
- title: resolvedTitle,
573
- inputSchema: jsonSchema({
574
- ...inputSchema,
575
- properties: inputSchema.properties ?? {},
576
- additionalProperties: false,
577
- } as JSONSchema7),
578
- execute,
579
- toModelOutput: mcpToModelOutput,
580
- })
581
- : tool({
582
- description,
583
- title: resolvedTitle,
584
- inputSchema: schemas[name].inputSchema,
585
- ...(outputSchema != null ? { outputSchema } : {}),
586
- execute,
587
- toModelOutput: mcpToModelOutput,
588
- });
589
-
590
- tools[name] = { ...toolWithExecute, _meta };
591
- }
591
+ return result;
592
+ };
592
593
 
593
- return tools as McpToolSet<TOOL_SCHEMAS>;
594
- } catch (error) {
595
- throw error;
594
+ const toolWithExecute =
595
+ schemas === 'automatic'
596
+ ? dynamicTool({
597
+ description,
598
+ title: resolvedTitle,
599
+ inputSchema: jsonSchema({
600
+ ...inputSchema,
601
+ properties: inputSchema.properties ?? {},
602
+ additionalProperties: false,
603
+ } as JSONSchema7),
604
+ execute,
605
+ toModelOutput: mcpToModelOutput,
606
+ })
607
+ : tool({
608
+ description,
609
+ title: resolvedTitle,
610
+ inputSchema: schemas[name].inputSchema,
611
+ ...(outputSchema != null ? { outputSchema } : {}),
612
+ execute,
613
+ toModelOutput: mcpToModelOutput,
614
+ });
615
+
616
+ tools[name] = { ...toolWithExecute, _meta };
596
617
  }
618
+
619
+ return tools as McpToolSet<TOOL_SCHEMAS>;
597
620
  }
598
621
 
599
622
  /**