11<script lang="ts" setup>
22import { TinyRemoter } from ' @opentiny/next-remoter'
3- import { createMessageChannelPairTransport , WebMcpClient , WebMcpServer , z } from ' @opentiny/next-sdk'
43import { TinyConfigProvider } from ' @opentiny/vue'
54import TinyThemeTool from ' @opentiny/vue-theme/theme-tool'
6- import { onMounted , provide , ref } from ' vue'
7- import { useRoute , useRouter } from ' vue-router'
5+ import { onMounted } from ' vue'
86import GlobalSetting from ' @/components/global-setting/index.vue'
97import { useTheme } from ' ./hooks/useTheme'
8+ import { clientTransport , createMcpServer } from ' ./mcp-servers'
9+ import { skills } from ' ./skills'
1010import ' @opentiny/next-remoter/dist/style.css'
1111
1212const theme = new TinyThemeTool ()
@@ -25,51 +25,18 @@ const design = {
2525 },
2626}
2727
28- const sessionId = ref (' ' )
29- const [serverTransport, clientTransport] = createMessageChannelPairTransport ()
30- provide (' serverTransport' , serverTransport )
31-
32- const AGENT_URL = ' https://agent.opentiny.design/api/v1/webmcp-trial/'
28+ // 将本地 MCP Server 注册到 TinyRemoter
29+ // key 为服务器名称(自定义),type: 'local' 表示浏览器本地运行
30+ const mcpServers = {
31+ ' my-mcp-server' : {
32+ type: ' local' ,
33+ transport: clientTransport ,
34+ },
35+ }
3336
37+ // 启动 MCP Server(注册工具 + 建立通信通道)
3438onMounted (async () => {
35- const server = new WebMcpServer ()
36- const $router = useRouter ()
37- const $route = useRoute ()
38-
39- // TODO: 参数需要优化,用户不会知道具体的路由路径,用户的语言可能是:帮我打开菜单管理页面,这时应该根据名称获取路由路径,再做路由跳转
40- // 进一步优化:用户可能在任意页面直接提需求:帮我创建 xx 菜单,这时 AI 应该先跳转菜单管理页面,然后调佣创建菜单的工具
41- server .registerTool (
42- ' switch-router' ,
43- {
44- title: ' 切换路由' ,
45- description: ' 切换路由' ,
46- inputSchema: {
47- routerPath: z .string ().describe (' 路由路径' ),
48- },
49- },
50- async ({ routerPath }) => {
51- if ($route .path === routerPath ) {
52- return { content: [{ type: ' text' , text: routerPath }] }
53- }
54-
55- $router .push (import .meta .env .VITE_CONTEXT + routerPath )
56- return { content: [{ type: ' text' , text: routerPath }] }
57- },
58- )
59- await server .connect (serverTransport )
60-
61- // 创建 WebMcpClient ,并与 WebAgent 连接
62- const client = new WebMcpClient ()
63- await client .connect (clientTransport )
64- const { sessionId : sessionID } = await client .connect ({
65- agent: true ,
66-
67- // sessionId 为可选参数。若传入该参数,系统将使用指定值作为会话标识;若未传入,WebAgent 服务将自动生成一个随机的字符串作为 sessionId。为便于通过 MCP Inspector 工具进行调试,此处采用了固定的 sessionId。用户亦可通过浏览器原生提供的 crypto.randomUUID() 方法生成随机字符串作为会话标识。
68- sessionId: ' d299a869-c674-4125-a84b-bb4e24079b99' ,
69-
70- url: ` ${AGENT_URL }mcp ` ,
71- })
72- sessionId .value = sessionID
39+ await createMcpServer ()
7340})
7441 </script >
7542
@@ -82,22 +49,8 @@ onMounted(async () => {
8249 <GlobalSetting />
8350 </div >
8451 <TinyRemoter
85- :agent-root =" AGENT_URL"
86- :session-id =" sessionId"
87- :menu-items =" [
88- {
89- action: 'qr-code',
90- show: false,
91- },
92- {
93- action: 'remote-control',
94- show: false,
95- },
96- {
97- action: 'remote-url',
98- show: false,
99- },
100- ]"
52+ :skills =" skills"
53+ :mcp-servers =" mcpServers"
10154 />
10255</template >
10356
0 commit comments