mcp_demo

beiyu/mcp_demo

3.2

If you are the rightful owner of mcp_demo and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

This project demonstrates the implementation of Model Context Protocol (MCP) servers using multiple transport protocols in Go, focusing on educational purposes.

MCP Demo - Multi-Transport Protocol Support

This project demonstrates how to implement Model Context Protocol (MCP) servers with multiple transport protocols using pure Go. The primary purpose is to understand MCP principles and interaction details by implementing the protocol from scratch without any external frameworks.

๐ŸŽฏ Project Purpose

This is a learning-oriented implementation designed to:

  • Understand MCP Protocol: Learn the core concepts and message flows of Model Context Protocol
  • Master Protocol Details: Implement JSON-RPC 2.0 and MCP specifications from scratch
  • Explore Transport Mechanisms: Compare different transport protocols (STDIO, HTTP, SSE)
  • Pure Go Implementation: Build without external dependencies to understand every detail

Note: This is an educational project focused on protocol understanding rather than production use.

๐Ÿ“ Project Structure

mcp_demo/
โ”œโ”€โ”€ cmd/                    # Different transport protocol entry points
โ”‚   โ”œโ”€โ”€ stdio/             # Standard Input/Output implementation
โ”‚   โ”œโ”€โ”€ http/              # HTTP transport implementation
โ”‚   โ””โ”€โ”€ sse/               # Server-Sent Events implementation
โ”œโ”€โ”€ internal/mcp/          # Custom MCP protocol implementation
โ”‚   โ”œโ”€โ”€ types.go           # MCP protocol type definitions
โ”‚   โ””โ”€โ”€ server.go          # MCP server implementation
โ”œโ”€โ”€ bin/                   # Compiled output directory
โ”œโ”€โ”€ Makefile              # Build system
โ”œโ”€โ”€ README.md             # English documentation
โ”œโ”€โ”€ README-CN.md          # Chinese documentation
โ””โ”€โ”€ go.mod                # Go module definition

๐Ÿš€ Supported Transport Protocols

1. STDIO (Standard Input/Output)

  • Purpose: Local MCP servers, command-line interface communication
  • Features: Simplest implementation, suitable for local tool integration
  • Port: None (uses stdin/stdout)

2. HTTP (Simple HTTP JSON-RPC)

  • Purpose: Basic remote access, RESTful API style
  • Features: Simple request-response pattern, easy to test and debug
  • Port: 8080

3. SSE (Server-Sent Events)

  • Purpose: Real-time bidirectional communication, server push support
  • Features: HTTP-based streaming communication, persistent connections
  • Port: 8081

๐Ÿ› ๏ธ Quick Start

Build All Versions

make all

Build Specific Versions

# Build STDIO version
make stdio

# Build HTTP version
make http

# Build SSE version
make sse

Run Servers

# Run STDIO server
make run-stdio

# Run HTTP server
make run-http

# Run SSE server
make run-sse

๐Ÿงช Testing Guide

STDIO Testing

# Start server
./bin/mcp-stdio

# Send initialization message (via stdin)
{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}},"id":1}

# Send calculator request
{"jsonrpc":"2.0","method":"tools/call","params":{"name":"calculate","arguments":{"operation":"add","x":5,"y":3}},"id":2}

HTTP Testing

# Start server
./bin/mcp-http

# Test initialization
curl -X POST http://localhost:8080 \
  -H 'Content-Type: application/json' \
  -d '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}},"id":1}'

# Test tool list
curl -X POST http://localhost:8080 \
  -H 'Content-Type: application/json' \
  -d '{"jsonrpc":"2.0","method":"tools/list","id":2}'

# Test calculator tool
curl -X POST http://localhost:8080 \
  -H 'Content-Type: application/json' \
  -d '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"calculate","arguments":{"operation":"add","x":5,"y":3}},"id":3}'

SSE Testing

# Start server
./bin/mcp-sse

# Open test page in browser
open http://localhost:8081

# Or test SSE connection with curl
curl -N -H "Accept: text/event-stream" http://localhost:8081/sse

๐Ÿ“– MCP Protocol Overview

Model Context Protocol (MCP) is an open protocol for seamless integration between LLM applications and external data sources and tools.

Core Concepts

  • JSON-RPC 2.0: All messages follow the JSON-RPC 2.0 specification
  • Tools: Functions for AI models to execute
  • Resources: Context and data for users or AI models to use
  • Prompts: Templated messages and workflows

Transport Mechanism Comparison

FeatureSTDIOHTTPSSE
ComplexityLowMediumHigh
Real-timeHighLowHigh
Remote AccessNoYesYes
BidirectionalYesNoYes
Debug ConvenienceLowHighMedium
Production UseLocal toolsAPI servicesReal-time apps

๐Ÿ”ง Development Guide

Adding New Tools

  1. Define new tools in the appropriate cmd/*/main.go files
  2. Use mcp.NewTool() to create tool definitions
  3. Use server.AddTool() to register tool handlers

Extending Protocol Support

  • Create new transport implementations in the cmd/ directory
  • Update Makefile to add new build targets
  • Update this README documentation

๐Ÿ’ก Learning Highlights

This implementation helps you understand:

  • JSON-RPC 2.0 Protocol: How to implement request/response and notification patterns
  • MCP Message Flow: Initialization, capability negotiation, tool calling
  • Transport Abstractions: How different protocols can carry the same messages
  • Go Concurrency: Handling multiple connections in SSE implementation
  • Protocol Design: Clean separation between transport and application logic

๐Ÿ› ๏ธ Tech Stack

  • Go: 1.21+ (pure standard library)
  • Protocol Version: MCP 2024-11-05
  • Dependencies: None (educational pure implementation)

๐Ÿ“ License

MIT License

๐Ÿ“š References