@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/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,16 @@ var DefaultMCPClient = class {
1520
1543
  constructor({
1521
1544
  transport: transportConfig,
1522
1545
  name: name3 = "ai-sdk-mcp-client",
1523
- onUncaughtError
1546
+ version = CLIENT_VERSION,
1547
+ onUncaughtError,
1548
+ capabilities
1524
1549
  }) {
1525
1550
  this.requestMessageId = 0;
1526
1551
  this.responseHandlers = /* @__PURE__ */ new Map();
1527
1552
  this.serverCapabilities = {};
1528
1553
  this.isClosed = true;
1529
1554
  this.onUncaughtError = onUncaughtError;
1555
+ this.clientCapabilities = capabilities != null ? capabilities : {};
1530
1556
  if (isCustomMcpTransport(transportConfig)) {
1531
1557
  this.transport = transportConfig;
1532
1558
  } else {
@@ -1536,18 +1562,22 @@ var DefaultMCPClient = class {
1536
1562
  this.transport.onerror = (error) => this.onError(error);
1537
1563
  this.transport.onmessage = (message) => {
1538
1564
  if ("method" in message) {
1539
- this.onError(
1540
- new MCPClientError({
1541
- message: "Unsupported message type"
1542
- })
1543
- );
1565
+ if ("id" in message) {
1566
+ this.onRequestMessage(message);
1567
+ } else {
1568
+ this.onError(
1569
+ new MCPClientError({
1570
+ message: "Unsupported message type"
1571
+ })
1572
+ );
1573
+ }
1544
1574
  return;
1545
1575
  }
1546
1576
  this.onResponse(message);
1547
1577
  };
1548
1578
  this.clientInfo = {
1549
1579
  name: name3,
1550
- version: CLIENT_VERSION
1580
+ version
1551
1581
  };
1552
1582
  }
1553
1583
  async init() {
@@ -1559,7 +1589,7 @@ var DefaultMCPClient = class {
1559
1589
  method: "initialize",
1560
1590
  params: {
1561
1591
  protocolVersion: LATEST_PROTOCOL_VERSION,
1562
- capabilities: {},
1592
+ capabilities: this.clientCapabilities,
1563
1593
  clientInfo: this.clientInfo
1564
1594
  }
1565
1595
  },
@@ -1859,6 +1889,77 @@ var DefaultMCPClient = class {
1859
1889
  }) {
1860
1890
  return this.getPromptInternal({ name: name3, args, options });
1861
1891
  }
1892
+ onElicitationRequest(schema, handler) {
1893
+ if (schema !== ElicitationRequestSchema) {
1894
+ throw new MCPClientError({
1895
+ message: "Unsupported request schema. Only ElicitationRequestSchema is supported."
1896
+ });
1897
+ }
1898
+ this.elicitationRequestHandler = handler;
1899
+ }
1900
+ async onRequestMessage(request) {
1901
+ try {
1902
+ if (request.method !== "elicitation/create") {
1903
+ await this.transport.send({
1904
+ jsonrpc: "2.0",
1905
+ id: request.id,
1906
+ error: {
1907
+ code: -32601,
1908
+ message: `Unsupported request method: ${request.method}`
1909
+ }
1910
+ });
1911
+ return;
1912
+ }
1913
+ if (!this.elicitationRequestHandler) {
1914
+ await this.transport.send({
1915
+ jsonrpc: "2.0",
1916
+ id: request.id,
1917
+ error: {
1918
+ code: -32601,
1919
+ message: "No elicitation handler registered on client"
1920
+ }
1921
+ });
1922
+ return;
1923
+ }
1924
+ const parsedRequest = ElicitationRequestSchema.safeParse({
1925
+ method: request.method,
1926
+ params: request.params
1927
+ });
1928
+ if (!parsedRequest.success) {
1929
+ await this.transport.send({
1930
+ jsonrpc: "2.0",
1931
+ id: request.id,
1932
+ error: {
1933
+ code: -32602,
1934
+ message: `Invalid elicitation request: ${parsedRequest.error.message}`,
1935
+ data: parsedRequest.error.issues
1936
+ }
1937
+ });
1938
+ return;
1939
+ }
1940
+ try {
1941
+ const result = await this.elicitationRequestHandler(parsedRequest.data);
1942
+ const validatedResult = ElicitResultSchema.parse(result);
1943
+ await this.transport.send({
1944
+ jsonrpc: "2.0",
1945
+ id: request.id,
1946
+ result: validatedResult
1947
+ });
1948
+ } catch (error) {
1949
+ await this.transport.send({
1950
+ jsonrpc: "2.0",
1951
+ id: request.id,
1952
+ error: {
1953
+ code: -32603,
1954
+ message: error instanceof Error ? error.message : "Failed to handle elicitation request"
1955
+ }
1956
+ });
1957
+ this.onError(error);
1958
+ }
1959
+ } catch (error) {
1960
+ this.onError(error);
1961
+ }
1962
+ }
1862
1963
  onClose() {
1863
1964
  if (this.isClosed) return;
1864
1965
  this.isClosed = true;
@@ -1897,6 +1998,8 @@ var DefaultMCPClient = class {
1897
1998
  }
1898
1999
  };
1899
2000
  export {
2001
+ ElicitResultSchema,
2002
+ ElicitationRequestSchema,
1900
2003
  UnauthorizedError,
1901
2004
  auth,
1902
2005
  createMCPClient as experimental_createMCPClient