@ai-sdk/mcp 1.0.0-beta.4 → 1.0.0-beta.41

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
@@ -47,6 +47,7 @@ var SUPPORTED_PROTOCOL_VERSIONS = [
47
47
  "2025-03-26",
48
48
  "2024-11-05"
49
49
  ];
50
+ var ToolMetaSchema = z.optional(z.record(z.string(), z.unknown()));
50
51
  var ClientOrServerImplementationSchema = z.looseObject({
51
52
  name: z.string(),
52
53
  version: z.string()
@@ -59,6 +60,9 @@ var RequestSchema = z.object({
59
60
  method: z.string(),
60
61
  params: z.optional(BaseParamsSchema)
61
62
  });
63
+ var ElicitationCapabilitySchema = z.object({
64
+ applyDefaults: z.optional(z.boolean())
65
+ }).loose();
62
66
  var ServerCapabilitiesSchema = z.looseObject({
63
67
  experimental: z.optional(z.object({}).loose()),
64
68
  logging: z.optional(z.object({}).loose()),
@@ -77,8 +81,12 @@ var ServerCapabilitiesSchema = z.looseObject({
77
81
  z.looseObject({
78
82
  listChanged: z.optional(z.boolean())
79
83
  })
80
- )
84
+ ),
85
+ elicitation: z.optional(ElicitationCapabilitySchema)
81
86
  });
87
+ var ClientCapabilitiesSchema = z.object({
88
+ elicitation: z.optional(ElicitationCapabilitySchema)
89
+ }).loose();
82
90
  var InitializeResultSchema = ResultSchema.extend({
83
91
  protocolVersion: z.string(),
84
92
  capabilities: ServerCapabilitiesSchema,
@@ -99,7 +107,8 @@ var ToolSchema = z.object({
99
107
  z.object({
100
108
  title: z.optional(z.string())
101
109
  }).loose()
102
- )
110
+ ),
111
+ _meta: ToolMetaSchema
103
112
  }).loose();
104
113
  var ListToolsResultSchema = PaginatedResultSchema.extend({
105
114
  tools: z.array(ToolSchema)
@@ -177,6 +186,48 @@ var ReadResourceResultSchema = ResultSchema.extend({
177
186
  z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
178
187
  )
179
188
  });
189
+ var PromptArgumentSchema = z.object({
190
+ name: z.string(),
191
+ description: z.optional(z.string()),
192
+ required: z.optional(z.boolean())
193
+ }).loose();
194
+ var PromptSchema = z.object({
195
+ name: z.string(),
196
+ title: z.optional(z.string()),
197
+ description: z.optional(z.string()),
198
+ arguments: z.optional(z.array(PromptArgumentSchema))
199
+ }).loose();
200
+ var ListPromptsResultSchema = PaginatedResultSchema.extend({
201
+ prompts: z.array(PromptSchema)
202
+ });
203
+ var PromptMessageSchema = z.object({
204
+ role: z.union([z.literal("user"), z.literal("assistant")]),
205
+ content: z.union([
206
+ TextContentSchema,
207
+ ImageContentSchema,
208
+ EmbeddedResourceSchema
209
+ ])
210
+ }).loose();
211
+ var GetPromptResultSchema = ResultSchema.extend({
212
+ description: z.optional(z.string()),
213
+ messages: z.array(PromptMessageSchema)
214
+ });
215
+ var ElicitationRequestParamsSchema = BaseParamsSchema.extend({
216
+ message: z.string(),
217
+ requestedSchema: z.unknown()
218
+ });
219
+ var ElicitationRequestSchema = RequestSchema.extend({
220
+ method: z.literal("elicitation/create"),
221
+ params: ElicitationRequestParamsSchema
222
+ });
223
+ var ElicitResultSchema = ResultSchema.extend({
224
+ action: z.union([
225
+ z.literal("accept"),
226
+ z.literal("decline"),
227
+ z.literal("cancel")
228
+ ]),
229
+ content: z.optional(z.record(z.string(), z.unknown()))
230
+ });
180
231
 
181
232
  // src/tool/json-rpc-message.ts
182
233
  var JSONRPC_VERSION = "2.0";
@@ -752,7 +803,8 @@ async function refreshAuthorization(authorizationServerUrl, {
752
803
  tokenUrl = new URL("/token", authorizationServerUrl);
753
804
  }
754
805
  const headers = new Headers({
755
- "Content-Type": "application/x-www-form-urlencoded"
806
+ "Content-Type": "application/x-www-form-urlencoded",
807
+ Accept: "application/json"
756
808
  });
757
809
  const params = new URLSearchParams({
758
810
  grant_type: grantType,
@@ -1499,13 +1551,16 @@ var DefaultMCPClient = class {
1499
1551
  constructor({
1500
1552
  transport: transportConfig,
1501
1553
  name: name3 = "ai-sdk-mcp-client",
1502
- onUncaughtError
1554
+ version = CLIENT_VERSION,
1555
+ onUncaughtError,
1556
+ capabilities
1503
1557
  }) {
1504
1558
  this.requestMessageId = 0;
1505
1559
  this.responseHandlers = /* @__PURE__ */ new Map();
1506
1560
  this.serverCapabilities = {};
1507
1561
  this.isClosed = true;
1508
1562
  this.onUncaughtError = onUncaughtError;
1563
+ this.clientCapabilities = capabilities != null ? capabilities : {};
1509
1564
  if (isCustomMcpTransport(transportConfig)) {
1510
1565
  this.transport = transportConfig;
1511
1566
  } else {
@@ -1515,18 +1570,22 @@ var DefaultMCPClient = class {
1515
1570
  this.transport.onerror = (error) => this.onError(error);
1516
1571
  this.transport.onmessage = (message) => {
1517
1572
  if ("method" in message) {
1518
- this.onError(
1519
- new MCPClientError({
1520
- message: "Unsupported message type"
1521
- })
1522
- );
1573
+ if ("id" in message) {
1574
+ this.onRequestMessage(message);
1575
+ } else {
1576
+ this.onError(
1577
+ new MCPClientError({
1578
+ message: "Unsupported message type"
1579
+ })
1580
+ );
1581
+ }
1523
1582
  return;
1524
1583
  }
1525
1584
  this.onResponse(message);
1526
1585
  };
1527
1586
  this.clientInfo = {
1528
1587
  name: name3,
1529
- version: CLIENT_VERSION
1588
+ version
1530
1589
  };
1531
1590
  }
1532
1591
  async init() {
@@ -1538,7 +1597,7 @@ var DefaultMCPClient = class {
1538
1597
  method: "initialize",
1539
1598
  params: {
1540
1599
  protocolVersion: LATEST_PROTOCOL_VERSION,
1541
- capabilities: {},
1600
+ capabilities: this.clientCapabilities,
1542
1601
  clientInfo: this.clientInfo
1543
1602
  }
1544
1603
  },
@@ -1591,6 +1650,14 @@ var DefaultMCPClient = class {
1591
1650
  });
1592
1651
  }
1593
1652
  break;
1653
+ case "prompts/list":
1654
+ case "prompts/get":
1655
+ if (!this.serverCapabilities.prompts) {
1656
+ throw new MCPClientError({
1657
+ message: `Server does not support prompts`
1658
+ });
1659
+ }
1660
+ break;
1594
1661
  default:
1595
1662
  throw new MCPClientError({
1596
1663
  message: `Unsupported method: ${method}`
@@ -1723,6 +1790,35 @@ var DefaultMCPClient = class {
1723
1790
  throw error;
1724
1791
  }
1725
1792
  }
1793
+ async listPromptsInternal({
1794
+ params,
1795
+ options
1796
+ } = {}) {
1797
+ try {
1798
+ return this.request({
1799
+ request: { method: "prompts/list", params },
1800
+ resultSchema: ListPromptsResultSchema,
1801
+ options
1802
+ });
1803
+ } catch (error) {
1804
+ throw error;
1805
+ }
1806
+ }
1807
+ async getPromptInternal({
1808
+ name: name3,
1809
+ args,
1810
+ options
1811
+ }) {
1812
+ try {
1813
+ return this.request({
1814
+ request: { method: "prompts/get", params: { name: name3, arguments: args } },
1815
+ resultSchema: GetPromptResultSchema,
1816
+ options
1817
+ });
1818
+ } catch (error) {
1819
+ throw error;
1820
+ }
1821
+ }
1726
1822
  async notification(notification) {
1727
1823
  const jsonrpcNotification = {
1728
1824
  ...notification,
@@ -1745,7 +1841,8 @@ var DefaultMCPClient = class {
1745
1841
  name: name3,
1746
1842
  description,
1747
1843
  inputSchema,
1748
- annotations
1844
+ annotations,
1845
+ _meta
1749
1846
  } of listToolsResult.tools) {
1750
1847
  const title = annotations == null ? void 0 : annotations.title;
1751
1848
  if (schemas !== "automatic" && !(name3 in schemas)) {
@@ -1772,7 +1869,7 @@ var DefaultMCPClient = class {
1772
1869
  inputSchema: schemas[name3].inputSchema,
1773
1870
  execute
1774
1871
  });
1775
- tools[name3] = toolWithExecute;
1872
+ tools[name3] = { ...toolWithExecute, _meta };
1776
1873
  }
1777
1874
  return tools;
1778
1875
  } catch (error) {
@@ -1796,6 +1893,90 @@ var DefaultMCPClient = class {
1796
1893
  } = {}) {
1797
1894
  return this.listResourceTemplatesInternal({ options });
1798
1895
  }
1896
+ experimental_listPrompts({
1897
+ params,
1898
+ options
1899
+ } = {}) {
1900
+ return this.listPromptsInternal({ params, options });
1901
+ }
1902
+ experimental_getPrompt({
1903
+ name: name3,
1904
+ arguments: args,
1905
+ options
1906
+ }) {
1907
+ return this.getPromptInternal({ name: name3, args, options });
1908
+ }
1909
+ onElicitationRequest(schema, handler) {
1910
+ if (schema !== ElicitationRequestSchema) {
1911
+ throw new MCPClientError({
1912
+ message: "Unsupported request schema. Only ElicitationRequestSchema is supported."
1913
+ });
1914
+ }
1915
+ this.elicitationRequestHandler = handler;
1916
+ }
1917
+ async onRequestMessage(request) {
1918
+ try {
1919
+ if (request.method !== "elicitation/create") {
1920
+ await this.transport.send({
1921
+ jsonrpc: "2.0",
1922
+ id: request.id,
1923
+ error: {
1924
+ code: -32601,
1925
+ message: `Unsupported request method: ${request.method}`
1926
+ }
1927
+ });
1928
+ return;
1929
+ }
1930
+ if (!this.elicitationRequestHandler) {
1931
+ await this.transport.send({
1932
+ jsonrpc: "2.0",
1933
+ id: request.id,
1934
+ error: {
1935
+ code: -32601,
1936
+ message: "No elicitation handler registered on client"
1937
+ }
1938
+ });
1939
+ return;
1940
+ }
1941
+ const parsedRequest = ElicitationRequestSchema.safeParse({
1942
+ method: request.method,
1943
+ params: request.params
1944
+ });
1945
+ if (!parsedRequest.success) {
1946
+ await this.transport.send({
1947
+ jsonrpc: "2.0",
1948
+ id: request.id,
1949
+ error: {
1950
+ code: -32602,
1951
+ message: `Invalid elicitation request: ${parsedRequest.error.message}`,
1952
+ data: parsedRequest.error.issues
1953
+ }
1954
+ });
1955
+ return;
1956
+ }
1957
+ try {
1958
+ const result = await this.elicitationRequestHandler(parsedRequest.data);
1959
+ const validatedResult = ElicitResultSchema.parse(result);
1960
+ await this.transport.send({
1961
+ jsonrpc: "2.0",
1962
+ id: request.id,
1963
+ result: validatedResult
1964
+ });
1965
+ } catch (error) {
1966
+ await this.transport.send({
1967
+ jsonrpc: "2.0",
1968
+ id: request.id,
1969
+ error: {
1970
+ code: -32603,
1971
+ message: error instanceof Error ? error.message : "Failed to handle elicitation request"
1972
+ }
1973
+ });
1974
+ this.onError(error);
1975
+ }
1976
+ } catch (error) {
1977
+ this.onError(error);
1978
+ }
1979
+ }
1799
1980
  onClose() {
1800
1981
  if (this.isClosed) return;
1801
1982
  this.isClosed = true;
@@ -1834,8 +2015,11 @@ var DefaultMCPClient = class {
1834
2015
  }
1835
2016
  };
1836
2017
  export {
2018
+ ElicitResultSchema,
2019
+ ElicitationRequestSchema,
1837
2020
  UnauthorizedError,
1838
2021
  auth,
2022
+ createMCPClient,
1839
2023
  createMCPClient as experimental_createMCPClient
1840
2024
  };
1841
2025
  //# sourceMappingURL=index.mjs.map