@opentiny/next-sdk 0.1.1 → 0.1.2

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/WebMcpServer.ts CHANGED
@@ -88,6 +88,10 @@ export class WebMcpServer {
88
88
  this.server.server.onerror = (error: Error) => {
89
89
  this.onerror?.(error)
90
90
  }
91
+
92
+ this.server.server.setRequestHandler(SetLevelRequestSchema, async () => {
93
+ return {}
94
+ })
91
95
  }
92
96
 
93
97
  /**
@@ -53,7 +53,7 @@ export class AgentModelProvider {
53
53
 
54
54
  // 每次会话需要获取最新的工具列表,因为工具是会发生变化的
55
55
  await this.initClients()
56
- const tools = await getMcpTools(this.mcpClients)
56
+ const tools = await getMcpTools(this.mcpClients, options)
57
57
 
58
58
  return generateText({
59
59
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
@@ -75,7 +75,7 @@ export class AgentModelProvider {
75
75
 
76
76
  // 每次会话需要获取最新的工具列表,因为工具是会发生变化的
77
77
  await this.initClients()
78
- const tools = await getMcpTools(this.mcpClients)
78
+ const tools = await getMcpTools(this.mcpClients, options)
79
79
 
80
80
  return streamText({
81
81
  // @ts-ignore 同上
@@ -1,10 +1,8 @@
1
- import {
2
- experimental_createMCPClient as createMCPClient,
3
- ToolSet,
4
- experimental_MCPClientConfig as MCPClientConfig
5
- } from 'ai'
1
+ import { experimental_createMCPClient as createMCPClient, experimental_MCPClientConfig as MCPClientConfig } from 'ai'
6
2
  import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
7
3
  import { McpServerConfig, MCPClient } from '../type'
4
+ import { dynamicTool, jsonSchema, Tool, ToolCallOptions, ToolSet } from 'ai'
5
+ import { WebMcpClient } from '../../WebMcpClient'
8
6
 
9
7
  /** 创建 McpClients, 其中 mcpServers 允许为配置为 McpServerConfig, 或者任意的 MCPTransport
10
8
  * 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client
@@ -37,8 +35,45 @@ export const getMcpClients = async (mcpServers: McpServerConfig[]) => {
37
35
  }
38
36
 
39
37
  /** 合并所有的Mcp Tools */
40
- export const getMcpTools = async (mcpClients: MCPClient[]): Promise<ToolSet> => {
38
+ export const getMcpTools = async (mcpClients: MCPClient[], options: Record<string, any>): Promise<ToolSet> => {
41
39
  const tools = await Promise.all(mcpClients.map((client) => client?.tools?.()))
40
+ const toolsResult = tools.reduce((acc, curr) => ({ ...acc, ...curr }), {})
41
+ const toolsOptions = options?.tools ?? {}
42
42
 
43
- return tools.reduce((acc, curr) => ({ ...acc, ...curr }), {})
43
+ return {
44
+ ...toolsResult,
45
+ ...toolsOptions
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Returns a set of AI SDK tools from the MCP server
51
+ * @returns A record of tool names to their implementations
52
+ */
53
+ export const getAISDKTools = async (client: WebMcpClient): Promise<ToolSet> => {
54
+ const tools: Record<string, Tool> = {}
55
+
56
+ try {
57
+ const listToolsResult = await client.listTools()
58
+
59
+ for (const { name, description, inputSchema } of listToolsResult.tools) {
60
+ const execute = async (args: any, options: ToolCallOptions): Promise<any> => {
61
+ return client.callTool({ name, arguments: args }, { signal: options?.abortSignal })
62
+ }
63
+
64
+ tools[name] = dynamicTool({
65
+ description,
66
+ inputSchema: jsonSchema({
67
+ ...inputSchema,
68
+ properties: (inputSchema.properties as Record<string, any>) ?? {},
69
+ additionalProperties: false
70
+ }),
71
+ execute
72
+ })
73
+ }
74
+
75
+ return tools
76
+ } catch (error) {
77
+ throw error
78
+ }
44
79
  }
@@ -41,6 +41,9 @@ export class WebMcpServer {
41
41
  var _a;
42
42
  (_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
43
43
  };
44
+ this.server.server.setRequestHandler(SetLevelRequestSchema, () => __awaiter(this, void 0, void 0, function* () {
45
+ return {};
46
+ }));
44
47
  }
45
48
  /**
46
49
  * Connects the server to a transport via the specified option.
@@ -64,7 +64,7 @@ export class AgentModelProvider {
64
64
  }
65
65
  // 每次会话需要获取最新的工具列表,因为工具是会发生变化的
66
66
  yield this.initClients();
67
- const tools = yield getMcpTools(this.mcpClients);
67
+ const tools = yield getMcpTools(this.mcpClients, options);
68
68
  return generateText(Object.assign({
69
69
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
70
70
  model: this.llm(model), tools: tools, stopWhen: stepCountIs(maxSteps) }, options));
@@ -78,7 +78,7 @@ export class AgentModelProvider {
78
78
  }
79
79
  // 每次会话需要获取最新的工具列表,因为工具是会发生变化的
80
80
  yield this.initClients();
81
- const tools = yield getMcpTools(this.mcpClients);
81
+ const tools = yield getMcpTools(this.mcpClients, options);
82
82
  return streamText(Object.assign({
83
83
  // @ts-ignore 同上
84
84
  model: this.llm(model), tools: tools, stopWhen: stepCountIs(maxSteps) }, options));
@@ -1,8 +1,14 @@
1
- import { ToolSet } from 'ai';
2
1
  import { McpServerConfig, MCPClient } from '../type';
2
+ import { ToolSet } from 'ai';
3
+ import { WebMcpClient } from '../../WebMcpClient';
3
4
  /** 创建 McpClients, 其中 mcpServers 允许为配置为 McpServerConfig, 或者任意的 MCPTransport
4
5
  * 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client
5
6
  */
6
7
  export declare const getMcpClients: (mcpServers: McpServerConfig[]) => Promise<(MCPClient | never[])[]>;
7
8
  /** 合并所有的Mcp Tools */
8
- export declare const getMcpTools: (mcpClients: MCPClient[]) => Promise<ToolSet>;
9
+ export declare const getMcpTools: (mcpClients: MCPClient[], options: Record<string, any>) => Promise<ToolSet>;
10
+ /**
11
+ * Returns a set of AI SDK tools from the MCP server
12
+ * @returns A record of tool names to their implementations
13
+ */
14
+ export declare const getAISDKTools: (client: WebMcpClient) => Promise<ToolSet>;
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { experimental_createMCPClient as createMCPClient } from 'ai';
11
11
  import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
12
+ import { dynamicTool, jsonSchema } from 'ai';
12
13
  /** 创建 McpClients, 其中 mcpServers 允许为配置为 McpServerConfig, 或者任意的 MCPTransport
13
14
  * 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client
14
15
  */
@@ -37,7 +38,35 @@ export const getMcpClients = (mcpServers) => __awaiter(void 0, void 0, void 0, f
37
38
  return allMcpClients;
38
39
  });
39
40
  /** 合并所有的Mcp Tools */
40
- export const getMcpTools = (mcpClients) => __awaiter(void 0, void 0, void 0, function* () {
41
+ export const getMcpTools = (mcpClients, options) => __awaiter(void 0, void 0, void 0, function* () {
42
+ var _a;
41
43
  const tools = yield Promise.all(mcpClients.map((client) => { var _a; return (_a = client === null || client === void 0 ? void 0 : client.tools) === null || _a === void 0 ? void 0 : _a.call(client); }));
42
- return tools.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), curr)), {});
44
+ const toolsResult = tools.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), curr)), {});
45
+ const toolsOptions = (_a = options === null || options === void 0 ? void 0 : options.tools) !== null && _a !== void 0 ? _a : {};
46
+ return Object.assign(Object.assign({}, toolsResult), toolsOptions);
47
+ });
48
+ /**
49
+ * Returns a set of AI SDK tools from the MCP server
50
+ * @returns A record of tool names to their implementations
51
+ */
52
+ export const getAISDKTools = (client) => __awaiter(void 0, void 0, void 0, function* () {
53
+ var _a;
54
+ const tools = {};
55
+ try {
56
+ const listToolsResult = yield client.listTools();
57
+ for (const { name, description, inputSchema } of listToolsResult.tools) {
58
+ const execute = (args, options) => __awaiter(void 0, void 0, void 0, function* () {
59
+ return client.callTool({ name, arguments: args }, { signal: options === null || options === void 0 ? void 0 : options.abortSignal });
60
+ });
61
+ tools[name] = dynamicTool({
62
+ description,
63
+ inputSchema: jsonSchema(Object.assign(Object.assign({}, inputSchema), { properties: (_a = inputSchema.properties) !== null && _a !== void 0 ? _a : {}, additionalProperties: false })),
64
+ execute
65
+ });
66
+ }
67
+ return tools;
68
+ }
69
+ catch (error) {
70
+ throw error;
71
+ }
43
72
  });