@opentiny/next-sdk 0.3.0-alpha.0 → 0.3.1
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/WebMcpClient.ts +7 -1
- package/agent/AgentModelProvider.ts +11 -1
- package/agent/type.ts +22 -1
- package/agent/utils/getBuiltinMcpTools.ts +86 -0
- package/dist/{AgentModelProvider-BIOOEdcN.js → AgentModelProvider-BnMj6YSC.js} +820 -706
- package/dist/WebMcpClient.d.ts +15 -8
- package/dist/agent/AgentModelProvider.d.ts +5 -2
- package/dist/agent/type.d.ts +23 -0
- package/dist/agent/utils/getBuiltinMcpTools.d.ts +12 -0
- package/dist/core.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.es.dev.js +1872 -691
- package/dist/index.es.js +10777 -9902
- package/dist/index.js +1665 -917
- package/dist/index.umd.dev.js +1872 -691
- package/dist/index.umd.js +55 -52
- package/dist/page-tools/bridge.d.ts +15 -86
- package/dist/utils/builtinProxy.d.ts +7 -0
- package/dist/utils/env.d.ts +8 -0
- package/dist/webagent.dev.js +164 -9
- package/dist/webagent.es.dev.js +164 -9
- package/dist/webagent.es.js +3067 -2940
- package/dist/webagent.js +35 -35
- package/dist/webmcp-full.dev.js +79 -1
- package/dist/webmcp-full.es.dev.js +79 -1
- package/dist/webmcp-full.es.js +377 -314
- package/dist/webmcp-full.js +7 -7
- package/dist/webmcp.dev.js +79 -1
- package/dist/webmcp.es.dev.js +79 -1
- package/dist/webmcp.es.js +323 -260
- package/dist/webmcp.js +1 -1
- package/index.ts +17 -0
- package/package.json +2 -1
- package/page-tools/bridge.ts +168 -821
- package/page-tools/effects.ts +3 -1
- package/utils/builtinProxy.ts +90 -0
- package/utils/env.ts +13 -0
package/page-tools/effects.ts
CHANGED
|
@@ -24,10 +24,12 @@ let labelElement: HTMLDivElement | null = null
|
|
|
24
24
|
let styleElement: HTMLStyleElement | null = null
|
|
25
25
|
let activeCount = 0
|
|
26
26
|
|
|
27
|
+
import { isDomAvailable } from '../utils/env'
|
|
28
|
+
|
|
27
29
|
const BODY_GLOW_CLASS = 'next-sdk-tool-body-glow'
|
|
28
30
|
|
|
29
31
|
function ensureDomReady() {
|
|
30
|
-
return
|
|
32
|
+
return isDomAvailable()
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
function ensureStyleElement() {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 建立原生的 JSON-RPC 拦截层:
|
|
5
|
+
* 将发向 remote 的 MCP 请求拦截,代理给所在浏览器的 navigator.modelContextTesting 上执行。
|
|
6
|
+
*/
|
|
7
|
+
export const setupBuiltinProxy = (transport: Transport) => {
|
|
8
|
+
transport.onmessage = async (message: any) => {
|
|
9
|
+
if (!message || typeof message !== 'object') return
|
|
10
|
+
const id = message.id
|
|
11
|
+
try {
|
|
12
|
+
if (message.method === 'initialize') {
|
|
13
|
+
await transport.send({
|
|
14
|
+
jsonrpc: '2.0',
|
|
15
|
+
id,
|
|
16
|
+
result: {
|
|
17
|
+
protocolVersion: '2024-11-05',
|
|
18
|
+
capabilities: { tools: {} },
|
|
19
|
+
serverInfo: { name: 'browser-builtin-webmcp-proxy', version: '1.0.0' }
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
} else if (message.method === 'notifications/initialized') {
|
|
23
|
+
// Ignore
|
|
24
|
+
} else if (message.method === 'ping' || message.method === 'custom_ping') {
|
|
25
|
+
await transport.send({ jsonrpc: '2.0', id, result: {} })
|
|
26
|
+
} else if (message.method === 'tools/list') {
|
|
27
|
+
const nativeCtx =
|
|
28
|
+
typeof navigator !== 'undefined'
|
|
29
|
+
? (navigator as any).modelContextTesting || (navigator as any).modelContext
|
|
30
|
+
: null
|
|
31
|
+
if (nativeCtx && nativeCtx.listTools) {
|
|
32
|
+
const rawTools = await nativeCtx.listTools()
|
|
33
|
+
const tools = rawTools.map((t: any) => {
|
|
34
|
+
let schemaObj: any = {}
|
|
35
|
+
if (typeof t.inputSchema === 'string') {
|
|
36
|
+
try {
|
|
37
|
+
schemaObj = JSON.parse(t.inputSchema)
|
|
38
|
+
} catch (e) {
|
|
39
|
+
console.error('Failed to parse inputSchema:', e)
|
|
40
|
+
}
|
|
41
|
+
} else if (typeof t.inputSchema === 'object' && t.inputSchema !== null) {
|
|
42
|
+
schemaObj = t.inputSchema
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
name: t.name,
|
|
46
|
+
description: t.description || '',
|
|
47
|
+
inputSchema: {
|
|
48
|
+
type: 'object',
|
|
49
|
+
properties: schemaObj.properties || {},
|
|
50
|
+
required: schemaObj.required || []
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
await transport.send({ jsonrpc: '2.0', id, result: { tools } })
|
|
55
|
+
} else {
|
|
56
|
+
await transport.send({ jsonrpc: '2.0', id, result: { tools: [] } })
|
|
57
|
+
}
|
|
58
|
+
} else if (message.method === 'tools/call') {
|
|
59
|
+
const nativeCtx = typeof navigator !== 'undefined' ? navigator.modelContextTesting || navigator.modelContext : null
|
|
60
|
+
if (nativeCtx && nativeCtx.executeTool) {
|
|
61
|
+
const { name, arguments: args } = message.params
|
|
62
|
+
const result = await nativeCtx.executeTool(name, JSON.stringify(args || {}))
|
|
63
|
+
// 如果结果已经是 MCP 格式 ({ content: [...] }),直接转发;否则包装为 text
|
|
64
|
+
const finalResult =
|
|
65
|
+
result && typeof result === 'object' && 'content' in result
|
|
66
|
+
? result
|
|
67
|
+
: { content: [{ type: 'text', text: typeof result === 'string' ? result : JSON.stringify(result) }] }
|
|
68
|
+
await transport.send({ jsonrpc: '2.0', id, result: finalResult })
|
|
69
|
+
} else {
|
|
70
|
+
await transport.send({
|
|
71
|
+
jsonrpc: '2.0',
|
|
72
|
+
id,
|
|
73
|
+
error: { code: -32601, message: 'Browser built-in WebMCP not available' }
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
} else if (id !== undefined) {
|
|
77
|
+
// 符合协议:对于未处理且带有 id 的请求,返回 Method not found
|
|
78
|
+
await transport.send({
|
|
79
|
+
jsonrpc: '2.0',
|
|
80
|
+
id,
|
|
81
|
+
error: { code: -32601, message: `Method not found: ${message.method}` }
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
} catch (err: any) {
|
|
85
|
+
if (id !== undefined) {
|
|
86
|
+
await transport.send({ jsonrpc: '2.0', id, error: { code: -32000, message: err.message || String(err) } })
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
package/utils/env.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 判断当前是否处于有效的浏览器环境
|
|
3
|
+
*/
|
|
4
|
+
export const isBrowser = (): boolean => {
|
|
5
|
+
return typeof window !== 'undefined' && typeof navigator !== 'undefined'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 判断当前是否处于有效且能访问 document 的浏览器环境
|
|
10
|
+
*/
|
|
11
|
+
export const isDomAvailable = (): boolean => {
|
|
12
|
+
return isBrowser() && typeof document !== 'undefined'
|
|
13
|
+
}
|