|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +MCP-Go makes it easy to build Model Context Protocol (MCP) servers in Go. This guide will help you create your first MCP server in just a few minutes. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add MCP-Go to your Go project: |
| 8 | + |
| 9 | +```bash |
| 10 | +go get github.com/mark3labs/mcp-go |
| 11 | +``` |
| 12 | + |
| 13 | +## Your First MCP Server |
| 14 | + |
| 15 | +Let's create a simple MCP server with a "hello world" tool: |
| 16 | + |
| 17 | +```go |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + |
| 25 | + "github.com/mark3labs/mcp-go/mcp" |
| 26 | + "github.com/mark3labs/mcp-go/server" |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + // Create a new MCP server |
| 31 | + s := server.NewMCPServer( |
| 32 | + "Demo 🚀", |
| 33 | + "1.0.0", |
| 34 | + server.WithToolCapabilities(false), |
| 35 | + ) |
| 36 | + |
| 37 | + // Add tool |
| 38 | + tool := mcp.NewTool("hello_world", |
| 39 | + mcp.WithDescription("Say hello to someone"), |
| 40 | + mcp.WithString("name", |
| 41 | + mcp.Required(), |
| 42 | + mcp.Description("Name of the person to greet"), |
| 43 | + ), |
| 44 | + ) |
| 45 | + |
| 46 | + // Add tool handler |
| 47 | + s.AddTool(tool, helloHandler) |
| 48 | + |
| 49 | + // Start the stdio server |
| 50 | + if err := server.ServeStdio(s); err != nil { |
| 51 | + fmt.Printf("Server error: %v\n", err) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func helloHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 56 | + name, ok := request.Params.Arguments["name"].(string) |
| 57 | + if !ok { |
| 58 | + return nil, errors.New("name must be a string") |
| 59 | + } |
| 60 | + |
| 61 | + return mcp.NewToolResultText(fmt.Sprintf("Hello, %s!", name)), nil |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Running Your Server |
| 66 | + |
| 67 | +1. Save the code above to a file (e.g., `main.go`) |
| 68 | +2. Run it with: |
| 69 | + ```bash |
| 70 | + go run main.go |
| 71 | + ``` |
| 72 | + |
| 73 | +Your MCP server is now running and ready to accept connections via stdio! |
| 74 | + |
| 75 | +## What's Next? |
| 76 | + |
| 77 | +Now that you have a basic server running, you can: |
| 78 | + |
| 79 | +- **Add more tools** - Create tools for calculations, file operations, API calls, etc. |
| 80 | +- **Add resources** - Expose data sources like files, databases, or APIs |
| 81 | +- **Add prompts** - Create reusable prompt templates for better LLM interactions |
| 82 | +- **Explore examples** - Check out the `examples/` directory for more complex use cases |
| 83 | + |
| 84 | +## Key Concepts |
| 85 | + |
| 86 | +### Tools |
| 87 | +Tools let LLMs take actions through your server. They're like functions that the LLM can call: |
| 88 | + |
| 89 | +```go |
| 90 | +calculatorTool := mcp.NewTool("calculate", |
| 91 | + mcp.WithDescription("Perform basic arithmetic operations"), |
| 92 | + mcp.WithString("operation", |
| 93 | + mcp.Required(), |
| 94 | + mcp.Enum("add", "subtract", "multiply", "divide"), |
| 95 | + ), |
| 96 | + mcp.WithNumber("x", mcp.Required()), |
| 97 | + mcp.WithNumber("y", mcp.Required()), |
| 98 | +) |
| 99 | +``` |
| 100 | + |
| 101 | +### Resources |
| 102 | +Resources expose data to LLMs. They can be static files or dynamic data: |
| 103 | + |
| 104 | +```go |
| 105 | +resource := mcp.NewResource( |
| 106 | + "docs://readme", |
| 107 | + "Project README", |
| 108 | + mcp.WithResourceDescription("The project's README file"), |
| 109 | + mcp.WithMIMEType("text/markdown"), |
| 110 | +) |
| 111 | +``` |
| 112 | + |
| 113 | +### Server Options |
| 114 | +Customize your server with various options: |
| 115 | + |
| 116 | +```go |
| 117 | +s := server.NewMCPServer( |
| 118 | + "My Server", |
| 119 | + "1.0.0", |
| 120 | + server.WithToolCapabilities(true), |
| 121 | + server.WithRecovery(), |
| 122 | + server.WithHooks(myHooks), |
| 123 | +) |
| 124 | +``` |
| 125 | + |
| 126 | +## Transport Options |
| 127 | + |
| 128 | +MCP-Go supports multiple transport methods: |
| 129 | + |
| 130 | +- **Stdio** (most common): `server.ServeStdio(s)` |
| 131 | +- **HTTP**: `server.ServeHTTP(s, ":8080")` |
| 132 | +- **Server-Sent Events**: `server.ServeSSE(s, ":8080")` |
| 133 | + |
| 134 | +## Need Help? |
| 135 | + |
| 136 | +- Check out the [examples](https://github.com/mark3labs/mcp-go/tree/main/examples) for more complex use cases |
| 137 | +- Join the discussion on [Discord](https://discord.gg/RqSS2NQVsY) |
| 138 | +- Read the full documentation in the [README](https://github.com/mark3labs/mcp-go/blob/main/README.md) |
0 commit comments