OT

ollama-tlms-golang/05-make-your-mcp-server

Developer tools

このページは AI により自動翻訳されています。技術的な詳細は原文をご確認ください。 原文を見る

11 stars 5 forks 品質 52 トレンド 18MIT

Today we'll look at how to create an MCP server in Go and serve it with Docker.

概要

Today we'll look at how to create an MCP server in Go and serve it with Docker. Prerequisites: having read Understanding the Model Context Protocol (MCP) To write an MCP server, there are several official SDKs: - TypeScript SDK - Python SDK - Kotlin SDK In recent years, I've developed an appetite for Go, so I looked around to see if there was a Go implementation. On this page https://github.com/punkpeye/awesome-mcp-servers?tab=readme-ov-file#frameworks, I found several, notably https://github.com/mark3labs/mcp-go, by the creator of mcphost (which I discuss and use in the previous blog post). The advantage of the mcp-go project is that it's simple and also provides tools to develop an MCP client, which will be very useful for a future blog post. I won't detail or implement all the possibilities of an MCP server here. I'll just implement the essentials: - Provide a list of tools for the LLM - Execute these tools when the LLM invokes them

README

以下は原文 README の抜粋です。原文を見る。

Creating an MCP Server in Go and Serving it with Docker

Introduction

Today we’ll look at how to create an MCP server in Go and serve it with Docker.

Prerequisites: having read Understanding the Model Context Protocol (MCP)

To write an MCP server, there are several official SDKs:

In recent years, I’ve developed an appetite for Go, so I looked around to see if there was a Go implementation. On this page https://github.com/punkpeye/awesome-mcp-servers?tab=readme-ov-file#frameworks, I found several, notably https://github.com/mark3labs/mcp-go, by the creator of mcphost (which I discuss and use in the previous blog post).

The advantage of the mcp-go project is that it’s simple and also provides tools to develop an MCP client, which will be very useful for a future blog post.

Creating an MCP Server

I won’t detail or implement all the possibilities of an MCP server here. I’ll just implement the essentials:

  • Provide a list of tools for the LLM
  • Execute these tools when the LLM invokes them

When I use an LLM, there’s one thing I’d like to be able to do: give it a website link so it can find information there, make a summary for me, etc.

So I created an MCP server that calls the curl utility with a URL parameter and returns its content. But let’s look at the source code:

go.mod:

module mcp-curl

go 1.23.4

require github.com/mark3labs/mcp-go v0.8.2
require github.com/google/uuid v1.6.0

main.go:

package main

import (
	"context"
	"fmt"
	"os/exec"

	"github.com/mark3labs/mcp-go/mcp"
	"github.com/mark3labs/mcp-go/server"
)

func main() {
	// Create MCP server
	s := server.NewMCPServer(
		"mcp-curl",
		"1.0.0",
	)

	// Add a tool
	tool := mcp.NewTool("use_curl",
		mcp.WithDescription("fetch this webpage"),
		mcp.WithString("url",
			mcp.Required(),
			mcp.Description("url of the webpage to fetch"),
		),
	)

	// Add a tool handler
	s.AddTool(tool, curlHandler)

	fmt.Println("🚀 Server started")
	// Start the stdio server
	if err := server.ServeStdio(s); err != nil {
		fmt.Printf("😡 Server error: %v\n", err)
	}
	fmt.Println("👋 Server stopped")
}

func curlHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {

	url, ok := request.Params.Arguments["url"].(string)
	if !ok {
		return mcp.NewToolResultError("url must be a string"), nil
	}
	cmd := exec.Command("curl", "-s", url)
	output, err := cmd.Output()
	if err != nil {
		return mcp.NewToolResultError(err.Error()), nil
	}
	content := string(output)
GitHub で README 全文を見る

インストール

docker run --rm -i mcp-curl
MCP 初心者?mcpskill.org のガイドから始めましょう →

設定

{ "mcpServers": { "mcp-curl-with-docker" :{ "command": "docker", "args": [ "run", "--rm", "-i", "mcp-curl" ] } } }