bun-mcp-server

bun-mcp-server

A lightweight HTTP-based MCP server built with Bun, enabling tool discovery and execution via JSON-RPC 2.0 over HTTP.

Category
访问服务器

README

bun-mcp-server

A lightweight HTTP-based Model Context Protocol (MCP) server built with Bun.

Motivation

This project explores the MCP lifecycle through a clean, modular implementation built with Bun. By separating HTTP routing from JSON-RPC 2.0 handling, the architecture aims to remain transport-agnostic, providing a clear reference for how the protocol functions under the hood.

[!Note] This is an educational, work-in-progress implementation and does not cover the full MCP specification.


本プロジェクトは、Bun を使用したクリーンかつモジュール化された実装を通じて、MCP(Model Context Protocol)のライフサイクルを探求するものです。HTTPルーティングとJSON-RPC 2.0の処理を分離することでトランスポート層に依存しない設計を実現し、プロトコルの内部動作を理解するためのリファレンスとして機能します。

[!Note] 本プロジェクトは学習・研究用であり、MCP仕様のすべてを実装しているわけではありません。

You can read the full MCP specification here.

Features

  • HTTP Transport — Serves MCP over a standard HTTP POST endpoint (/mcp), compatible with any MCP client that supports the HTTP transport.
  • JSON-RPC 2.0 — Fully compliant request/response framing with proper error codes (-32700, -32600, -32601, -32602, -32603).
  • Tool Registry — Pluggable tool system for easy registration and execution of tools.

Project Structure

bun-mcp-server/
├── src/
│   ├── index.js          # Entry point — starts the Bun HTTP server on port 3000
│   ├── lib/
│   │   └── utils.js      # Shared utilities (timestamps, CORS headers, logging, IP parsing)
│   ├── mcp/
│   │   ├── index.js      # Core MCP request handler (JSON-RPC dispatch)
│   │   └── tools.js      # Tool definitions and registration
│   └── routes/
│       └── index.js      # HTTP route definitions (/mcp, /.well-known/mcp, catch-all)
├── package.json
├── jsconfig.json
└── BRIEF.md              # Project brief / requirements

Prerequisites

  • Bun v1.3+ installed

Getting Started

Install dependencies

bun install

Start the server

bun run dev

The server starts on http://localhost:3000 with file-watching enabled for live reload during development.

API Endpoints

POST /mcp

The main MCP endpoint. Accepts JSON-RPC 2.0 requests and dispatches them to the appropriate handler.

Supported methods:

Method Description
initialize Handshake — returns server info, protocol version, and capabilities
tools/list Returns the list of available tools with their schemas
tools/call Executes a registered tool by name with the given arguments
notifications/initialized Acknowledges client initialization (returns 202)
notifications/roots/list_changed Acknowledges root change notifications (returns 202)

GET /.well-known/mcp

This endpoint serves as the MCP Server Card. It provides essential metadata about the server—such as its name, description, supported transport types, and the base URL for the MCP endpoint—allowing clients to identify and connect to the service automatically.

While this mechanism is not currently part of the official Model Context Protocol (MCP) specification, it addresses a critical gap: enabling HTTP-based MCP servers to be discoverable via standard web patterns and simplifying the process for users to add them to their environments.

The return value will be mapped to the client's MCP server config file:

{
  "servers": {
    "weather-server": {
      "type": "http",
      "url": "http://localhost/mcp"
    }
  }
}

* /*

Catch-all route that returns a 404 Not Found JSON response for any unmatched path.

Usage Examples

Initialize the server

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

List available tools

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

Call a tool

curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 3,
    "method": "tools/call",
    "params": {
      "name": "get_weather",
      "arguments": { "city": "Tokyo" }
    }
  }'

Discover the server

curl http://localhost:3000/.well-known/mcp

Live Testing

You can verify that the server is functioning correctly by connecting it to an MCP client, such as VS Code or AntiGravity.

VSCode

Follow these steps to configure and connect:

  1. Create or open .vscode/mcp.json in your project directory.
  2. Add the following entry:
{
  "servers": {
    "weather-server": {
      "url": "http://localhost:3000/mcp",
      "headers": {
        "Content-Type": "application/json"
      }
    }
  }
}

  1. In the VS Code Chat panel, click the gear icon to open the Agent Customizations dialog.
  2. Select MCP Servers from the sidebar. You should see weather-server listed under Workspace.
  3. Right-click weather-server and select Start Server from the context menu to initialize the connection.

AntiGravity

Follow these steps to configure and connect:

  1. Open Configuration: Navigate to Additional Options (...) and select MCP Servers.
  2. Manage Servers: Click Manage MCP Servers, then select View raw config to open the mcp_config.json file.
  3. Add Server: Add the following configuration:
{
  "mcpServers": {
    "weather-server": {
      "serverUrl": "http://localhost:3000/mcp",
      "headers": {
        "Content-Type": "application/json"
      }
    }
  }
}

  1. Connect: Save the file, return to the Manage MCP Servers menu, and click Refresh.

[!Note] Ensure your MCP server is already running locally before connecting to either one. Upon success, you will see connection activity in your server’s console logs.

Once connected, confirm that tool discovery and execution are working by sending a prompt through your IDE's chat interface:

"Let's test the MCP server. Get the weather for Tokyo today."

If successful, the chat should trigger the tool and return the relevant data from your server.

Available Tools

Tool Description Parameters
get_weather Get the current weather for a given city city (string, required)

The get_weather tool returns simulated data (random temperature, "Cloudy" condition) — it is included as a demo tool for testing the MCP lifecycle.

Adding New Tools

Register new tools in src/mcp/tools.js using the tool registry:

toolRegistry.register(
  "my_tool",
  {
    name: "my_tool",
    title: "My Tool",
    description: "Does something useful.",
    parameters: {
      type: "object",
      properties: {
        input: { type: "string", description: "Some input value" }
      },
      required: ["input"]
    }
  },
  async ({ input }) => {
    // Your tool logic here
    return { result: `Processed: ${input}` }
  }
)

The tool is automatically available via tools/list and executable via tools/call — no additional wiring needed.

Error Handling

The server returns standard JSON-RPC 2.0 error responses:

Code Meaning
-32700 Parse error — malformed JSON body
-32600 Invalid Request — missing jsonrpc or method
-32601 Method not found — unsupported MCP method
-32602 Invalid params — missing or malformed tool parameters
-32603 Internal error — unexpected server-side failure

License

MIT © supershaneski

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选