Cover image
Try Now
2025-01-03

3 years

Works with Finder

0

Github Watches

2

Github Forks

4

Github Stars

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)

	return mcp.NewToolResultText(content), nil
}

Explanations

It's really very simple. My server will provide only one tool. The code implements a server that exposes a web "fetching" tool based on curl using the Model Control Protocol.

  • First, I create an MCP server named mcp-curl version 1.0.0 that works with standard input/output (stdio)
  • Then I define a tool named use_curl that takes a required url parameter
  • Finally, I add the handler for this tool that executes the curl command with the -s option to retrieve the webpage content
  • Of course, I handle errors and return the webpage content as text

Packaging the MCP Server with Docker

To make my life easier, I'll use a Dockerfile to build a Docker image containing my MCP server. This way, I can more easily deploy it on different platforms or make it available to anyone who wants to use it.

Dockerfile:

FROM golang:1.23.4-alpine AS builder
WORKDIR /app
COPY go.mod .
COPY main.go .

RUN <<EOF
go mod tidy 
go build
EOF

FROM curlimages/curl:8.6.0
WORKDIR /app
COPY --from=builder /app/mcp-curl .
ENTRYPOINT ["./mcp-curl"]

My Dockerfile has two parts:

  • The first part builds my MCP server in Go
  • The second part uses the curlimages/curl:8.6.0 image that contains curl and copies my mcp-curl server into the image (so mcp-curl is the executable that calls curl)

To build the Docker image, I run the following command:

docker build -t mcp-curl .

And now let's see how to use our new MCP server with mcphost:

Using the MCP Server with mcphost

First, we need to create a configuration file mcp.json for mcphost:

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

Then we can use mcphost to use our MCP server and an LLM with Ollama, like this:

mcphost --config ./mcp.json --model ollama:qwen2.5-coder:14b

The MCP server is recognized by mcphost: mcp

You can request the list of available tools with the /tools command: mcp

Now you can request the content of a webpage and analyze its content (in my example I retrieve Go code from GitHub): mcp

Wait a little bit: mcp

And here's the webpage content: mcp

Conclusion

You can see that with just a few lines, it becomes really easy to give "superpowers" to your LLMs. In a future blog post, I'll show you how to create a generative AI application with Ollama and an MCP client in Go to interact with our MCP server.

相关推荐

  • NiKole Maxwell
  • I craft unique cereal names, stories, and ridiculously cute Cereal Baby images.

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Khalid kalib
  • Write professional emails

  • https://tovuti.be
  • Oede knorrepot die vasthoudt an de goeie ouwe tied van 't boerenleven

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • ANGEL LEON
  • A world class elite tech co-founder entrepreneur, expert in software development, entrepreneurship, marketing, coaching style leadership and aligned with ambition for excellence, global market penetration and worldy perspectives.

  • Gil kaminski
  • Make sure you are post-ready before you post on social media

  • INFOLAB OPERATIONS 2
  • A medical specialist offering assistance grounded in clinical guidelines. Disclaimer: This is intended for research and is NOT safe for clinical use!

  • apappascs
  • Entdecken Sie die umfassendste und aktuellste Sammlung von MCP-Servern auf dem Markt. Dieses Repository dient als zentraler Hub und bietet einen umfangreichen Katalog von Open-Source- und Proprietary MCP-Servern mit Funktionen, Dokumentationslinks und Mitwirkenden.

  • OffchainLabs
  • GO -Umsetzung des Ethereum -Beweises des Anteils

  • huahuayu
  • Ein einheitliches API-Gateway zur Integration mehrerer Ethercan-ähnlicher Blockchain-Explorer-APIs mit Modellkontextprotokoll (MCP) für AI-Assistenten.

  • deemkeen
  • Steuern Sie Ihren MBOT2 mit einer Power Combo: MQTT+MCP+LLM

  • zhaoyunxing92
  • 本项目是一个钉钉 MCP (Message Connector Protocol )服务 , 提供了与钉钉企业应用交互的 api 接口。项目基于 Go 语言开发 , 支持员工信息查询和消息发送等功能。

  • pontusab
  • Die Cursor & Windsurf -Community finden Regeln und MCPs

    Reviews

    3 (1)
    Avatar
    user_desaWJcd
    2025-04-16

    As a dedicated MCP application user, I found "05-make-your-mcp-server" by ollama-tlms-golang incredibly useful. The guide on GitHub is precise and user-friendly, making the setup process straightforward even for beginners. It's a must-have resource for anyone looking to build their own MCP server effortlessly. Highly recommend checking it out!