# mcp-go — Build MCP Servers in Go > mcp-go is a Go SDK for building MCP servers (tools, resources, prompts) and serving them over stdio transports, so agents can call your Go code as tools. ## Install Merge the JSON below into your `.mcp.json`: ## Quick Use 1. Create a scratch server: ```bash mkdir mcp-go-demo && cd mcp-go-demo go mod init example.com/mcp-go-demo go get github.com/mark3labs/mcp-go ``` 2. Add a minimal server (see README Quickstart) and run: ```bash go run . ``` 3. Connect your MCP client to the stdio transport and call the `calculate` tool. ## Intro mcp-go is a Go SDK for building MCP servers (tools, resources, prompts) and serving them over stdio transports, so agents can call your Go code as tools. - **Best for:** Go teams who want first-class tool servers (strong typing, middleware, session handling) for agent workflows - **Works with:** Go 1.x toolchain, stdio MCP clients, server-side tool/resource/prompt patterns - **Setup time:** 12 minutes ## Practical Notes - Quickstart includes a calculator tool example you can run in <15 minutes - The README contains multiple example sections (server/resources/tools/prompts/session management) you can copy incrementally ## A Go-Native Pattern for Tool Design Treat each MCP tool as a stable “function contract”: - Keep inputs small and typed (string/number/enum) so the agent can call it reliably. - Put your real side effects behind a thin validation layer. - Prefer idempotent operations (safe retries) and provide clear error messages. Once you have one tool, scale to a toolkit: 1. Add resources for read-only context (like listing projects or configs). 2. Add tools for side effects (create/update/apply). 3. Add prompts to encode standard operating procedures for your team. That split (resources vs tools vs prompts) makes agent behavior easier to reason about and audit. ### FAQ **Q: Is mcp-go only for servers?** A: It’s primarily a server SDK; it helps you expose tools/resources/prompts to MCP clients. **Q: Do I need HTTP?** A: No. The README quickstart uses stdio transport, which works with many clients. **Q: How do I keep tools safe?** A: Validate inputs, keep side effects minimal, and prefer idempotent operations. ## Source & Thanks > Source: https://github.com/mark3labs/mcp-go > License: MIT > GitHub stars: 8,682 · forks: 826 --- ## 快速使用 1. 创建一个最小示例: ```bash mkdir mcp-go-demo && cd mcp-go-demo go mod init example.com/mcp-go-demo go get github.com/mark3labs/mcp-go ``` 2. 按 README 的 Quickstart 粘贴最小 server 代码,然后运行: ```bash go run . ``` 3. 把 MCP 客户端连到 stdio transport,测试调用 `calculate` 工具。 ## 简介 mcp-go 是 Go 语言的 MCP SDK:用统一协议把你的 Go 代码暴露为 tools/resources/prompts,并通过 stdio 等传输交给 AI 客户端调用,适合把业务能力封装成可审计的工具层。 - **适合谁:** 想用强类型 Go 写工具服务(tool server),并用于 Agent 工作流的团队 - **可搭配:** Go 工具链、stdio MCP 客户端、server 侧 tools/resources/prompts 模式 - **准备时间:** 12 分钟 ## 实战建议 - Quickstart 提供可直接跑通的计算器 tool 示例,通常 <15 分钟即可冒烟 - README 里按主题给了多段示例(server/resources/tools/prompts/session),可逐步拷贝扩展 ## 用 Go 写“可控工具”的建议 把每个 MCP tool 当成一个稳定的“函数契约”: - 输入尽量小且强类型(string/number/enum),让 Agent 更可靠地调用。 - 把真实副作用放在薄薄的一层校验之后(避免脏输入直达系统)。 - 优先做幂等操作(可安全重试),错误信息写清楚(便于 Agent 自修复)。 从一个 tool 扩展为一套工具箱的路径: 1. 先加 resources 提供只读上下文(列项目/配置等)。 2. 再加 tools 做有副作用的动作(create/update/apply)。 3. 用 prompts 固化团队 SOP(让“怎么用”变成可复用模板)。 resources / tools / prompts 三分法会让 Agent 行为更容易审计与推理。 ### FAQ **mcp-go 只做 server 吗?** 答:主要面向 server 侧:把 tools/resources/prompts 暴露给 MCP 客户端。 **必须用 HTTP 吗?** 答:不需要,README quickstart 使用 stdio 传输,很多客户端都支持。 **怎么让工具更安全?** 答:严格校验输入、收敛副作用、尽量幂等并提供清晰错误信息。 ## 来源与感谢 > Source: https://github.com/mark3labs/mcp-go > License: MIT > GitHub stars: 8,682 · forks: 826 --- Source: https://tokrepo.com/en/workflows/mcp-go-build-mcp-servers-in-go Author: MCP Hub