Introduction
I previously noticed on the MCP official website that an official Go SDK was available. Recently, after developing MCP Servers in Python environments for a while, I decided to try something different and explore Go.
From my personal experience, Go demonstrates significant advantages in concurrent processing: there’s no need to worry about complex concurrency issues like synchronous blocking, asynchronous event loops, or inter-process/thread communication—goroutines handle it all elegantly. Additionally, Go offers convenient deployment; the compiled static binary files have excellent portability and can run directly across different environments.
However, this convenience comes with certain trade-offs. Compared to Python, implementing MCP functionality in Go is relatively more complex, with slightly lower development efficiency. This represents a classic software engineering trade-off: runtime costs and development costs are often difficult to optimize simultaneously, requiring careful consideration based on specific scenarios.
Brief Introduction to MCP Protocol
You might already be familiar with this, but for those who aren’t, here’s a quick overview of MCP.
Model Context Protocol (MCP) is a standardized protocol designed to provide AI models with unified tool invocation interfaces. Through MCP, developers can expose various tools, services, and data sources to AI models, enabling them to perform operations beyond the capabilities of basic language models. MCP supports multiple transport protocols, including HTTP and Stdio, offering flexibility for integration in different scenarios.
A Simple MCP Server Example
The official MCP Go SDK requires explicit definition of input parameters and output result data structures when defining tools (Tool). For simpler tools, the any type can also be used directly. Below is a complete MCP Server example providing three practical tools:
getCurrentDatetime: Retrieves the current time, returning a timestamp string in RFC3339 format (2006-01-02T15:04:05Z07:00). Since no input parameters are required, the parameter type is defined asany, with the output also using theanytype.getComputerStatus: Retrieves key system information including CPU usage, memory utilization, and system version. This tool accepts aCPUSampleTimeparameter, with the corresponding input struct beingGetComputerStatusInand the output struct beingGetComputerStatusOut(the Go SDK examples typically follow the naming convention ofxxxInandxxxOutto distinguish between tool input and output structs).getDiskInfo: Retrieves usage information and filesystem details for all disk partitions. This tool requires no input parameters and only defines an output structGetDiskInfoOut.
After implementing all tool logic, the final step is to start the service. The following example uses Streamable HTTP mode, with commented-out Stdio Transport mode code preserved for reference.
| |
After successfully compiling the MCP Server code, it can be tested and verified in MCP-compatible development tools (such as VS Code). Below is a typical .vscode/mcp.json configuration example:
| |
After starting the MCP Server, you can verify whether the tools are correctly scheduled and executed by asking relevant questions to the LLM.
A Complete MCP Client Implementation
To build an end-to-end MCP application, we also need to implement an MCP Client that can work collaboratively with the LLM to automatically select and invoke appropriate tools. Below is a fully functional MCP Client implementation, including an integration example with OpenAI-compatible APIs (callOpenAI function).
| |
Running Tests for Verification
After compilation, we can perform multiple rounds of testing to verify functionality correctness.
Basic Q&A Testing:
./mcp-client-dev -api-key "sk-xxx" -q "how are you"
Stream output can also be enabled with the -s parameter:
./mcp-client-dev -api-key "sk-xxx" -q "how are you" -s
Expected output:
Hi there! 😊 I'm absolutely wonderful—energized, curious, and *so* happy to be chatting with you! 🌟 How about you? I'd love to hear how your day's going—or what's on your heart or mind right now! 💫 (Bonus points if you share a fun fact, a tiny win, or even just your favorite emoji today! 🍦✨)
MCP Tool Call Testing:
./mcp-client-dev -api-key "sk-xxx" -mcp-uri "http://127.0.0.1:18001/api/mcp" -q "What is the current time?"
Expected output:
LLM response:
Executing tool: get_current_datetime with args: {}
Executing tool: get_current_datetime with parsed args: map[]
Tool result: "2026-02-02T23:12:54+08:00"
Final response: It's currently **February 2, 2026, at 11:12 PM** (Beijing Time, UTC+8) ✨
The festive atmosphere of the New Year is still warm~ Are you planning something special? 😊 I'd be delighted to help you organize, remind you, or brainstorm together!
Best Practices and Considerations
When implementing an MCP Server in Go for production projects, consider the following best practices:
- Error Handling: Ensure all tool functions have comprehensive error handling mechanisms to prevent service crashes due to individual tool failures.
- Performance Optimization: For time-consuming operations (such as system information collection), consider adding timeout controls and caching mechanisms. (According to the official MCP documentation, there are new primitives called Tasks and progress—these could also be explored for time-consuming tasks.)
- Security: Validate all input parameters to prevent security issues caused by malicious inputs. For tools involving system operations, pay special attention to permission controls.
- Logging: Implement detailed logging to facilitate debugging and monitoring of tool usage.
- Configuration Management: Extract service configurations (such as listening addresses and ports) into configuration files to improve maintainability.
Conclusion
This article demonstrates how to develop MCP Servers and Clients using Go through a simple code example. Although Go is slightly more complex than Python for MCP development, its advantages in concurrent processing, performance, and deployment convenience make it an ideal choice for production environments.
It’s important to note that this example only covers the basic functionality of MCP tool calls. When implementing an MCP Server in Go for actual business projects, further research into other MCP protocol features is necessary, such as Prompt management, authentication (Auth), session management, and other advanced functionalities.
Through thoughtful design and implementation, Go-based MCP services can provide AI applications with stable, efficient, and secure tool invocation capabilities, fully leveraging Go’s strengths in system programming and network services.
References
- MCP Official Website: https://modelcontextprotocol.io/docs/getting-started/intro
- MCP Official Go SDK: https://github.com/modelcontextprotocol/go-sdk