@ai-sdk/mcp 0.0.8 → 0.0.9

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.mjs CHANGED
@@ -59,6 +59,9 @@ var RequestSchema = z.object({
59
59
  method: z.string(),
60
60
  params: z.optional(BaseParamsSchema)
61
61
  });
62
+ var ElicitationCapabilitySchema = z.object({
63
+ applyDefaults: z.optional(z.boolean())
64
+ }).loose();
62
65
  var ServerCapabilitiesSchema = z.looseObject({
63
66
  experimental: z.optional(z.object({}).loose()),
64
67
  logging: z.optional(z.object({}).loose()),
@@ -77,8 +80,12 @@ var ServerCapabilitiesSchema = z.looseObject({
77
80
  z.looseObject({
78
81
  listChanged: z.optional(z.boolean())
79
82
  })
80
- )
83
+ ),
84
+ elicitation: z.optional(ElicitationCapabilitySchema)
81
85
  });
86
+ var ClientCapabilitiesSchema = z.object({
87
+ elicitation: z.optional(ElicitationCapabilitySchema)
88
+ }).loose();
82
89
  var InitializeResultSchema = ResultSchema.extend({
83
90
  protocolVersion: z.string(),
84
91
  capabilities: ServerCapabilitiesSchema,
@@ -198,6 +205,22 @@ var GetPromptResultSchema = ResultSchema.extend({
198
205
  description: z.optional(z.string()),
199
206
  messages: z.array(PromptMessageSchema)
200
207
  });
208
+ var ElicitationRequestParamsSchema = BaseParamsSchema.extend({
209
+ message: z.string(),
210
+ requestedSchema: z.unknown()
211
+ });
212
+ var ElicitationRequestSchema = RequestSchema.extend({
213
+ method: z.literal("elicitation/create"),
214
+ params: ElicitationRequestParamsSchema
215
+ });
216
+ var ElicitResultSchema = ResultSchema.extend({
217
+ action: z.union([
218
+ z.literal("accept"),
219
+ z.literal("decline"),
220
+ z.literal("cancel")
221
+ ]),
222
+ content: z.optional(z.record(z.string(), z.unknown()))
223
+ });
201
224
 
202
225
  // src/tool/json-rpc-message.ts
203
226
  var JSONRPC_VERSION = "2.0";
@@ -1520,13 +1543,15 @@ var DefaultMCPClient = class {
1520
1543
  constructor({
1521
1544
  transport: transportConfig,
1522
1545
  name: name3 = "ai-sdk-mcp-client",
1523
- onUncaughtError
1546
+ onUncaughtError,
1547
+ capabilities
1524
1548
  }) {
1525
1549
  this.requestMessageId = 0;
1526
1550
  this.responseHandlers = /* @__PURE__ */ new Map();
1527
1551
  this.serverCapabilities = {};
1528
1552
  this.isClosed = true;
1529
1553
  this.onUncaughtError = onUncaughtError;
1554
+ this.clientCapabilities = capabilities != null ? capabilities : {};
1530
1555
  if (isCustomMcpTransport(transportConfig)) {
1531
1556
  this.transport = transportConfig;
1532
1557
  } else {
@@ -1536,11 +1561,15 @@ var DefaultMCPClient = class {
1536
1561
  this.transport.onerror = (error) => this.onError(error);
1537
1562
  this.transport.onmessage = (message) => {
1538
1563
  if ("method" in message) {
1539
- this.onError(
1540
- new MCPClientError({
1541
- message: "Unsupported message type"
1542
- })
1543
- );
1564
+ if ("id" in message) {
1565
+ this.onRequestMessage(message);
1566
+ } else {
1567
+ this.onError(
1568
+ new MCPClientError({
1569
+ message: "Unsupported message type"
1570
+ })
1571
+ );
1572
+ }
1544
1573
  return;
1545
1574
  }
1546
1575
  this.onResponse(message);
@@ -1559,7 +1588,7 @@ var DefaultMCPClient = class {
1559
1588
  method: "initialize",
1560
1589
  params: {
1561
1590
  protocolVersion: LATEST_PROTOCOL_VERSION,
1562
- capabilities: {},
1591
+ capabilities: this.clientCapabilities,
1563
1592
  clientInfo: this.clientInfo
1564
1593
  }
1565
1594
  },
@@ -1859,6 +1888,77 @@ var DefaultMCPClient = class {
1859
1888
  }) {
1860
1889
  return this.getPromptInternal({ name: name3, args, options });
1861
1890
  }
1891
+ onElicitationRequest(schema, handler) {
1892
+ if (schema !== ElicitationRequestSchema) {
1893
+ throw new MCPClientError({
1894
+ message: "Unsupported request schema. Only ElicitationRequestSchema is supported."
1895
+ });
1896
+ }
1897
+ this.elicitationRequestHandler = handler;
1898
+ }
1899
+ async onRequestMessage(request) {
1900
+ try {
1901
+ if (request.method !== "elicitation/create") {
1902
+ await this.transport.send({
1903
+ jsonrpc: "2.0",
1904
+ id: request.id,
1905
+ error: {
1906
+ code: -32601,
1907
+ message: `Unsupported request method: ${request.method}`
1908
+ }
1909
+ });
1910
+ return;
1911
+ }
1912
+ if (!this.elicitationRequestHandler) {
1913
+ await this.transport.send({
1914
+ jsonrpc: "2.0",
1915
+ id: request.id,
1916
+ error: {
1917
+ code: -32601,
1918
+ message: "No elicitation handler registered on client"
1919
+ }
1920
+ });
1921
+ return;
1922
+ }
1923
+ const parsedRequest = ElicitationRequestSchema.safeParse({
1924
+ method: request.method,
1925
+ params: request.params
1926
+ });
1927
+ if (!parsedRequest.success) {
1928
+ await this.transport.send({
1929
+ jsonrpc: "2.0",
1930
+ id: request.id,
1931
+ error: {
1932
+ code: -32602,
1933
+ message: `Invalid elicitation request: ${parsedRequest.error.message}`,
1934
+ data: parsedRequest.error.issues
1935
+ }
1936
+ });
1937
+ return;
1938
+ }
1939
+ try {
1940
+ const result = await this.elicitationRequestHandler(parsedRequest.data);
1941
+ const validatedResult = ElicitResultSchema.parse(result);
1942
+ await this.transport.send({
1943
+ jsonrpc: "2.0",
1944
+ id: request.id,
1945
+ result: validatedResult
1946
+ });
1947
+ } catch (error) {
1948
+ await this.transport.send({
1949
+ jsonrpc: "2.0",
1950
+ id: request.id,
1951
+ error: {
1952
+ code: -32603,
1953
+ message: error instanceof Error ? error.message : "Failed to handle elicitation request"
1954
+ }
1955
+ });
1956
+ this.onError(error);
1957
+ }
1958
+ } catch (error) {
1959
+ this.onError(error);
1960
+ }
1961
+ }
1862
1962
  onClose() {
1863
1963
  if (this.isClosed) return;
1864
1964
  this.isClosed = true;
@@ -1897,6 +1997,8 @@ var DefaultMCPClient = class {
1897
1997
  }
1898
1998
  };
1899
1999
  export {
2000
+ ElicitResultSchema,
2001
+ ElicitationRequestSchema,
1900
2002
  UnauthorizedError,
1901
2003
  auth,
1902
2004
  createMCPClient as experimental_createMCPClient