example-mcp-server-stdio
example-mcp-server-stdio
README
Calculator Learning Demo - STDIO Transport
<div align="center">
</div>
🎯 Overview
This repository demonstrates a learning-edition MCP calculator server using STDIO transport. It showcases the Model Context Protocol (MCP) SDK implementation with standard tools/list and tools/call methods, communicating via stdin and stdout using JSON-RPC messages.
This transport is the most performant and secure option for local inter-process communication (IPC).
Key Characteristics
- Transport Layer: Direct
stdin/stdoutpipes between parent (client) and child (server) processes. - State Model: Ephemeral, in-process memory. All state (e.g., calculation history) is lost when the process exits.
- Latency: The lowest possible latency (microseconds), as it avoids all network stack overhead.
- Security: Extremely high due to OS-level process isolation. There is no network attack surface.
- Use Case: Ideal for high-performance, secure local tooling, such as command-line interfaces (CLIs), IDE plugins, and build-system integrations.
📊 Transport Comparison
This table compares the four primary MCP transport mechanisms demonstrated in the learning series. The implementation in this repository is highlighted.
| Dimension | STDIO | SSE (Legacy) | Streamable HTTP (Stateful) | Streamable HTTP (Stateless) |
|---|---|---|---|---|
| Transport Layer | Local Pipes (stdin/stdout) |
2 × HTTP endpoints (GET+POST) |
Single HTTP endpoint /mcp |
Single HTTP endpoint /mcp |
| Bidirectional Stream | ✅ Yes (full duplex) | ⚠️ Server→Client only | ✅ Yes (server push + client stream) | ✅ Yes (within each request) |
| State Management | Ephemeral (Process Memory) | Ephemeral (Session Memory) | Persistent (Session State) | ❌ None (Stateless) |
| Resumability | ❌ None | ❌ None | ✅ Yes (Last-Event-Id) |
❌ None (by design) |
| Scalability | ⚠️ Single Process | ✅ Multi-Client | ✅ Horizontal (Sticky Sessions) | ♾️ Infinite (Serverless) |
| Security | 🔒 Process Isolation | 🌐 Network Exposed | 🌐 Network Exposed | 🌐 Network Exposed |
| Ideal Use Case | ✅ CLI Tools, IDE Plugins | Legacy Web Apps | Enterprise APIs, Workflows | Serverless, Edge Functions |
📐 Architecture and Flow
The STDIO transport architecture is based on a parent-child process model. A client application spawns the MCP server as a child process and communicates with it by writing to the child's stdin and reading from its stdout.
sequenceDiagram
participant Client (Parent Process)
participant Server (Child Process)
Client->>Server: Writes JSON-RPC request to stdin stream
Server-->>Client: Writes JSON-RPC response to stdout stream
Note over Client,Server: Communication is full-duplex, concurrent, and newline-delimited.
MCP SDK Implementation
This repository contains a single, production-ready MCP server implementation:
dist/server.js- Built with the official@modelcontextprotocol/sdk- Uses standard MCP methods:
tools/list,tools/call,resources/list,prompts/list - High-level abstractions with
server.registerTool()andserver.registerResource() - Compatible with all MCP clients and registries (including Smithery)
✨ Feature Compliance
This server implements the complete MCP Latest Standard feature set for the learning edition.
| Name | Status | Implementation |
|---|---|---|
calculate |
Core ✅ | Basic arithmetic with optional streaming progress. |
batch_calculate |
Extended ✅ | Processes multiple calculations in a single request. |
advanced_calculate |
Extended ✅ | Factorial, logarithm, and combinatorics operations. |
demo_progress |
Extended ✅ | Demonstrates progress notifications over the stdout stream. |
explain-calculation |
Core ✅ | Returns a Markdown explanation of a calculation. |
generate-problems |
Core ✅ | Returns Markdown-formatted practice problems. |
calculator-tutor |
Core ✅ | Returns Markdown-formatted tutoring content. |
solve_math_problem |
Extended ✅ | Solves a math problem, may elicit input. |
explain_formula |
Extended ✅ | Provides an interactive formula explanation. |
calculator_assistant |
Extended ✅ | Offers interactive calculator assistance. |
calculator://constants |
Core ✅ | Resource for mathematical constants (π, e, φ, etc.). |
calculator://history/{id} |
Extended ✅ | Resource for the last 50 calculation results stored in memory. |
calculator://stats |
Extended ✅ | Resource for server uptime and request statistics. |
formulas://library |
Extended ✅ | Resource for a collection of mathematical formulas. |
🚀 Getting Started
Prerequisites
- Node.js (v18.x or higher)
- npm or yarn
Installation
# Clone the repository
git clone https://github.com/modelcontextprotocol/mcp-server-examples.git
cd mcp-server-examples/stdio
# Install dependencies
npm install
# Build the project
npm run build
Running the Server
The server is designed to be spawned by a client. You can run it directly to send it commands interactively.
# Run the MCP server
npm start
# Run directly with Node.js
node dist/server.js --stdio
# Run in development mode
npm run dev
Testing with MCP Inspector
Interact with the SDK-based server using the official MCP Inspector CLI. This command spawns the server and pipes its I/O to the inspector.
npx @modelcontextprotocol/inspector --cli "node dist/server.js --stdio"
📋 API Usage Examples
All requests use standard MCP protocol with JSON-RPC messages.
Standard MCP Protocol
The server implements the standard MCP SDK protocol:
# List available tools
→ {"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}
# Call a tool
→ {"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"calculate","arguments":{"a":7,"b":6,"op":"multiply"}}}
# Response
{"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"7 × 6 = 42"}]}}
Progress Demonstration
Progress notifications are sent as standard JSON-RPC notifications (no id field) over stdout.
# Request
→ {"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"demo_progress","arguments":{}}}
# Response Stream
{"jsonrpc":"2.0","method":"progress","params":{"progressToken":"progress_3","progress":20,"total":100}}
{"jsonrpc":"2.0","method":"progress","params":{"progressToken":"progress_3","progress":40,"total":100}}
{"jsonrpc":"2.0","method":"progress","params":{"progressToken":"progress_3","progress":60,"total":100}}
{"jsonrpc":"2.0","method":"progress","params":{"progressToken":"progress_3","progress":80,"total":100}}
{"jsonrpc":"2.0","method":"progress","params":{"progressToken":"progress_3","progress":100,"total":100}}
{"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"Progress demonstration completed"}]}}
🧠 State Management Model
Principle: State is ephemeral and strictly scoped to the lifetime of the server process.
- Mechanism: All state is held in standard JavaScript variables and
Mapobjects within the Node.js process. - Stored Data:
- Calculation History: A
Mapstores the last 50 calculation results as a ring buffer. - Server Statistics: Variables track the process start time and total request count.
- In-flight Requests: The MCP SDK maintains a
Mapto track concurrent requests and route responses correctly.
- Calculation History: A
- Lifecycle: When the process exits for any reason, all in-memory state is irrevocably lost. Each new process starts with a clean slate. This is a fundamental and intentional design choice for this transport.
🛡️ Security Model
The STDIO transport provides the most secure environment of all MCP transports by leveraging operating system primitives.
- Process Isolation: The server runs in a separate memory space from the client, preventing any direct memory access or interference. The OS enforces this boundary.
- No Network Exposure: Communication is entirely via local IPC pipes. There are no open ports, making network-based attacks (e.g., CSRF, MitM, remote exploits) impossible.
- Input Validation: All incoming request parameters are rigorously validated by Zod schemas (defined in
src/types.ts) to ensure type safety and prevent injection-style attacks. - Resource Limiting: The server enforces hard limits on batch sizes (
maxBatchSize: 100) and history storage (maxHistorySize: 50) to prevent resource exhaustion attacks. - Exit Code Signaling: The server uses standard Unix exit codes to signal its termination status to the parent process (e.g.,
0for success,65for data errors,70for software errors), allowing the client to react appropriately.
🧪 Testing
This project includes a test suite that spawns the server as a child process to validate its I/O behavior.
# Run all tests defined in jest.config.js
npm test
# Run tests and generate a code coverage report
npm run test:coverage
# Run tests in watch mode for interactive development
npm run test:watch
📚 Official Resources
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。