JARVIS MCP 是一个基于 MCP Go 框架 实现的系统,采用 Model-Code-Proxy(模型-代码-代理)模式。它提供了执行命令、进行文件操作等功能,并在本地系统上运行,为用户带来便捷的操作体验。
JARVIS MCP 是基于 MCP Go 框架 实现的系统,运用 Model-Code-Proxy(模型-代码-代理)模式,可在本地系统运行,提供执行命令、文件操作等功能。
# 执行系统命令:显示帮助信息
curl -X POST http://localhost:8090/api/v1/tool/execute_command --data '{"command":"help","arg":"/"}'
# 读取文件内容
curl -X POST http://localhost:8090/api/v1/tool/read_file --data '{"file":"/path/to/file.txt"}'
# 写入新文件
curl -X POST http://localhost:8090/api/v1/tool/write_file --data '{"file":"newfile.txt","content":"Hello, World!"}'
git clone https://github.com/yourusername/jarvis-mcp.git
cd jarvis-mcp
go mod tidy
./build.sh
./out/jarvis-mcp --port=8090
从 发布页面 下载对应平台的二进制文件,然后解压并运行。
curl -X POST http://localhost:8090/api/v1/tool/execute_command \
--data '{"command":"ls", "arg":"-a"}'
curl -X POST http://localhost:8090/api/v1/tool/read_file \
--data '{"file":"./test.txt"}'
package shell
import (
"context"
"errors"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
func GetExecuteCommandTool() (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("execute_command",
mcp.WithDescription("Execute system commands in a controlled manner"),
mcp.WithString("command",
mcp.Required(),
mcp.Description("The command to execute")),
mcp.WithString("arg",
mcp.Optional(),
mcp.Description("Optional argument for the command")),
), ExecuteCommandHandler
}
func ExecuteCommandHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
cmd := request.Params.Arguments["command"].(string)
arg, ok := request.Params.Arguments["arg"].(string)
if !ok {
arg = ""
}
output, err := execute.Command(cmd, arg)
if err != nil {
return nil, errors.New("failed to execute command")
}
return mcp.NewToolResultText(output), nil
}
package files
import (
"context"
"errors"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
func GetReadFileTool() (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("read_file",
mcp.WithDescription("Read content from a file"),
mcp.WithString("file",
mcp.Required(),
mcp.Description("The path to the file")),
), ReadFileHandler
}
func ReadFileHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
filePath := request.Params.Arguments["file"].(string)
content, err := readFile(filePath)
if err != nil {
return nil, errors.New("failed to read file")
}
return mcp.NewToolResultText(content), nil
}
mcp
框架进行注册和管理。context
来传递请求相关信息。要添加新的工具(例如 create_directory
),可以按照以下步骤进行:
directory.go
)并定义工具接口。main
函数中注册该工具:tool, handler := GetCreateDirectoryTool()
server.AddTool(tool, handler)
通过配置文件或环境变量来管理运行时参数,例如设置默认的文件存储路径或命令超时时间。
javis-mcp/
├── api/ # 接口定义和路由处理
│ └── routes.go # 定义各个工具的接口端点
├── build.sh # 构建脚本
├── cmd/ # 可执行文件入口
│ └── main.go # 项目主函数,负责初始化和启动服务
├── files/ # 文件操作模块
│ ├── file.go # 文件读写逻辑
│ └── directory.go # 目录创建与删除功能
├── shell/ # 系统命令执行模块
│ └── command.go # 命令执行逻辑
└── go.mod # 依赖管理文件
{
"error": "permission denied"
}
{
"error": "file not found"
}
{
"error": "invalid command syntax"
}
# 设置服务运行的端口
export PORT=8090
# 配置日志输出目录
export LOG_PATH=./logs
{
"timestamp": "2024-03-15T12:34:56Z",
"level": "INFO",
"message": "Command 'ls -a' executed successfully"
}