模块化MCP服务器

一个基于Go语言实现的模块化MCP服务器,提供文件操作和命令执行工具,具有路径限制和安全性配置功能。

  • by da-snap
  • Go
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 da-snap/mcp-server-developer-tool

使用说明

模块化MCP服务器

这是模型上下文协议(MCP)服务器的Go实现,使用 github.com/metoro-io/mcp-golang 库,以模块化方式重组。

项目结构

mcp-server/
├── cmd/
│   └── mcp-server/
│       └── main.go           # Entry point
├── internal/
│   ├── config/
│   │   └── config.go         # Server configuration
│   ├── server/
│   │   ├── server.go         # MCP server implementation
│   │   └── server_test.go    # Server tests
│   ├── tools/
│   │   ├── tool.go           # Tool interface
│   │   ├── execute.go        # Execute shell command tool
│   │   ├── showfile.go       # Show file tool
│   │   ├── searchfile.go     # Search in file tool
│   │   └── writefile.go      # Write file tool
│   └── utils/
│       └── response.go       # Common response utilities
├── go.mod
├── go.sum
└── README.md

建立和运行

# Build the server
cd cmd/mcp-server
go build -o mcp-server

# Run the server
./mcp-server

安全功能

服务器现在包括一个路径限制系统,将文件操作限制在指定的目录中。

配置允许的路径

您可以通过多种方式配置允许的路径:

1.使用命令行标志

# Allow operations only in specific directories
./mcp-server --paths=/home/user/safe:/tmp/workspace

# Explicitly deny specific paths even if within allowed paths
./mcp-server --paths=/home/user --deny-paths=/home/user/.ssh:/home/user/credentials

2.使用环境变量

# Set allowed paths
export MCP_ALLOWED_PATHS=/home/user/safe:/tmp/workspace

# Set denied paths
export MCP_DENIED_PATHS=/home/user/.ssh:/home/user/credentials

# Run the server
./mcp-server

3.方案上

您还可以通过编程创建自定义配置:

cfg := config.DefaultConfig()
cfg.AddAllowedPath("/path/to/allow")
cfg.AddDeniedPath("/path/to/deny")

server, err := server.NewServerWithConfig(cfg)

默认行为

  • 如果没有指定路径,服务器默认只允许当前工作目录。
  • 常见的敏感目录,如 .git 和 .env 被自动添加到拒绝列表中。

壳牌指挥安全

对于 execute_shell_command 工具:

  • 命令仅限于常用实用程序的白名单
  • 根据允许的路径配置检查自定义可执行路径
  • 工作目录必须在允许的路径内

添加新工具

要添加新工具,请执行以下操作:

  1. 在中创建新文件 internal/tools 目录
  2. 实施 Tool 接口
  3. 可选地实施 ConfigAware 如果您的工具需要访问服务器配置,请使用界面
  4. 在中注册该工具 cmd/mcp-server/main.go

例子:

// internal/tools/newtool.go
package tools

import (
    "github.com/metoro-io/mcp-golang"
    "mcp-server/internal/config"
    "mcp-server/internal/utils"
)

type NewToolArgs struct {
    // Tool arguments
}

type NewTool struct{
    config *config.ServerConfig
}

func NewNewTool() *NewTool {
    return &NewTool{}
}

// Implement ConfigAware interface
func (t *NewTool) SetConfig(cfg *config.ServerConfig) {
    t.config = cfg
}

func (t *NewTool) Name() string {
    return "new_tool"
}

func (t *NewTool) Description() string {
    return "Description of the new tool"
}

func (t *NewTool) Execute(args NewToolArgs) (*mcp.ToolResponse, error) {
    // Access configuration if needed
    if t.config != nil {
        // Use configuration for security checks
    }

    // Tool implementation
    return utils.CreateSuccessResponse(result), nil
}

测试

go test ./...