@ai-sdk/mcp 1.0.0-beta.3 → 1.0.0-beta.30

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.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
@@ -77,6 +79,7 @@ var SUPPORTED_PROTOCOL_VERSIONS = [
77
79
  "2025-03-26",
78
80
  "2024-11-05"
79
81
  ];
82
+ var ToolMetaSchema = import_v4.z.optional(import_v4.z.record(import_v4.z.string(), import_v4.z.unknown()));
80
83
  var ClientOrServerImplementationSchema = import_v4.z.looseObject({
81
84
  name: import_v4.z.string(),
82
85
  version: import_v4.z.string()
@@ -89,6 +92,9 @@ var RequestSchema = import_v4.z.object({
89
92
  method: import_v4.z.string(),
90
93
  params: import_v4.z.optional(BaseParamsSchema)
91
94
  });
95
+ var ElicitationCapabilitySchema = import_v4.z.object({
96
+ applyDefaults: import_v4.z.optional(import_v4.z.boolean())
97
+ }).loose();
92
98
  var ServerCapabilitiesSchema = import_v4.z.looseObject({
93
99
  experimental: import_v4.z.optional(import_v4.z.object({}).loose()),
94
100
  logging: import_v4.z.optional(import_v4.z.object({}).loose()),
@@ -107,8 +113,12 @@ var ServerCapabilitiesSchema = import_v4.z.looseObject({
107
113
  import_v4.z.looseObject({
108
114
  listChanged: import_v4.z.optional(import_v4.z.boolean())
109
115
  })
110
- )
116
+ ),
117
+ elicitation: import_v4.z.optional(ElicitationCapabilitySchema)
111
118
  });
119
+ var ClientCapabilitiesSchema = import_v4.z.object({
120
+ elicitation: import_v4.z.optional(ElicitationCapabilitySchema)
121
+ }).loose();
112
122
  var InitializeResultSchema = ResultSchema.extend({
113
123
  protocolVersion: import_v4.z.string(),
114
124
  capabilities: ServerCapabilitiesSchema,
@@ -129,7 +139,8 @@ var ToolSchema = import_v4.z.object({
129
139
  import_v4.z.object({
130
140
  title: import_v4.z.optional(import_v4.z.string())
131
141
  }).loose()
132
- )
142
+ ),
143
+ _meta: ToolMetaSchema
133
144
  }).loose();
134
145
  var ListToolsResultSchema = PaginatedResultSchema.extend({
135
146
  tools: import_v4.z.array(ToolSchema)
@@ -207,6 +218,48 @@ var ReadResourceResultSchema = ResultSchema.extend({
207
218
  import_v4.z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
208
219
  )
209
220
  });
221
+ var PromptArgumentSchema = import_v4.z.object({
222
+ name: import_v4.z.string(),
223
+ description: import_v4.z.optional(import_v4.z.string()),
224
+ required: import_v4.z.optional(import_v4.z.boolean())
225
+ }).loose();
226
+ var PromptSchema = import_v4.z.object({
227
+ name: import_v4.z.string(),
228
+ title: import_v4.z.optional(import_v4.z.string()),
229
+ description: import_v4.z.optional(import_v4.z.string()),
230
+ arguments: import_v4.z.optional(import_v4.z.array(PromptArgumentSchema))
231
+ }).loose();
232
+ var ListPromptsResultSchema = PaginatedResultSchema.extend({
233
+ prompts: import_v4.z.array(PromptSchema)
234
+ });
235
+ var PromptMessageSchema = import_v4.z.object({
236
+ role: import_v4.z.union([import_v4.z.literal("user"), import_v4.z.literal("assistant")]),
237
+ content: import_v4.z.union([
238
+ TextContentSchema,
239
+ ImageContentSchema,
240
+ EmbeddedResourceSchema
241
+ ])
242
+ }).loose();
243
+ var GetPromptResultSchema = ResultSchema.extend({
244
+ description: import_v4.z.optional(import_v4.z.string()),
245
+ messages: import_v4.z.array(PromptMessageSchema)
246
+ });
247
+ var ElicitationRequestParamsSchema = BaseParamsSchema.extend({
248
+ message: import_v4.z.string(),
249
+ requestedSchema: import_v4.z.unknown()
250
+ });
251
+ var ElicitationRequestSchema = RequestSchema.extend({
252
+ method: import_v4.z.literal("elicitation/create"),
253
+ params: ElicitationRequestParamsSchema
254
+ });
255
+ var ElicitResultSchema = ResultSchema.extend({
256
+ action: import_v4.z.union([
257
+ import_v4.z.literal("accept"),
258
+ import_v4.z.literal("decline"),
259
+ import_v4.z.literal("cancel")
260
+ ]),
261
+ content: import_v4.z.optional(import_v4.z.record(import_v4.z.string(), import_v4.z.unknown()))
262
+ });
210
263
 
211
264
  // src/tool/json-rpc-message.ts
212
265
  var JSONRPC_VERSION = "2.0";
@@ -782,7 +835,8 @@ async function refreshAuthorization(authorizationServerUrl, {
782
835
  tokenUrl = new URL("/token", authorizationServerUrl);
783
836
  }
784
837
  const headers = new Headers({
785
- "Content-Type": "application/x-www-form-urlencoded"
838
+ "Content-Type": "application/x-www-form-urlencoded",
839
+ Accept: "application/json"
786
840
  });
787
841
  const params = new URLSearchParams({
788
842
  grant_type: grantType,
@@ -1525,13 +1579,16 @@ var DefaultMCPClient = class {
1525
1579
  constructor({
1526
1580
  transport: transportConfig,
1527
1581
  name: name3 = "ai-sdk-mcp-client",
1528
- onUncaughtError
1582
+ version = CLIENT_VERSION,
1583
+ onUncaughtError,
1584
+ capabilities
1529
1585
  }) {
1530
1586
  this.requestMessageId = 0;
1531
1587
  this.responseHandlers = /* @__PURE__ */ new Map();
1532
1588
  this.serverCapabilities = {};
1533
1589
  this.isClosed = true;
1534
1590
  this.onUncaughtError = onUncaughtError;
1591
+ this.clientCapabilities = capabilities != null ? capabilities : {};
1535
1592
  if (isCustomMcpTransport(transportConfig)) {
1536
1593
  this.transport = transportConfig;
1537
1594
  } else {
@@ -1541,18 +1598,22 @@ var DefaultMCPClient = class {
1541
1598
  this.transport.onerror = (error) => this.onError(error);
1542
1599
  this.transport.onmessage = (message) => {
1543
1600
  if ("method" in message) {
1544
- this.onError(
1545
- new MCPClientError({
1546
- message: "Unsupported message type"
1547
- })
1548
- );
1601
+ if ("id" in message) {
1602
+ this.onRequestMessage(message);
1603
+ } else {
1604
+ this.onError(
1605
+ new MCPClientError({
1606
+ message: "Unsupported message type"
1607
+ })
1608
+ );
1609
+ }
1549
1610
  return;
1550
1611
  }
1551
1612
  this.onResponse(message);
1552
1613
  };
1553
1614
  this.clientInfo = {
1554
1615
  name: name3,
1555
- version: CLIENT_VERSION
1616
+ version
1556
1617
  };
1557
1618
  }
1558
1619
  async init() {
@@ -1564,7 +1625,7 @@ var DefaultMCPClient = class {
1564
1625
  method: "initialize",
1565
1626
  params: {
1566
1627
  protocolVersion: LATEST_PROTOCOL_VERSION,
1567
- capabilities: {},
1628
+ capabilities: this.clientCapabilities,
1568
1629
  clientInfo: this.clientInfo
1569
1630
  }
1570
1631
  },
@@ -1617,6 +1678,14 @@ var DefaultMCPClient = class {
1617
1678
  });
1618
1679
  }
1619
1680
  break;
1681
+ case "prompts/list":
1682
+ case "prompts/get":
1683
+ if (!this.serverCapabilities.prompts) {
1684
+ throw new MCPClientError({
1685
+ message: `Server does not support prompts`
1686
+ });
1687
+ }
1688
+ break;
1620
1689
  default:
1621
1690
  throw new MCPClientError({
1622
1691
  message: `Unsupported method: ${method}`
@@ -1749,6 +1818,35 @@ var DefaultMCPClient = class {
1749
1818
  throw error;
1750
1819
  }
1751
1820
  }
1821
+ async listPromptsInternal({
1822
+ params,
1823
+ options
1824
+ } = {}) {
1825
+ try {
1826
+ return this.request({
1827
+ request: { method: "prompts/list", params },
1828
+ resultSchema: ListPromptsResultSchema,
1829
+ options
1830
+ });
1831
+ } catch (error) {
1832
+ throw error;
1833
+ }
1834
+ }
1835
+ async getPromptInternal({
1836
+ name: name3,
1837
+ args,
1838
+ options
1839
+ }) {
1840
+ try {
1841
+ return this.request({
1842
+ request: { method: "prompts/get", params: { name: name3, arguments: args } },
1843
+ resultSchema: GetPromptResultSchema,
1844
+ options
1845
+ });
1846
+ } catch (error) {
1847
+ throw error;
1848
+ }
1849
+ }
1752
1850
  async notification(notification) {
1753
1851
  const jsonrpcNotification = {
1754
1852
  ...notification,
@@ -1771,7 +1869,8 @@ var DefaultMCPClient = class {
1771
1869
  name: name3,
1772
1870
  description,
1773
1871
  inputSchema,
1774
- annotations
1872
+ annotations,
1873
+ _meta
1775
1874
  } of listToolsResult.tools) {
1776
1875
  const title = annotations == null ? void 0 : annotations.title;
1777
1876
  if (schemas !== "automatic" && !(name3 in schemas)) {
@@ -1798,7 +1897,7 @@ var DefaultMCPClient = class {
1798
1897
  inputSchema: schemas[name3].inputSchema,
1799
1898
  execute
1800
1899
  });
1801
- tools[name3] = toolWithExecute;
1900
+ tools[name3] = { ...toolWithExecute, _meta };
1802
1901
  }
1803
1902
  return tools;
1804
1903
  } catch (error) {
@@ -1822,6 +1921,90 @@ var DefaultMCPClient = class {
1822
1921
  } = {}) {
1823
1922
  return this.listResourceTemplatesInternal({ options });
1824
1923
  }
1924
+ listPrompts({
1925
+ params,
1926
+ options
1927
+ } = {}) {
1928
+ return this.listPromptsInternal({ params, options });
1929
+ }
1930
+ getPrompt({
1931
+ name: name3,
1932
+ arguments: args,
1933
+ options
1934
+ }) {
1935
+ return this.getPromptInternal({ name: name3, args, options });
1936
+ }
1937
+ onElicitationRequest(schema, handler) {
1938
+ if (schema !== ElicitationRequestSchema) {
1939
+ throw new MCPClientError({
1940
+ message: "Unsupported request schema. Only ElicitationRequestSchema is supported."
1941
+ });
1942
+ }
1943
+ this.elicitationRequestHandler = handler;
1944
+ }
1945
+ async onRequestMessage(request) {
1946
+ try {
1947
+ if (request.method !== "elicitation/create") {
1948
+ await this.transport.send({
1949
+ jsonrpc: "2.0",
1950
+ id: request.id,
1951
+ error: {
1952
+ code: -32601,
1953
+ message: `Unsupported request method: ${request.method}`
1954
+ }
1955
+ });
1956
+ return;
1957
+ }
1958
+ if (!this.elicitationRequestHandler) {
1959
+ await this.transport.send({
1960
+ jsonrpc: "2.0",
1961
+ id: request.id,
1962
+ error: {
1963
+ code: -32601,
1964
+ message: "No elicitation handler registered on client"
1965
+ }
1966
+ });
1967
+ return;
1968
+ }
1969
+ const parsedRequest = ElicitationRequestSchema.safeParse({
1970
+ method: request.method,
1971
+ params: request.params
1972
+ });
1973
+ if (!parsedRequest.success) {
1974
+ await this.transport.send({
1975
+ jsonrpc: "2.0",
1976
+ id: request.id,
1977
+ error: {
1978
+ code: -32602,
1979
+ message: `Invalid elicitation request: ${parsedRequest.error.message}`,
1980
+ data: parsedRequest.error.issues
1981
+ }
1982
+ });
1983
+ return;
1984
+ }
1985
+ try {
1986
+ const result = await this.elicitationRequestHandler(parsedRequest.data);
1987
+ const validatedResult = ElicitResultSchema.parse(result);
1988
+ await this.transport.send({
1989
+ jsonrpc: "2.0",
1990
+ id: request.id,
1991
+ result: validatedResult
1992
+ });
1993
+ } catch (error) {
1994
+ await this.transport.send({
1995
+ jsonrpc: "2.0",
1996
+ id: request.id,
1997
+ error: {
1998
+ code: -32603,
1999
+ message: error instanceof Error ? error.message : "Failed to handle elicitation request"
2000
+ }
2001
+ });
2002
+ this.onError(error);
2003
+ }
2004
+ } catch (error) {
2005
+ this.onError(error);
2006
+ }
2007
+ }
1825
2008
  onClose() {
1826
2009
  if (this.isClosed) return;
1827
2010
  this.isClosed = true;
@@ -1861,6 +2044,8 @@ var DefaultMCPClient = class {
1861
2044
  };
1862
2045
  // Annotate the CommonJS export names for ESM import in node:
1863
2046
  0 && (module.exports = {
2047
+ ElicitResultSchema,
2048
+ ElicitationRequestSchema,
1864
2049
  UnauthorizedError,
1865
2050
  auth,
1866
2051
  experimental_createMCPClient