MCP服务器适配器
描述

这 mcp-server-adapter 是一个Go包,旨在简化模型上下文协议(MCP)服务器与LangChain Go应用程序的集成。它充当中介,允许LangChain代理无缝地发现、使用和管理各种MCP服务器公开的工具。此适配器处理服务器生命周期管理、配置监视以及将MCP工具转换为LangChain可使用的格式。
注:此项目目前正在进行中。在功能上,随着它的发展,它可能会发生重大变化,包括打破API的变化。
特性
- 无缝的LangChain集成:将MCP服务器工具作为本地LangChain公开
tools.Tool物体。 - MCP服务器生命周期管理:启动、停止和监视多个MCP服务器的状态。
- 动态配置:支持从JSON文件加载服务器配置和热重新加载更改。
- 服务器状态和配置访问:轻松检查服务器是否已禁用或检索其完整配置。
- 文件监视器:自动检测配置文件中的更改并作出反应,重新启动受影响的服务器。
- 多种运输支持:通过标准I/O(stdio)、服务器发送事件(SSE)和HTTP传输连接到MCP服务器。
- 稳健的错误处理:包括安全关闭服务器连接和处理常见错误的机制。
- 可扩展:设计有易于扩展和测试的接口(例如,自定义客户端工厂)。
安装
此项目需要Go 1.21或更高版本。
- 克隆存储库:
git clone https://github.com/denkhaus/mcp-server-adapter.git
cd mcp-server-adapter- 安装依赖项:
go mod tidy- 构建项目:
go build ./...运行示例
这些示例可以使用提供的构建和运行 Makefile。导航到项目根目录并使用 make 命令。
例如,要构建所有示例:
make build-examples要运行一个特定的示例,如演示:
make run-demo用法
这 mcp-server-adapter 通常在Go应用程序中用于管理与MCP服务器的连接,并将它们的工具集成到LLM代理中。
- 定义您的MCP服务器配置:创建JSON文件(例如。,
config.json)指定应用程序需要连接到的MCP服务器。请参阅examples/mcp-spec-config.json作为一个实际例子。
{
"mcpServers": {
"fetcher": {
"command": "npx",
"args": ["-y", "fetcher-mcp"],
"transport": "stdio"
},
"tavily-mcp": {
"command": "npx",
"args": ["-y", "tavily-mcp@0.1.3"],
"transport": "stdio"
}
}
}- 初始化适配器:
package main
import (
"context"
"log"
"time"
"github.com/denkhaus/mcp-server-adapter"
)
func main() {
adapter, err := mcpadapter.New(
mcpadapter.WithConfigPath("./config.json"),
mcpadapter.WithLogLevel("info"),
mcpadapter.WithFileWatcher(true), // Enable hot-reloading of config
)
if err != nil {
log.Fatalf("Failed to create MCP adapter: %v", err)
}
defer adapter.Close()
ctx := context.Background()
// Start all configured MCP servers
if err := adapter.StartAllServers(ctx); err != nil {
log.Fatalf("Failed to start all MCP servers: %v", err)
}
// Wait for servers to be ready
if err := adapter.WaitForServersReady(ctx, 30*time.Second); err != nil {
log.Fatalf("Servers not ready: %v", err)
}
// Get all available tools from all running servers
allTools, err := adapter.GetAllTools(ctx)
if err != nil {
log.Fatalf("Failed to get LangChain tools: %v", err)
}
log.Printf("Available tools: %d", len(allTools))
for _, tool := range allTools {
log.Printf("- %s: %s", tool.Name(), tool.Description())
}
// Example: Check if a server is disabled
isDisabled, err := adapter.IsServerDisabled("fetcher")
if err != nil {
log.Printf("Error checking if server is disabled: %v", err)
} else if isDisabled {
log.Printf("Server 'fetcher' is disabled.")
} else {
log.Printf("Server 'fetcher' is enabled.")
}
// Example: Get server configuration
serverConfig, err := adapter.GetServerConfig("tavily-mcp")
if err != nil {
log.Printf("Error getting server config: %v", err)
} else {
log.Printf("Server 'tavily-mcp' command: %s", serverConfig.Command)
}
// Example: Using a tool (replace with actual tool usage)
// If you have a "fetcher.fetch_url" tool, you could call it like this:
// if len(allTools) > 0 {
// ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// defer cancel()
// result, err := allTools[0].Call(ctx, `{"url": "https://example.com"}`)
// if err != nil {
// log.Printf("Tool call failed: %v", err)
// } else {
// log.Printf("Tool result: %s", result)
// }
// }
}API变更
为了提高清晰度和一致性,以下API函数已被重命名:
GetServerStatus(serverName string) ServerStatus现在是GetServerStatusByName(serverName string) ServerStatus.GetLangChainTools(ctx context.Context, serverName string) ([]tools.Tool, error)现在是GetToolsByServerName(ctx context.Context, serverName string) ([]tools.Tool, error).GetAllLangChainTools(ctx context.Context) ([]tools.Tool, error)现在是GetAllTools(ctx context.Context) ([]tools.Tool, error).
例子
这 mcp-server-adapter 包括几个示例来演示其功能以及与LangChain应用程序的集成。有关详细的使用方法、代码和技术规格,请参阅 examples/ 目录,尤其是 examples/EXAMPLES_SUMMARY.md 文件。
以下是关键示例的总结:
生成文件目标
此项目使用 Makefile 自动化各种任务,包括构建、测试、linting和运行示例。以下是可用的完整列表 make 目标:
构建目标
make build-all:构建所有二进制文件。make build-examples:构建所有示例二进制文件。make build-demo:构建演示示例。make build-agent:构建代理示例。make build-server:构建服务器示例。make build-simple-demo:构建简单的演示示例。
测试目标
make test:运行所有测试。make test-examples:运行所有带超时的示例测试。make test-demo:测试演示示例(带超时)。make test-agent:测试代理示例(带超时)。make test-simple-demo:测试简单的演示示例(带超时)。make test-servers:测试单个MCP服务器。make test-config:验证配置文件。
Linting和代码质量目标
make lint:跑golangci lint。make lint-fix:运行golangci lint并自动修复。make fmt:格式化Go代码。make vet兽医,快跑。make check:运行所有代码质量检查。make ci:完整的CI管道。
发展目标
make run-demo:构建并运行演示。make run-agent:构建并运行代理。make run-simple-demo:构建并运行简单的演示。
清理
make clean:清理构建工件。
帮助
make help:显示此帮助消息。
______________________________________________________________________
1.🖥️ MCP服务器 (examples/server/)
- 目的:演示如何使用创建基本MCP服务器
mark3labs/mcp-go. - 特性:实现a
fetch_url通过stdio传输进行web内容检索的工具。
2. 🤖 朗链代理 (examples/agent/)
- 目的:演示如何将MCP工具与LangChain代理集成。
- 特性:支持多LLM(OpenAI、Google AI、Anthropic),将MCP工具与标准LangChain工具相结合,并自动管理MCP服务器生命周期。
3. 🚀 简单演示 (examples/simple-demo/)
- 目的:不需要LLM API密钥的基本演示。
- 特性:显示MCP适配器创建和工具发现,使用真实的HTTP请求测试工具执行,并且除了Go之外没有外部依赖关系。
4. 🧪 测试服务器 (examples/test-servers/)
- 目的:提供以不同语言(Node.js、Python)实现的简单MCP服务器,用于测试目的。
5.⚙️ 配置文件 (examples/config.json, examples/enhanced-config.json, examples/mcp-spec-config.json)
- 目的:说明MCP服务器的各种配置,包括
stdio和sse传输和不同的命令参数。
配置
这 mcp-server-adapter 通过JSON文件进行配置。这 Config 结构体(定义于 config.go 和 types.go)概述了预期的结构。
关键配置选项包括:
mcpServers:一个映射,其中键是服务器名称,值是ServerConfig物体。ServerConfig:
- command:(必需 stdio transport)执行MCP服务器的命令。 - args:(可选)传递给命令的参数。 - cwd:(可选)服务器进程的工作目录。 - env:(可选)服务器进程的环境变量。 - disabled:(可选, true/false)如果 true,服务器将不会启动。 - transport:(可选,默认值 stdio)通信运输(stdio, sse, http). - url:(必需 sse, http 传输)MCP服务器的URL。 - headers:(可选)的HTTP标头 sse 或 http 运输。 - timeout:(可选)服务器操作超时。 - Method:(可选,默认值 POST 为了 http transport)的HTTP方法 http 运输(例如。, GET, POST). - tool_prefix:(可选,默认空字符串)已解析工具名称的自定义前缀。指定后,工具名称格式将从 serverName.toolName 向 tool_prefix/toolname. 例子: "tool_prefix": "magic-prefix" - alwaysAllow:(可选)始终允许的工具名称列表,可用于权限或访问控制。
灵感
这个项目的灵感来自 langchaingo-mcp-adapter.
贡献
欢迎投稿!请参阅项目的问题跟踪器以了解未完成的任务,或提交包含改进和错误修复的拉取请求。确保你的代码遵循现有的风格和约定。
许可证
该项目根据MIT许可证获得许可。请参阅 LICENSE 文件以获取详细信息。
