模块化MCP开发工具
STDIO模块化结构的MCP服务器实现
模块化结构的MCP服务器实现
This is a Go implementation of the Model Context Protocol (MCP) server using the github.com/metoro-io/mcp-golang
library, restructured in a modular way.
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
The server now includes a path restriction system to limit file operations to specified directories.
You can configure allowed paths in several ways:
# 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
# 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
You can also create a custom configuration programmatically:
cfg := config.DefaultConfig() cfg.AddAllowedPath("/path/to/allow") cfg.AddDeniedPath("/path/to/deny") server, err := server.NewServerWithConfig(cfg)
.git
and .env
are automatically added to the deny list.For the execute_shell_command
tool:
To add a new tool:
internal/tools
directoryTool
interfaceConfigAware
interface if your tool needs access to server configurationcmd/mcp-server/main.go
Example:
// 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 ./...