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