@ai-sdk/mcp 1.0.0-beta.9 → 1.0.0

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