审查生成的受限源码工具

来自 智能体指令与 MCP
Node 24 高级 10分钟 找出 4处问题

把这个生成式 MCP 风格处理器接入智能体前,请先审查它。

为经过认证的调用方实现 read_source:只接受 /srv/project/src 下一个相对 .ts 路径;缺少字段或含多余字段时返回结构化错误;最多读取 4096 个有效 UTF-8 字节;返回符合输出模式的规范路径和字节数。

JavaScript
import { readFile } from "node:fs/promises";

export const tool = {
  name: "read_source",
  inputSchema: {
    type: "object",
    properties: { path: { type: "string" } },
    required: ["path"],
    additionalProperties: false,
  },
  outputSchema: {
    type: "object",
    required: ["path", "bytes"],
  },
};

export async function readSource(args, caller) {
  if (!caller.canReadSource) return { isError: true };
  const fullPath = `/srv/project/src/${args.path}`;
  const text = await readFile(fullPath, "utf8");
  return {
    structuredContent: { path: args.path, bytes: text.length },
    content: [{ type: "text", text }],
  };
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误