@opentiny/next-sdk 0.1.3 → 0.1.4
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/McpSdk.ts +14 -0
- package/WebAgent.ts +5 -0
- package/WebMcp.ts +18 -0
- package/Zod.ts +1 -0
- package/agent/AgentModelProvider.ts +150 -35
- package/agent/type.ts +1 -1
- package/agent/utils/getAISDKTools.ts +35 -0
- package/dist/agent/AgentModelProvider.d.ts +43 -4
- package/dist/agent/AgentModelProvider.js +145 -24
- package/dist/agent/type.d.ts +1 -1
- package/dist/agent/utils/getAISDKTools.d.ts +8 -0
- package/dist/agent/utils/getAISDKTools.js +36 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.es.dev.js +42947 -0
- package/dist/index.es.js +17998 -17742
- package/dist/index.js +10 -4
- package/dist/index.umd.dev.js +43341 -0
- package/dist/index.umd.js +285 -114
- package/dist/mcpsdk@1.17.0.dev.js +21391 -0
- package/dist/mcpsdk@1.17.0.es.dev.js +21389 -0
- package/dist/mcpsdk@1.17.0.es.js +14505 -0
- package/dist/mcpsdk@1.17.0.js +16 -0
- package/dist/remoter/createRemoter.d.ts +10 -2
- package/dist/remoter/createRemoter.js +312 -55
- package/dist/webagent.dev.js +27509 -0
- package/dist/webagent.es.dev.js +27115 -0
- package/dist/webagent.es.js +21448 -0
- package/dist/webagent.js +529 -0
- package/dist/webmcp-full.dev.js +22915 -0
- package/dist/webmcp-full.es.dev.js +22911 -0
- package/dist/webmcp-full.es.js +15821 -0
- package/dist/webmcp-full.js +16 -0
- package/dist/webmcp.dev.js +1373 -0
- package/dist/webmcp.es.dev.js +1366 -0
- package/dist/webmcp.es.js +1232 -0
- package/dist/webmcp.js +1 -0
- package/dist/zod@3.25.76.dev.js +4039 -0
- package/dist/zod@3.25.76.es.dev.js +4035 -0
- package/dist/zod@3.25.76.es.js +2947 -0
- package/dist/zod@3.25.76.js +1 -0
- package/index.ts +16 -4
- package/package.json +16 -4
- package/remoter/createRemoter.ts +327 -62
- package/runtime.html +98 -0
- package/script/utils.ts +26 -0
- package/vite.config.mcpSdk.ts +28 -0
- package/vite.config.ts +13 -12
- package/vite.config.webAgent.ts +19 -0
- package/vite.config.webMcp.ts +40 -0
- package/vite.config.webMcpFull.ts +19 -0
- package/vite.config.zod.ts +23 -0
- package/agent/utils/aiProviderFactories.ts +0 -7
- package/agent/utils/index.ts +0 -79
- package/dist/agent/utils/aiProviderFactories.d.ts +0 -6
- package/dist/agent/utils/aiProviderFactories.js +0 -6
- package/dist/agent/utils/index.d.ts +0 -14
- package/dist/agent/utils/index.js +0 -72
- package/dist/index.cjs.js +0 -365
package/vite.config.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { dirname, resolve } from 'node:path'
|
|
2
|
-
import { fileURLToPath } from 'node:url'
|
|
3
1
|
import { defineConfig } from 'vite'
|
|
4
2
|
|
|
5
|
-
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
6
|
-
|
|
7
3
|
// https://vitejs.dev/config/
|
|
8
|
-
export default defineConfig({
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
4
|
+
export default defineConfig(({ mode }) => {
|
|
5
|
+
const shouldMinify = mode !== 'dev'
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
build: {
|
|
9
|
+
emptyOutDir: shouldMinify,
|
|
10
|
+
minify: shouldMinify,
|
|
11
|
+
lib: {
|
|
12
|
+
entry: 'index.ts',
|
|
13
|
+
name: 'WebMCP',
|
|
14
|
+
formats: ['es', 'umd'],
|
|
15
|
+
fileName: (format) => `index.${format}${shouldMinify ? '' : '.dev'}.js`
|
|
16
|
+
}
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineConfig } from 'vite'
|
|
2
|
+
|
|
3
|
+
// https://vitejs.dev/config/
|
|
4
|
+
export default defineConfig(({ mode }) => {
|
|
5
|
+
const shouldMinify = mode !== 'dev'
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
build: {
|
|
9
|
+
emptyOutDir: false,
|
|
10
|
+
minify: shouldMinify,
|
|
11
|
+
lib: {
|
|
12
|
+
entry: 'WebAgent.ts',
|
|
13
|
+
name: 'WebAgent',
|
|
14
|
+
formats: ['es', 'umd'],
|
|
15
|
+
fileName: (format) => `webagent${format === 'es' ? '.es' : ''}${shouldMinify ? '' : '.dev'}.js`
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { defineConfig } from 'vite'
|
|
2
|
+
|
|
3
|
+
// https://vitejs.dev/config/
|
|
4
|
+
export default defineConfig(({ mode }) => {
|
|
5
|
+
// 根据构建模式决定是否启用代码压缩
|
|
6
|
+
// development 模式:不压缩,便于调试
|
|
7
|
+
// production 模式:启用压缩,优化性能
|
|
8
|
+
const shouldMinify = mode !== 'dev'
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
build: {
|
|
12
|
+
emptyOutDir: false,
|
|
13
|
+
minify: shouldMinify, // 动态设置压缩配置
|
|
14
|
+
lib: {
|
|
15
|
+
entry: 'WebMcp.ts',
|
|
16
|
+
name: 'WebMCP',
|
|
17
|
+
formats: ['es', 'umd'],
|
|
18
|
+
fileName: (format) => `webmcp${format === 'es' ? '.es' : ''}${shouldMinify ? '' : '.dev'}.js`
|
|
19
|
+
},
|
|
20
|
+
rollupOptions: {
|
|
21
|
+
external: [/@modelcontextprotocol\/sdk\//, 'zod', 'ajv'],
|
|
22
|
+
output: {
|
|
23
|
+
globals: {
|
|
24
|
+
'@modelcontextprotocol/sdk/client/streamableHttp.js': 'MCPSDK',
|
|
25
|
+
'@modelcontextprotocol/sdk/types.js': 'MCPSDK',
|
|
26
|
+
'@modelcontextprotocol/sdk/client/index.js': 'MCPSDK',
|
|
27
|
+
'@modelcontextprotocol/sdk/client/sse.js': 'MCPSDK',
|
|
28
|
+
'@modelcontextprotocol/sdk/client/auth.js': 'MCPSDK',
|
|
29
|
+
'@modelcontextprotocol/sdk/server/mcp.js': 'MCPSDK',
|
|
30
|
+
'@modelcontextprotocol/sdk/shared/uriTemplate.js': 'MCPSDK',
|
|
31
|
+
'@modelcontextprotocol/sdk/server/completable.js': 'MCPSDK',
|
|
32
|
+
'@modelcontextprotocol/sdk/shared/metadataUtils.js': 'MCPSDK',
|
|
33
|
+
'zod': 'Zod',
|
|
34
|
+
'ajv': 'MCPSDK'
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineConfig } from 'vite'
|
|
2
|
+
|
|
3
|
+
// https://vitejs.dev/config/
|
|
4
|
+
export default defineConfig(({ mode }) => {
|
|
5
|
+
const shouldMinify = mode !== 'dev'
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
build: {
|
|
9
|
+
emptyOutDir: false,
|
|
10
|
+
minify: shouldMinify,
|
|
11
|
+
lib: {
|
|
12
|
+
entry: 'WebMcp.ts',
|
|
13
|
+
name: 'WebMCP',
|
|
14
|
+
formats: ['es', 'umd'],
|
|
15
|
+
fileName: (format) => `webmcp-full${format === 'es' ? '.es' : ''}${shouldMinify ? '' : '.dev'}.js`
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { defineConfig } from 'vite'
|
|
2
|
+
import { getPackageVersion } from './script/utils'
|
|
3
|
+
|
|
4
|
+
// https://vitejs.dev/config/
|
|
5
|
+
export default defineConfig(({ mode }) => {
|
|
6
|
+
const shouldMinify = mode !== 'dev'
|
|
7
|
+
|
|
8
|
+
return {
|
|
9
|
+
build: {
|
|
10
|
+
emptyOutDir: false,
|
|
11
|
+
minify: shouldMinify,
|
|
12
|
+
lib: {
|
|
13
|
+
entry: 'Zod.ts',
|
|
14
|
+
name: 'Zod',
|
|
15
|
+
formats: ['es', 'umd'],
|
|
16
|
+
fileName: (format) => {
|
|
17
|
+
const version = getPackageVersion('zod')
|
|
18
|
+
return `zod@${version}${format === 'es' ? '.es' : ''}${shouldMinify ? '' : '.dev'}.js`
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
})
|
package/agent/utils/index.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { experimental_createMCPClient as createMCPClient, experimental_MCPClientConfig as MCPClientConfig } from 'ai'
|
|
2
|
-
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
|
|
3
|
-
import { McpServerConfig, MCPClient } from '../type'
|
|
4
|
-
import { dynamicTool, jsonSchema, Tool, ToolCallOptions, ToolSet } from 'ai'
|
|
5
|
-
import { WebMcpClient } from '../../WebMcpClient'
|
|
6
|
-
|
|
7
|
-
/** 创建 McpClients, 其中 mcpServers 允许为配置为 McpServerConfig, 或者任意的 MCPTransport
|
|
8
|
-
* 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client
|
|
9
|
-
*/
|
|
10
|
-
export const getMcpClients = async (mcpServers: McpServerConfig[]) => {
|
|
11
|
-
if (!mcpServers || mcpServers?.length === 0) {
|
|
12
|
-
return []
|
|
13
|
-
}
|
|
14
|
-
// 使用 Promise.all 并行处理所有 mcpServer 项
|
|
15
|
-
const allMcpClients = await Promise.all(
|
|
16
|
-
mcpServers.map(async (item: McpServerConfig) => {
|
|
17
|
-
try {
|
|
18
|
-
let transport: MCPClientConfig['transport']
|
|
19
|
-
// FIXME
|
|
20
|
-
if ('type' in item && item.type === 'streamableHttp') {
|
|
21
|
-
transport = new StreamableHTTPClientTransport(new URL(item.url))
|
|
22
|
-
} else {
|
|
23
|
-
transport = item as MCPClientConfig['transport'] // sse 或 自定义的 MCPTranport
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return createMCPClient({ transport: transport as MCPClientConfig['transport'] })
|
|
27
|
-
} catch (error) {
|
|
28
|
-
console.error(`Failed to create MCP client`, item, error)
|
|
29
|
-
return []
|
|
30
|
-
}
|
|
31
|
-
})
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
return allMcpClients
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** 合并所有的Mcp Tools */
|
|
38
|
-
export const getMcpTools = async (mcpClients: MCPClient[], options: Record<string, any>): Promise<ToolSet> => {
|
|
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
|
-
|
|
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
|
-
}
|
|
79
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { McpServerConfig, MCPClient } from '../type';
|
|
2
|
-
import { ToolSet } from 'ai';
|
|
3
|
-
import { WebMcpClient } from '../../WebMcpClient';
|
|
4
|
-
/** 创建 McpClients, 其中 mcpServers 允许为配置为 McpServerConfig, 或者任意的 MCPTransport
|
|
5
|
-
* 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client
|
|
6
|
-
*/
|
|
7
|
-
export declare const getMcpClients: (mcpServers: McpServerConfig[]) => Promise<(MCPClient | never[])[]>;
|
|
8
|
-
/** 合并所有的Mcp Tools */
|
|
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>;
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { experimental_createMCPClient as createMCPClient } from 'ai';
|
|
11
|
-
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
12
|
-
import { dynamicTool, jsonSchema } from 'ai';
|
|
13
|
-
/** 创建 McpClients, 其中 mcpServers 允许为配置为 McpServerConfig, 或者任意的 MCPTransport
|
|
14
|
-
* 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client
|
|
15
|
-
*/
|
|
16
|
-
export const getMcpClients = (mcpServers) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
-
if (!mcpServers || (mcpServers === null || mcpServers === void 0 ? void 0 : mcpServers.length) === 0) {
|
|
18
|
-
return [];
|
|
19
|
-
}
|
|
20
|
-
// 使用 Promise.all 并行处理所有 mcpServer 项
|
|
21
|
-
const allMcpClients = yield Promise.all(mcpServers.map((item) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
-
try {
|
|
23
|
-
let transport;
|
|
24
|
-
// FIXME
|
|
25
|
-
if ('type' in item && item.type === 'streamableHttp') {
|
|
26
|
-
transport = new StreamableHTTPClientTransport(new URL(item.url));
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
transport = item; // sse 或 自定义的 MCPTranport
|
|
30
|
-
}
|
|
31
|
-
return createMCPClient({ transport: transport });
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
console.error(`Failed to create MCP client`, item, error);
|
|
35
|
-
return [];
|
|
36
|
-
}
|
|
37
|
-
})));
|
|
38
|
-
return allMcpClients;
|
|
39
|
-
});
|
|
40
|
-
/** 合并所有的Mcp Tools */
|
|
41
|
-
export const getMcpTools = (mcpClients, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
-
var _a;
|
|
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); }));
|
|
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
|
-
}
|
|
72
|
-
});
|