Skip to Content
Build an MCP server

Build an MCP server

An MCP server is a thin layer over the API you already have. Each tool is one function: a name, a description, an input schema and a handler that calls your API.

Go

main.go
package main import ( "context" "net/http" "github.com/modelcontextprotocol/go-sdk/mcp" ) type SendSMSArgs struct { To string `json:"to" jsonschema:"phone number in +255 format"` Message string `json:"message" jsonschema:"text, up to 918 characters"` } func main() { server := mcp.NewServer(&mcp.Implementation{Name: "sendafrica-mcp", Version: "1.0.0"}, nil) mcp.AddTool(server, &mcp.Tool{Name: "send_sms", Description: "Send an SMS to a Tanzanian number."}, func(ctx context.Context, req *mcp.CallToolRequest, in SendSMSArgs) (*mcp.CallToolResult, any, error) { // Call your existing REST API here, with the key the agent sent. key := req.Extra.Header.Get("X-Api-Key") id, err := sendViaREST(ctx, key, in.To, in.Message) if err != nil { return nil, nil, err } return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: "Sent. Message ID " + id}}}, nil, nil }) handler := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server { return server }, nil) http.Handle("/mcp", handler) http.ListenAndServe(":8080", nil) }

TypeScript

server.ts
import express from "express"; import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; import { z } from "zod"; const app = express(); app.use(express.json()); app.post("/mcp", async (req, res) => { const server = new McpServer({ name: "ziadapos-mcp", version: "1.0.0" }); server.tool("check_stock", "Check stock for a product in each branch.", { product: z.string().describe("Product name or SKU") }, async ({ product }) => { const r = await fetch(`https://api.ziadapos.com/stock?q=${encodeURIComponent(product)}`, { headers: { "X-Api-Key": String(req.headers["x-api-key"] ?? "") }, }); return { content: [{ type: "text", text: await r.text() }] }; }); const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined }); res.on("close", () => transport.close()); await server.connect(transport); await transport.handleRequest(req, res, req.body); }); app.listen(8080);

Writing good tools

  • Name tools with a verb: send_sms, check_stock. The marketplace search covers tool names and descriptions.
  • Describe each input. Agents read the descriptions to fill in arguments, and the listing shows them to people.
  • Return short, plain answers. “Sent. Message ID 8841” is easier for an agent than a raw JSON dump.
  • Keep reads and writes separate, so people can trust a read-only tool.
Last updated on