@ai-sdk/mcp 0.0.8 → 0.0.10

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ai-sdk/mcp
2
2
 
3
+ ## 0.0.10
4
+
5
+ ### Patch Changes
6
+
7
+ - 638de7b: feat(mcp): add the possibility to define client version in mcp client definition
8
+
9
+ ## 0.0.9
10
+
11
+ ### Patch Changes
12
+
13
+ - 89b59d7: feat(mcp): add client elicitation support
14
+
3
15
  ## 0.0.8
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -270,6 +270,12 @@ type RequestOptions = {
270
270
  timeout?: number;
271
271
  maxTotalTimeout?: number;
272
272
  };
273
+ declare const ClientCapabilitiesSchema: z.ZodObject<{
274
+ elicitation: z.ZodOptional<z.ZodObject<{
275
+ applyDefaults: z.ZodOptional<z.ZodBoolean>;
276
+ }, z.core.$loose>>;
277
+ }, z.core.$loose>;
278
+ type ClientCapabilities = z.infer<typeof ClientCapabilitiesSchema>;
273
279
  type PaginatedRequest = Request & {
274
280
  params?: BaseParams & {
275
281
  cursor?: string;
@@ -393,6 +399,21 @@ declare const GetPromptResultSchema: z.ZodObject<{
393
399
  }, z.core.$loose>>;
394
400
  }, z.core.$loose>;
395
401
  type GetPromptResult = z.infer<typeof GetPromptResultSchema>;
402
+ declare const ElicitationRequestSchema: z.ZodObject<{
403
+ method: z.ZodLiteral<"elicitation/create">;
404
+ params: z.ZodObject<{
405
+ _meta: z.ZodOptional<z.ZodObject<{}, z.core.$loose>>;
406
+ message: z.ZodString;
407
+ requestedSchema: z.ZodUnknown;
408
+ }, z.core.$loose>;
409
+ }, z.core.$strip>;
410
+ type ElicitationRequest = z.infer<typeof ElicitationRequestSchema>;
411
+ declare const ElicitResultSchema: z.ZodObject<{
412
+ _meta: z.ZodOptional<z.ZodObject<{}, z.core.$loose>>;
413
+ action: z.ZodUnion<readonly [z.ZodLiteral<"accept">, z.ZodLiteral<"decline">, z.ZodLiteral<"cancel">]>;
414
+ content: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
415
+ }, z.core.$loose>;
416
+ type ElicitResult = z.infer<typeof ElicitResultSchema>;
396
417
 
397
418
  interface MCPClientConfig {
398
419
  /** Transport configuration for connecting to the MCP server */
@@ -401,6 +422,14 @@ interface MCPClientConfig {
401
422
  onUncaughtError?: (error: unknown) => void;
402
423
  /** Optional client name, defaults to 'ai-sdk-mcp-client' */
403
424
  name?: string;
425
+ /** Optional client version, defaults to '1.0.0' */
426
+ version?: string;
427
+ /**
428
+ * Optional client capabilities to advertise during initialization
429
+ *
430
+ * NOTE: It is up to the client application to handle the requests properly. This parameter just helps surface the request from the server
431
+ */
432
+ capabilities?: ClientCapabilities;
404
433
  }
405
434
  declare function createMCPClient(config: MCPClientConfig): Promise<MCPClient>;
406
435
  interface MCPClient {
@@ -427,7 +456,8 @@ interface MCPClient {
427
456
  arguments?: Record<string, unknown>;
428
457
  options?: RequestOptions;
429
458
  }): Promise<GetPromptResult>;
459
+ onElicitationRequest(schema: typeof ElicitationRequestSchema, handler: (request: ElicitationRequest) => Promise<ElicitResult> | ElicitResult): void;
430
460
  close: () => Promise<void>;
431
461
  }
432
462
 
433
- export { type JSONRPCError, type JSONRPCMessage, type JSONRPCNotification, type JSONRPCRequest, type JSONRPCResponse, type MCPTransport, type OAuthClientInformation, type OAuthClientMetadata, type OAuthClientProvider, type OAuthTokens, UnauthorizedError, auth, type MCPClient as experimental_MCPClient, type MCPClientConfig as experimental_MCPClientConfig, createMCPClient as experimental_createMCPClient };
463
+ export { type ElicitResult, ElicitResultSchema, type ElicitationRequest, ElicitationRequestSchema, type JSONRPCError, type JSONRPCMessage, type JSONRPCNotification, type JSONRPCRequest, type JSONRPCResponse, type MCPTransport, type OAuthClientInformation, type OAuthClientMetadata, type OAuthClientProvider, type OAuthTokens, UnauthorizedError, auth, type MCPClient as experimental_MCPClient, type ClientCapabilities as experimental_MCPClientCapabilities, type MCPClientConfig as experimental_MCPClientConfig, createMCPClient as experimental_createMCPClient };
package/dist/index.d.ts CHANGED
@@ -270,6 +270,12 @@ type RequestOptions = {
270
270
  timeout?: number;
271
271
  maxTotalTimeout?: number;
272
272
  };
273
+ declare const ClientCapabilitiesSchema: z.ZodObject<{
274
+ elicitation: z.ZodOptional<z.ZodObject<{
275
+ applyDefaults: z.ZodOptional<z.ZodBoolean>;
276
+ }, z.core.$loose>>;
277
+ }, z.core.$loose>;
278
+ type ClientCapabilities = z.infer<typeof ClientCapabilitiesSchema>;
273
279
  type PaginatedRequest = Request & {
274
280
  params?: BaseParams & {
275
281
  cursor?: string;
@@ -393,6 +399,21 @@ declare const GetPromptResultSchema: z.ZodObject<{
393
399
  }, z.core.$loose>>;
394
400
  }, z.core.$loose>;
395
401
  type GetPromptResult = z.infer<typeof GetPromptResultSchema>;
402
+ declare const ElicitationRequestSchema: z.ZodObject<{
403
+ method: z.ZodLiteral<"elicitation/create">;
404
+ params: z.ZodObject<{
405
+ _meta: z.ZodOptional<z.ZodObject<{}, z.core.$loose>>;
406
+ message: z.ZodString;
407
+ requestedSchema: z.ZodUnknown;
408
+ }, z.core.$loose>;
409
+ }, z.core.$strip>;
410
+ type ElicitationRequest = z.infer<typeof ElicitationRequestSchema>;
411
+ declare const ElicitResultSchema: z.ZodObject<{
412
+ _meta: z.ZodOptional<z.ZodObject<{}, z.core.$loose>>;
413
+ action: z.ZodUnion<readonly [z.ZodLiteral<"accept">, z.ZodLiteral<"decline">, z.ZodLiteral<"cancel">]>;
414
+ content: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
415
+ }, z.core.$loose>;
416
+ type ElicitResult = z.infer<typeof ElicitResultSchema>;
396
417
 
397
418
  interface MCPClientConfig {
398
419
  /** Transport configuration for connecting to the MCP server */
@@ -401,6 +422,14 @@ interface MCPClientConfig {
401
422
  onUncaughtError?: (error: unknown) => void;
402
423
  /** Optional client name, defaults to 'ai-sdk-mcp-client' */
403
424
  name?: string;
425
+ /** Optional client version, defaults to '1.0.0' */
426
+ version?: string;
427
+ /**
428
+ * Optional client capabilities to advertise during initialization
429
+ *
430
+ * NOTE: It is up to the client application to handle the requests properly. This parameter just helps surface the request from the server
431
+ */
432
+ capabilities?: ClientCapabilities;
404
433
  }
405
434
  declare function createMCPClient(config: MCPClientConfig): Promise<MCPClient>;
406
435
  interface MCPClient {
@@ -427,7 +456,8 @@ interface MCPClient {
427
456
  arguments?: Record<string, unknown>;
428
457
  options?: RequestOptions;
429
458
  }): Promise<GetPromptResult>;
459
+ onElicitationRequest(schema: typeof ElicitationRequestSchema, handler: (request: ElicitationRequest) => Promise<ElicitResult> | ElicitResult): void;
430
460
  close: () => Promise<void>;
431
461
  }
432
462
 
433
- export { type JSONRPCError, type JSONRPCMessage, type JSONRPCNotification, type JSONRPCRequest, type JSONRPCResponse, type MCPTransport, type OAuthClientInformation, type OAuthClientMetadata, type OAuthClientProvider, type OAuthTokens, UnauthorizedError, auth, type MCPClient as experimental_MCPClient, type MCPClientConfig as experimental_MCPClientConfig, createMCPClient as experimental_createMCPClient };
463
+ export { type ElicitResult, ElicitResultSchema, type ElicitationRequest, ElicitationRequestSchema, type JSONRPCError, type JSONRPCMessage, type JSONRPCNotification, type JSONRPCRequest, type JSONRPCResponse, type MCPTransport, type OAuthClientInformation, type OAuthClientMetadata, type OAuthClientProvider, type OAuthTokens, UnauthorizedError, auth, type MCPClient as experimental_MCPClient, type ClientCapabilities as experimental_MCPClientCapabilities, type MCPClientConfig as experimental_MCPClientConfig, createMCPClient as experimental_createMCPClient };
package/dist/index.js CHANGED
@@ -30,6 +30,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
+ ElicitResultSchema: () => ElicitResultSchema,
34
+ ElicitationRequestSchema: () => ElicitationRequestSchema,
33
35
  UnauthorizedError: () => UnauthorizedError,
34
36
  auth: () => auth,
35
37
  experimental_createMCPClient: () => createMCPClient
@@ -89,6 +91,9 @@ var RequestSchema = import_v4.z.object({
89
91
  method: import_v4.z.string(),
90
92
  params: import_v4.z.optional(BaseParamsSchema)
91
93
  });
94
+ var ElicitationCapabilitySchema = import_v4.z.object({
95
+ applyDefaults: import_v4.z.optional(import_v4.z.boolean())
96
+ }).loose();
92
97
  var ServerCapabilitiesSchema = import_v4.z.looseObject({
93
98
  experimental: import_v4.z.optional(import_v4.z.object({}).loose()),
94
99
  logging: import_v4.z.optional(import_v4.z.object({}).loose()),
@@ -107,8 +112,12 @@ var ServerCapabilitiesSchema = import_v4.z.looseObject({
107
112
  import_v4.z.looseObject({
108
113
  listChanged: import_v4.z.optional(import_v4.z.boolean())
109
114
  })
110
- )
115
+ ),
116
+ elicitation: import_v4.z.optional(ElicitationCapabilitySchema)
111
117
  });
118
+ var ClientCapabilitiesSchema = import_v4.z.object({
119
+ elicitation: import_v4.z.optional(ElicitationCapabilitySchema)
120
+ }).loose();
112
121
  var InitializeResultSchema = ResultSchema.extend({
113
122
  protocolVersion: import_v4.z.string(),
114
123
  capabilities: ServerCapabilitiesSchema,
@@ -228,6 +237,22 @@ var GetPromptResultSchema = ResultSchema.extend({
228
237
  description: import_v4.z.optional(import_v4.z.string()),
229
238
  messages: import_v4.z.array(PromptMessageSchema)
230
239
  });
240
+ var ElicitationRequestParamsSchema = BaseParamsSchema.extend({
241
+ message: import_v4.z.string(),
242
+ requestedSchema: import_v4.z.unknown()
243
+ });
244
+ var ElicitationRequestSchema = RequestSchema.extend({
245
+ method: import_v4.z.literal("elicitation/create"),
246
+ params: ElicitationRequestParamsSchema
247
+ });
248
+ var ElicitResultSchema = ResultSchema.extend({
249
+ action: import_v4.z.union([
250
+ import_v4.z.literal("accept"),
251
+ import_v4.z.literal("decline"),
252
+ import_v4.z.literal("cancel")
253
+ ]),
254
+ content: import_v4.z.optional(import_v4.z.record(import_v4.z.string(), import_v4.z.unknown()))
255
+ });
231
256
 
232
257
  // src/tool/json-rpc-message.ts
233
258
  var JSONRPC_VERSION = "2.0";
@@ -1546,13 +1571,16 @@ var DefaultMCPClient = class {
1546
1571
  constructor({
1547
1572
  transport: transportConfig,
1548
1573
  name: name3 = "ai-sdk-mcp-client",
1549
- onUncaughtError
1574
+ version = CLIENT_VERSION,
1575
+ onUncaughtError,
1576
+ capabilities
1550
1577
  }) {
1551
1578
  this.requestMessageId = 0;
1552
1579
  this.responseHandlers = /* @__PURE__ */ new Map();
1553
1580
  this.serverCapabilities = {};
1554
1581
  this.isClosed = true;
1555
1582
  this.onUncaughtError = onUncaughtError;
1583
+ this.clientCapabilities = capabilities != null ? capabilities : {};
1556
1584
  if (isCustomMcpTransport(transportConfig)) {
1557
1585
  this.transport = transportConfig;
1558
1586
  } else {
@@ -1562,18 +1590,22 @@ var DefaultMCPClient = class {
1562
1590
  this.transport.onerror = (error) => this.onError(error);
1563
1591
  this.transport.onmessage = (message) => {
1564
1592
  if ("method" in message) {
1565
- this.onError(
1566
- new MCPClientError({
1567
- message: "Unsupported message type"
1568
- })
1569
- );
1593
+ if ("id" in message) {
1594
+ this.onRequestMessage(message);
1595
+ } else {
1596
+ this.onError(
1597
+ new MCPClientError({
1598
+ message: "Unsupported message type"
1599
+ })
1600
+ );
1601
+ }
1570
1602
  return;
1571
1603
  }
1572
1604
  this.onResponse(message);
1573
1605
  };
1574
1606
  this.clientInfo = {
1575
1607
  name: name3,
1576
- version: CLIENT_VERSION
1608
+ version
1577
1609
  };
1578
1610
  }
1579
1611
  async init() {
@@ -1585,7 +1617,7 @@ var DefaultMCPClient = class {
1585
1617
  method: "initialize",
1586
1618
  params: {
1587
1619
  protocolVersion: LATEST_PROTOCOL_VERSION,
1588
- capabilities: {},
1620
+ capabilities: this.clientCapabilities,
1589
1621
  clientInfo: this.clientInfo
1590
1622
  }
1591
1623
  },
@@ -1885,6 +1917,77 @@ var DefaultMCPClient = class {
1885
1917
  }) {
1886
1918
  return this.getPromptInternal({ name: name3, args, options });
1887
1919
  }
1920
+ onElicitationRequest(schema, handler) {
1921
+ if (schema !== ElicitationRequestSchema) {
1922
+ throw new MCPClientError({
1923
+ message: "Unsupported request schema. Only ElicitationRequestSchema is supported."
1924
+ });
1925
+ }
1926
+ this.elicitationRequestHandler = handler;
1927
+ }
1928
+ async onRequestMessage(request) {
1929
+ try {
1930
+ if (request.method !== "elicitation/create") {
1931
+ await this.transport.send({
1932
+ jsonrpc: "2.0",
1933
+ id: request.id,
1934
+ error: {
1935
+ code: -32601,
1936
+ message: `Unsupported request method: ${request.method}`
1937
+ }
1938
+ });
1939
+ return;
1940
+ }
1941
+ if (!this.elicitationRequestHandler) {
1942
+ await this.transport.send({
1943
+ jsonrpc: "2.0",
1944
+ id: request.id,
1945
+ error: {
1946
+ code: -32601,
1947
+ message: "No elicitation handler registered on client"
1948
+ }
1949
+ });
1950
+ return;
1951
+ }
1952
+ const parsedRequest = ElicitationRequestSchema.safeParse({
1953
+ method: request.method,
1954
+ params: request.params
1955
+ });
1956
+ if (!parsedRequest.success) {
1957
+ await this.transport.send({
1958
+ jsonrpc: "2.0",
1959
+ id: request.id,
1960
+ error: {
1961
+ code: -32602,
1962
+ message: `Invalid elicitation request: ${parsedRequest.error.message}`,
1963
+ data: parsedRequest.error.issues
1964
+ }
1965
+ });
1966
+ return;
1967
+ }
1968
+ try {
1969
+ const result = await this.elicitationRequestHandler(parsedRequest.data);
1970
+ const validatedResult = ElicitResultSchema.parse(result);
1971
+ await this.transport.send({
1972
+ jsonrpc: "2.0",
1973
+ id: request.id,
1974
+ result: validatedResult
1975
+ });
1976
+ } catch (error) {
1977
+ await this.transport.send({
1978
+ jsonrpc: "2.0",
1979
+ id: request.id,
1980
+ error: {
1981
+ code: -32603,
1982
+ message: error instanceof Error ? error.message : "Failed to handle elicitation request"
1983
+ }
1984
+ });
1985
+ this.onError(error);
1986
+ }
1987
+ } catch (error) {
1988
+ this.onError(error);
1989
+ }
1990
+ }
1888
1991
  onClose() {
1889
1992
  if (this.isClosed) return;
1890
1993
  this.isClosed = true;
@@ -1924,6 +2027,8 @@ var DefaultMCPClient = class {
1924
2027
  };
1925
2028
  // Annotate the CommonJS export names for ESM import in node:
1926
2029
  0 && (module.exports = {
2030
+ ElicitResultSchema,
2031
+ ElicitationRequestSchema,
1927
2032
  UnauthorizedError,
1928
2033
  auth,
1929
2034
  experimental_createMCPClient