DEV Community

Chris Battarbee
Chris Battarbee

Posted on • Edited on

8 1 1 2

Write tools for LLMs with go - mcp-golang

We just open-sourced mcp-golang!

A few weeks ago, Anthropic released Model Context Protocol - a protocol to allow LLMs to use tools and interact with the world.

The base sdks written by Anthropic were for Typescript and python, so if you wanted to use go, you were out of luck.

With mcp-golang you can now write tool servers in just a few lines of code in go.

The server below allows an LLM to understand the weather in any location.

package main

import (
    "fmt"
    mcp_golang "github.com/metoro-io/mcp-golang"
    "github.com/metoro-io/mcp-golang/transport/stdio"
    "io"
    "net/http"
)

type WeatherArguments struct {
    Longitude float64 `json:"longitude" jsonschema:"required,description=The longitude of the location to get the weather for"`
    Latitude  float64 `json:"latitude" jsonschema:"required,description=The latitude of the location to get the weather for"`
}

// This is explained in the docs at https://mcpgolang.com/tools
func main() {
    done := make(chan struct{})
    server := mcp_golang.NewServer(stdio.NewStdioServerTransport())
    err := server.RegisterTool("get_weather", "Get the weather forecast for temperature, wind speed and relative humidity", func(arguments WeatherArguments) (*mcp_golang.ToolResponse, error) {
        url := fmt.Sprintf("https://api.open-meteo.com/v1/forecast?latitude=%f&longitude=%f&current=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m", arguments.Latitude, arguments.Longitude)
        resp, err := http.Get(url)
        if err != nil {
            return nil, err
        }
        defer resp.Body.Close()
        output, err := io.ReadAll(resp.Body)
        if err != nil {
            return nil, err
        }
        return mcp_golang.NewToolReponse(mcp_golang.NewTextContent(string(output))), nil
    })
    err = server.Serve()
    if err != nil {
        panic(err)
    }
    <-done
}
Enter fullscreen mode Exit fullscreen mode

Check out a demo at https://www.youtube.com/watch?v=xaMkMl_R-0A! And the github repo at https://github.com/metoro-io/mcp-golang

Happy coding!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more