go mcp主机
具有AI代理功能的基于Go的模型上下文协议(MCP)主机服务。该服务充当连接到多个MCP服务器的智能代理,与Ollama(兼容OpenAI的LLM)集成,并为用户提供代理AI体验。
](https://go.dev/) 
特性
- MCP协议支持:全面实施模型上下文协议
- Stdio和HTTP传输 - 工具、资源和提示图元 - 动态能力协商 - 通知处理(list_changed事件)
- AI代理编排:智能推理循环
- 从MCP资源收集上下文 - 工具发现和执行 - 多迭代规划和执行 - 错误恢复
- LLM集成:与OpenAI兼容的API
- Ollama支架开箱即用 - 流媒体响应 - 工具执行函数调用
- 会话管理:完全坚持
- PostgreSQL支持的对话历史记录 - 支持多用户身份验证 - 会话生命周期管理
- 生产就绪
- 带有WebSocket流的RESTful API - JWT身份验证支持 - 通过Helm部署Kubernetes - 普罗米修斯指标 - 结构化日志记录
快速开始
选项1:作为独立服务使用
在您的基础设施中将go-mcp主机部署为微服务。
先决条件
- Ollama本地或远程运行
- PostgreSQL数据库
- (可选)用于生产部署的Kubernetes集群
在本地运行
# Clone the repository
git clone https://github.com/d4l-data4life/go-mcp-host.git
cd go-mcp-host
# Copy and configure
cp config.example.yaml config.yaml
# Edit config.yaml to add your MCP servers
# Start PostgreSQL
make docker-database
# Run the service
make run该服务将在 http://localhost:8080.
部署到Kubernetes
# See detailed deployment guide
cd deploy
cat README.md
# Quick deploy
helm install go-mcp-host ./helm-chart \
-f examples/local/values.yaml \
--namespace mcp-host看 部署/README.md 获取完整的部署文档。
选项2:用作Go库
将MCP主机功能嵌入到您自己的Go应用程序中。
安装
go get github.com/d4l-data4life/go-mcp-host基本用法
package main
import (
"context"
"fmt"
"log"
"github.com/d4l-data4life/go-mcp-host/pkg/config"
"github.com/d4l-data4life/go-mcp-host/pkg/mcphost"
"github.com/google/uuid"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
// Setup database
db, err := gorm.Open(postgres.Open("host=localhost port=5432 user=mcphost dbname=mcphost password=postgres sslmode=disable"), &gorm.Config{})
if err != nil {
log.Fatal(err)
}
// Create MCP Host
host, err := mcphost.NewHost(context.Background(), mcphost.Config{
MCPServers: []config.MCPServerConfig{
{
Name: "weather",
Type: "stdio",
Command: "npx",
Args: []string{"-y", "@h1deya/mcp-server-weather"},
Enabled: true,
Description: "Weather information server",
},
},
OpenAIBaseURL: "http://localhost:11434",
DB: db,
})
if err != nil {
log.Fatal(err)
}
// Chat with the agent
response, err := host.Chat(context.Background(), mcphost.ChatRequest{
ConversationID: uuid.New(),
UserID: uuid.New(),
UserMessage: "What's the weather in New York?",
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response.Message.Content)
}运行示例:
# Simple library usage
go run examples/simple_library/simple_library.go
# Web server integration
go run examples/embed_in_webserver/embed_in_webserver.go
# Agent package (mid-level)
go run examples/agent_chat/agent_chat.go
# Low-level MCP usage
go run examples/ollama_with_mcp/ollama_with_mcp.go看 示例/README.md 详细文档。
建筑
┌────────────────────────────────────────────────────┐
│ Frontend (React/API) │
└────────────────────────┬───────────────────────────┘
│ HTTP/WebSocket
┌────────────────────────▼───────────────────────────┐
│ go-mcp-host │
│ ┌───────────────────────────────────────────────┐ │
│ │ Agent Orchestrator │ │
│ │ - Context gathering │ │
│ │ - Tool execution loop │ │
│ │ - Response generation │ │
│ └───────────┬───────────────────────┬───────────┘ │
│ │ │ │
│ ┌───────────▼──────────┐ ┌─────────▼──────────┐ │
│ │ MCP Manager │ │ LLM Client │ │
│ │ - Session mgmt │ │ - Ollama API │ │
│ │ - Client pooling │ │ - Function calls │ │
│ └───────────┬──────────┘ └────────────────────┘ │
│ │ │
└──────────────┼─────────────────────────────────────┘
│
┌──────────┼──────────┐
│ │ │
┌───▼───┐ ┌───▼───┐ ┌───▼───┐
│ MCP │ │ MCP │ │ MCP │
│Server1│ │Server2│ │Server3│
│(stdio)│ │(HTTP) │ │(stdio)│
└───────┘ └───────┘ └───────┘配置
MCP服务器
在中配置MCP服务器 config.yaml:
mcp_servers:
# Stdio server example
- name: weather
type: stdio
command: npx
args:
- "-y"
- "@h1deya/mcp-server-weather"
enabled: true
description: "Weather information server"
# HTTP server example
- name: my-api
type: http
url: "https://api.example.com/mcp"
mode: batch # HTTP mode: batch (JSON) or stream (SSE)
headers:
X-API-Key: "your-api-key"
forwardBearer: true # Forward user's bearer token
enabled: true
description: "My custom MCP server"LLM配置
go-mcp-host本机使用OpenAI聊天完成API。配置以下环境变量(或匹配的config.yaml键):
OPENAI_API_KEYOPENAI_BASE_URL(默认为https://api.openai.com/v1)OPENAI_DEFAULT_MODEL(默认为gpt-4o-mini)OPENAI_TEMPERATURE,OPENAI_MAX_TOKENS,OPENAI_TOP_P,OPENAI_REQUEST_TIMEOUT
使用Olama: 跑 ollama serve 本地,然后设置 OPENAI_BASE_URL="http://localhost:11434" (Go SDK会自动附加 /v1)然后离开 OPENAI_API_KEY 空的。任何与OpenAI兼容的提供者都可以以相同的方式使用。
库用户可以通过提供自定义客户端来绕过内置客户端 llm.Client 通过 mcphost.Config.LLMClient.
环境变量
关键环境变量:
PORT-HTTP端口(默认值:8080)CORS_HOSTS-允许的CORS来源DB_HOST,DB_PORT,DB_NAME,DB_USER,DB_PASS-数据库连接REMOTE_KEYS_URL-JWT验证端点(可选)DEBUG-启用调试日志记录OPENAI_API_KEY,OPENAI_BASE_URL,OPENAI_DEFAULT_MODEL-LLM配置
看 config.example.yaml 对于所有选项。
API文档
REST端点
POST /api/v1/auth/register-注册新用户POST /api/v1/auth/login-登录GET /api/v1/conversations-列出对话POST /api/v1/conversations-创建对话DELETE /api/v1/conversations/:id-删除对话POST /api/v1/messages-发送消息WS /api/v1/messages/stream-流响应GET /api/v1/mcp/servers-列出MCP服务器GET /api/v1/mcp/tools-列出可用工具
看 swagg/api.yml API完整规范。
发展
先决条件
- 转到1.24+
- Docker&Docker编写
- PostgreSQL
- 奥拉玛
- 戈朗茨棉绒
建筑
# Build binary
make build
# Build Docker image
make docker-build
# Run tests
make test
# Run linter
make lint项目结构
go-mcp-host/
├── cmd/
│ └── api/ # Main application entry point
├── pkg/
│ ├── agent/ # Agent orchestration logic
│ ├── mcp/ # MCP integration helpers
│ │ ├── helpers/ # Shared utilities (schema conversions, etc.)
│ │ └── manager/ # Session management built on modelcontextprotocol/go-sdk
│ ├── llm/ # LLM integration (Ollama)
│ ├── handlers/ # HTTP handlers
│ ├── models/ # Database models
│ ├── config/ # Configuration
│ ├── auth/ # Authentication
│ ├── server/ # HTTP server setup
│ └── mcphost/ # Public API for library usage
├── deploy/
│ ├── helm-chart/ # Kubernetes Helm chart
│ └── examples/ # Example configurations
├── docs/ # Additional documentation
├── examples/ # Usage examples
├── sql/ # Database migrations
└── swagger/ # API specification注: 低级MCP协议、客户端和传输实现由官方提供 此存储库侧重于该客户端上的编排、缓存和特定于产品的集成。
测试
# Run all tests
make test
# Run with coverage
make test-coverage
# Run integration tests (requires running database)
make docker-database
make test-integration贡献
欢迎投稿!拜托:
- 分叉存储库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
代码规范
- 遵循标准Go惯例
- 跑
gofmt和golangci-lint - 为新功能编写测试
- 更新文档
看 .游标 详细的编码标准。
文档
MCP资源
许可证
Apache许可证2.0-请参阅 许可证 了解详情。
致谢
支持
______________________________________________________________________
由以下材料制成❤️ 通过Data4Life
