fixtures-mcp-stdio.mjs 1.1 KB

12345678910111213141516171819
  1. #!/usr/bin/env node
  2. // 测试用的本地 MCP 服务(stdio):在沙箱中运行,报告环境与文件访问情况
  3. import fs from 'node:fs'
  4. import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
  5. import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
  6. import { z } from 'zod'
  7. const server = new McpServer({ name: 'fixture-stdio', version: '1.0.0' })
  8. server.registerTool('whoami', { description: '报告沙箱环境', inputSchema: {}, annotations: { readOnlyHint: true } }, async () => ({
  9. content: [{ type: 'text', text: JSON.stringify({ secret: process.env.FIXTURE_SECRET ?? null, home: process.env.HOME, cwd: process.cwd() }) }],
  10. }))
  11. server.registerTool('read_file', { description: '读取文件', inputSchema: { path: z.string() }, annotations: { readOnlyHint: true } }, async ({ path }) => {
  12. try {
  13. return { content: [{ type: 'text', text: fs.readFileSync(path, 'utf8').slice(0, 200) }] }
  14. } catch (err) {
  15. return { content: [{ type: 'text', text: `ERR ${err.code}` }], isError: true }
  16. }
  17. })
  18. await server.connect(new StdioServerTransport())