Build a Multi-Protocol MCP Server in TypeScript: stdio, StreamableHttp & SSE
This article introduces a TypeScript MCP‑Server framework that simultaneously supports stdio, StreamableHttp, and SSE protocols, explains its motivations, core features, directory layout, startup commands, production configuration, key code snippets, and showcases deployment results.
Project Overview
The MCP‑Server‑TS framework is a TypeScript‑based server that unifies three communication protocols—stdio, StreamableHttp (referred to as streamableHttpless), and SSE—into a single codebase, addressing repetitive development and steep learning curves when integrating AI services.
Key Features
All three modes (stdio, StreamableHttp, SSE) are supported out of the box, enabling one‑time development for multiple deployment scenarios.
Functionality is modularized; developers only need to implement business logic in designated files to create custom MCP services.
Environment variables allow configuration of domain, host, port, and base path, making the server production‑ready.
Mode switching is achieved by changing the startup command, with near‑zero code changes.
Built‑in logging module for monitoring service calls and startup events.
Additional deployment scripts support the Xingyun cloud platform.
Directory Structure
## 目录结构
- build: compiled files
- src
-- router: routes for StreamableHttp and SSE
--- index.ts: entry for StreamableHttp routes
--- mcp.ts: path config for StreamableHttp (process.env.MCP_BASE_PATH, default /mcp)
--- sse.ts: path config for SSE (same env variable)
-- tools: utility modules
--- index.ts: tool registration
--- mockFunc.ts: example mock implementation
-- cli.ts: command‑line parser
-- index.ts: main entry
-- server.ts: creates MCP service and registers tools
-- sse.ts: runs SSE mode
-- stdio.ts: runs stdio mode
-- streamableHttp.ts: runs StreamableHttp mode
- xingyun/bin: deployment scripts for the Xingyun platform
- build_xingyun.sh: helper script for deploymentRunning the Server
Local Startup
Start stdio (default): npm run start Start StreamableHttp (default port 3001): npm run start:http Start StreamableHttp on a custom port: npm run dev:http or npm run start -- -t http -p 3001 Start SSE (default port 3001):
npm run start:sseDeployment Startup
A shell script xingyun/bin/control.sh launches the StreamableHttp mode; to run SSE, replace the command with npm run start:sse.
start(){
npm run start:http
sleep 3
status
}Production Environment Configuration
# Bind to a specific internal IP
export MCP_HOST=192.168.1.100
export MCP_PORT=3001
# Optional internal domain
export MCP_DOMAIN=mcp-server.internal.com
# Optional base path (default /mcp)
export MCP_BASE_PATH=/api/mcpConfiguration priority:
Port: MCP_PORT (env) > --port (CLI) > default 3001
Domain: MCP_DOMAIN (env) > MCP_HOST (env) > default localhost
Example access URLs:
Basic: http://192.168.1.100:3001/sales With domain: http://mcp-server.internal.com/sales Custom base path:
http://192.168.1.100:3001/api/mcpKey Code Explanation
The package.json scripts define the build and start commands:
"scripts": {
"build": "tsc && chmod 755 build/src/index.js build/src/cli.js",
"start": "node ./build/src/cli.js",
"start:http": "node ./build/src/cli.js --transport http",
"start:sse": "node ./build/src/cli.js --transport sse",
"dev:http": "node ./build/src/cli.js --transport http --port 3002",
"stop": "pkill -f \"demo\" || true",
"restart": "npm run stop && npm run start:http",
"inspector": "npx @modelcontextprotocol/inspector"
}The index.ts file dispatches requests to the appropriate mode based on the --transport argument; the mode‑specific modules ( stdio.ts, streamableHttp.ts, sse.ts) contain the actual service logic and remain unchanged for most projects.
Utility registration occurs in tools/index.ts, where each tool is exported and imported by server.ts, which also creates the MCP service instance.
Result Showcase
1. stdio – Published as an npm package @jd/demo-mcp-server and successfully connected via Joycode.
2. StreamableHttp – Connected and running through Joycode.
3. SSE – Integrated into the Autobots business, providing permission‑intercepted MCP services.
JD Cloud Developers
JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
