模型上下文协议服务器

Model Context Protocol (MCP) 是一个新兴的可移植工具定义标准。
MCP定义了一种协议,允许共享工具并使用它们,而无需考虑底层框架/运行时环境。
GenAIScript 实现了一个将脚本转换为MCP工具的服务器。
将脚本作为MCP工具使用
GenAIScript 启动一个 MCP 服务器,将每个 GenAIScript 脚本作为 MCP 工具公开(不要与 defTool
混淆)。
MCP工具参数是从脚本参数和文件中自动推断出来的。
然后MCP参数会像往常一样填充到脚本中的env.vars
对象。
MCP工具的输出即为脚本的输出。也就是说,通常这是使用顶层上下文的脚本的最后一条助手消息。 或者是通过env.output传入的任何内容。
让我们看一个例子。这里有一个脚本task.genai.mjs
,它接收一个task
参数输入,构建提示词并将LLM的输出返回。
script({ parameters: { task: { type: "string", description: "The task to perform", required: true } }})
const { task } = env.vars // extract the task parameter
... // genaiscript logic$`... prompt ... ${task}` // output the result
一个更高级的脚本可能不会使用顶层上下文,而是使用env.output
来传递结果。
script({ accept: "none", // this script does not use 'env.files' parameters: { task: { type: "string", description: "The task to perform", required: true } }})
const { output } = env // store the output builderconst { task } = env.vars // extract the task parameter
... // genaiscript logic with inline promptsconst res = runPrompt(_ => `... prompt ... ${task}`) // run some inner the prompt...
// build the outputoutput.fence(`The result is ${res.text}`)
使用说明
mcp
命令使用 stdio 传输启动 MCP 服务器。
- @modelcontextprotocol/inspector 是一个MCP客户端,可用于检查服务器并列出可用的工具。
npx --yes @modelcontextprotocol/inspector npx --yes genaiscript mcp
- Claude桌面版
{ "mcpServers": { "genaiscript": { "command": "npx", "args": ["-y", "genaiscript", "mcp"] } }}
- 带有GitHub Copilot Chat的Visual Studio Code Insiders版本
{ "servers": { "genaiscript": { "type": "stdio", "command": "node", "args": [ "npx", "-y", "genaiscript", "mcp", "--cwd", "${workspaceFolder}", "--groups", "mcp" ], "envFile": "${workspaceFolder}/.env" } }}
从远程仓库运行脚本
您可以使用--remote
选项从远程仓库加载脚本。
GenAIScript会对仓库进行浅克隆,并从克隆文件夹中运行脚本。
npx --yes genaiscript mcp --remote https://github.com/...
还有一些关于如何克隆仓库的额外标志:
--remote-branch <branch>
: 从远程仓库克隆的分支。--remote-force
: 即使克隆文件夹已存在,也强制进行克隆。--remote-install
: 克隆仓库后安装依赖项。