DEV Community

Cover image for Build a MCP server using Go to connect AI agents with databases
Abhishek Gupta
Abhishek Gupta

Posted on

4 1 1 1 1

Build a MCP server using Go to connect AI agents with databases

Like many of you, I have been playing around with Model Context Protocol (MCP). To dive in, I built a sample MCP server implementation for Azure Cosmos DB with Go. It uses the Go SDK, and mcp-go as the MCP Go implementation.

This MCP server exposes the following tools for interacting with Azure Cosmos DB:

  • List Databases: Retrieve a list of all databases in a Cosmos DB account.
  • List Containers: Retrieve a list of all containers in a specific database.
  • Read Container Metadata: Fetch metadata or configuration details of a specific container.
  • Create Container: Create a new container in a specified database with a defined partition key.
  • Add Item to Container: Add a new item to a specified container in a database.
  • Read Item: Read a specific item from a container using its ID and partition key.
  • Execute Query: Execute a SQL query on a Cosmos DB container with optional partition key scoping.

Here is a demo (recommend watching at 2x speed 😉) using VS Code Insiders in Agent mode:

How to run

git clone https://github.com/abhirockzz/mcp_cosmosdb_go
cd mcp_cosmosdb_go

go build -o mcp_azure_cosmosdb main.go
Enter fullscreen mode Exit fullscreen mode

Configure the MCP server:

mkdir -p .vscode

# Define the content for mcp.json
MCP_JSON_CONTENT=$(cat <<EOF
{
  "servers": {
    "CosmosDB Golang MCP": {
      "type": "stdio",
      "command": "$(pwd)/mcp_azure_cosmosdb"
    }
  }
}
EOF
)

# Write the content to mcp.json
echo "$MCP_JSON_CONTENT" > .vscode/mcp.json
Enter fullscreen mode Exit fullscreen mode

Azure Cosmos DB RBAC permissions and authentication

  • The user principal you will be using should have permissions (control and data plane) to execute CRUD operations on database, container, and items.
  • Authentication

    • Local credentials - Just login locally using Azure CLI (az login) and the MCP server will use the DefaultAzureCredential implementation automatically.
    • Or, you can set the COSMOSDB_ACCOUNT_KEY environment variable in the MCP server configuration:
  {
    "servers": {
      "CosmosDB Golang MCP": {
        "type": "stdio",
        "command": "/Users/demo/mcp_azure_cosmosdb",
        "env": {
          "COSMOSDB_ACCOUNT_KEY": "enter the key"
        }
      }
    }
  }
Enter fullscreen mode Exit fullscreen mode

You are good to go! Now spin up VS Code Insiders in Agent Mode, or any other MCP tool (like Claude Desktop) and try this out!

Local dev/testing

MCP inspector

Start with MCP inspector - npx @modelcontextprotocol/inspector ./mcp_azure_cosmosdb

MCP, MCP everywhere!

mcp all the things

While this is not an "official" Cosmos DB MCP server, I really wanted to try MCP with Go and also demonstrate the Go SDK for Azure Cosmos DB. Win win 🙌 After all, why should Pythonistas have all the fun 🤷‍♂️ Huge props to the creator of the mcp-go project, Ed Zynda! MCP (at the moment) does not have an "official" Go SDK (yeah I know, shocking right!). But there are discussions going on and I encourage all Gophers to chime in!

MCP is touted as the next big thing in agentic app landscape. Let's see how that goes. In the meanwhile, keep building stuff and trying out new things. Try out the MCP server and let me know how it goes!

Top comments (0)