Token导航 LogoToken导航TokenDH.com
bare MCP logo
运维云端未说明官方级别未说明来源级核验

bare MCP

MCP Server

一个最小化、通用的模型上下文协议(MCP)实现,支持工具注册、资源管理和多种传输协议。

工具数

4

提示词数

0

GitHub Stars

1

资源数

0
资源管理WebSocketJavaScriptClaudeAI工具集成Claude DesktopClaudeCursor

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

CameronTofer

提供方

CameronTofer

最后核验

2026/5/17 20:20

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

MCP服务器库

模型上下文协议 (MCP)。

适用于 Node.js裸运行时 (梨/Holepunch)。

特性

  • 工具 --注册AI客户端可以调用的函数
  • 资源 --公开客户端可以读取的数据(静态或动态)
  • 资源模板 --带参数的URI模式(user://{id})
  • 注释 --工具和内容的元数据提示(MCP 2025-11-25)
  • 通知 --向连接的客户端推送更新
  • 订阅 --客户端可以订阅资源更改
  • 多个传输 --HTTP、websocket、SSE、Stdio

快速开始

import { createMCPServer } from 'bare-mcp'
import { createHttpTransport } from 'bare-mcp/http'

// Create server
const mcp = createMCPServer({
  name: 'my-server',
  version: '1.0.0'
})

// Register a tool
mcp.addTool({
  name: 'greet',
  description: 'Say hello to someone',
  inputSchema: {
    type: 'object',
    properties: {
      name: { type: 'string', description: 'Name to greet' }
    },
    required: ['name']
  },
  execute: async ({ name }) => `Hello, ${name}!`
})

// Start HTTP server (works on both Node.js and Bare)
await createHttpTransport(mcp, { port: 3000 })

工具

工具是AI客户端可以调用的功能。

mcp.addTool({
  name: 'calculate',
  description: 'Perform arithmetic',
  inputSchema: {
    type: 'object',
    properties: {
      a: { type: 'number' },
      b: { type: 'number' },
      op: { type: 'string', enum: ['add', 'subtract', 'multiply', 'divide'] }
    },
    required: ['a', 'b', 'op']
  },
  execute: async ({ a, b, op }) => {
    const ops = { add: a + b, subtract: a - b, multiply: a * b, divide: a / b }
    return JSON.stringify({ result: ops[op] })
  }
})

// Register multiple tools
mcp.addTools([tool1, tool2, tool3])

工具注释

工具可以包括描述其行为的注释:

mcp.addTool({
  name: 'search',
  description: 'Search the web',
  inputSchema: {
    type: 'object',
    properties: { query: { type: 'string' } },
    required: ['query']
  },
  execute: async ({ query }) => `Results for: ${query}`,
  annotations: {
    title: 'Web Search',        // Human-readable title
    readOnlyHint: true,         // Doesn't modify environment (default: false)
    openWorldHint: true         // Interacts with external systems (default: true)
  }
})

// Destructive tool example
mcp.addTool({
  name: 'delete_file',
  description: 'Delete a file',
  inputSchema: {
    type: 'object',
    properties: { path: { type: 'string' } },
    required: ['path']
  },
  execute: async ({ path }) => { /* ... */ },
  annotations: {
    title: 'Delete File',
    readOnlyHint: false,        // Modifies environment
    destructiveHint: true,      // May destroy data (default: true)
    idempotentHint: true,       // Repeated calls have same effect (default: false)
    openWorldHint: false        // Only affects local system
  }
})
注释默认值说明
title--人类可读的显示名称
readOnlyHintfalse如果为真,则工具不会修改其环境
destructiveHinttrue如果为真,工具可能会销毁数据(仅当readOnlyHint=false时)
idempotentHintfalse如果为true,重复调用没有额外效果(仅当readOnlyHint=false时)
openWorldHinttrue如果为真,则与外部系统交互

带注释的工具结果

工具可以返回带有注释的丰富内容:

mcp.addTool({
  name: 'analyze',
  execute: async () => [{
    type: 'text',
    text: 'Analysis results...',
    annotations: {
      audience: ['user'],           // Who content is for: 'user', 'assistant', or both
      priority: 0.9                  // Importance: 0.0 (optional) to 1.0 (required)
    }
  }]
})

// Return error with content
mcp.addTool({
  name: 'fetch',
  execute: async () => ({
    content: [{ type: 'text', text: 'Connection timeout' }],
    isError: true
  })
})

错误处理

工具可以投掷 MCPError 带有特定错误代码:

import { MCPError, ErrorCode } from 'bare-mcp'

mcp.addTool({
  name: 'get_user',
  inputSchema: {
    type: 'object',
    properties: { id: { type: 'string' } },
    required: ['id']
  },
  execute: async ({ id }) => {
    const user = await db.findUser(id)
    if (!user) {
      throw new MCPError(
        ErrorCode.INVALID_PARAMS,
        `User not found: ${id}`,
        { userId: id }  // Optional data field
      )
    }
    return JSON.stringify(user)
  }
})

// Custom error codes (use values > -32000)
mcp.addTool({
  name: 'rate_limited_api',
  execute: async () => {
    throw new MCPError(-32001, 'Rate limit exceeded', { retryAfter: 60 })
  }
})

标准错误代码:

代码名称描述
-32700PARSE_ERRORJSON无效
-32600INVALID_REQUEST不是有效的JSON-RPC请求
-32601METHOD_NOT_FOUND方法不存在
-32602INVALID_PARAMS无效参数(验证,缺少参数)
-32603INTERNAL_ERROR内部服务器错误
-32002RESOURCE_NOT_FOUND未找到资源

错误响应遵循JSON-RPC 2.0规范:

{
  "jsonrpc": "2.0",
  "id": 1,
  "error": {
    "code": -32602,
    "message": "User not found: abc123",
    "data": { "userId": "abc123" }
  }
}

资源

资源公开了客户端可以读取的数据。

静态资源

mcp.addResource({
  uri: 'config://app',
  name: 'App Configuration',
  description: 'Application settings',
  mimeType: 'application/json',
  text: JSON.stringify({ theme: 'dark', version: '1.0' })
})

动态资源

mcp.addResource({
  uri: 'stats://live',
  name: 'Live Statistics',
  mimeType: 'application/json',
  read: async () => JSON.stringify({
    uptime: process.uptime(),
    memory: process.memoryUsage()
  })
})

资源注释

资源支持显示提示和内容元数据的注释:

mcp.addResource({
  uri: 'doc://readme',
  name: 'README',
  title: 'Project Documentation',     // Human-readable title
  mimeType: 'text/markdown',
  text: '# My Project',
  annotations: {
    audience: ['user'],               // Who content is for
    priority: 0.9,                    // Importance (0.0 to 1.0)
    lastModified: '2025-01-15T10:00:00Z'
  }
})

动态资源每次读取都可以返回注释:

mcp.addResource({
  uri: 'cache://data',
  name: 'Cached Data',
  read: async () => ({
    text: JSON.stringify(getCachedData()),
    annotations: {
      lastModified: new Date().toISOString(),
      audience: ['assistant']
    }
  })
})
注释类型描述
audiencestring[]内容针对谁: ["user"], ["assistant"],或 ["user", "assistant"]
prioritynumber重要性:0.0(可选)到1.0(必需)
lastModifiedstringISO 8601上次修改时间戳

资源模板

提取参数的URI模式:

mcp.addResourceTemplate({
  uriTemplate: 'user://{id}',
  name: 'User by ID',
  description: 'Fetch user details',
  mimeType: 'application/json',
  read: async ({ id }) => {
    const user = await db.getUser(id)
    return JSON.stringify(user)
  }
})

// Client can read: user://alice, user://bob, etc.

通知

向连接的客户端推送更新。

// Resource was modified
mcp.notifyResourceUpdated('stats://live')

// Resource list changed (added/removed)
mcp.notifyResourceListChanged()

// Tool list changed
mcp.notifyToolListChanged()

// Progress update for long operations
mcp.notifyProgress('upload-token', 50, 100)

// Custom notification
mcp.notify('notifications/custom', { data: 'anything' })

运输

运行时检测是自动的-- bare-mcp/httpbare-mcp/stdio 使用 which-runtime 在导入时选择正确的实现(Node.js或Bare)。下游包装永远不需要担心。

HTTP传输

HTTP传输支持三种连接模式,所有连接模式都由同一服务器提供服务:

模式端点协议方向
流式HTTPPOST /mcp基于HTTP的JSON-RPC请求→ 答复
上海证券交易所GET /sse + POST /message基于SSE的JSON-RPC双向
双向通信ws://host:port基于WS的JSON-RPC双向

启动服务器

import { createMCPServer } from 'bare-mcp'
import { createHttpTransport } from 'bare-mcp/http'

const mcp = createMCPServer({ name: 'my-server', version: '1.0.0' })

mcp.addTool({
  name: 'greet',
  description: 'Say hello',
  inputSchema: {
    type: 'object',
    properties: { name: { type: 'string' } },
    required: ['name']
  },
  execute: async ({ name }) => `Hello, ${name}!`
})

const transport = await createHttpTransport(mcp, {
  port: 3000,
  host: '0.0.0.0',
  websocket: true,   // Enable WebSocket (default: true, Node.js only)
  verbose: false,     // Log requests/notifications to stderr (default: false)
  onActivity: (entry) => console.log('Tool called:', entry.tool)
})

HTTP端点

方法路径描述
POST/mcp/JSON-RPC端点(可流式HTTP)
GET/sseSSE流(双向MCP传输)
POST/message?sessionId=...SSE消息端点(与配对 /sse)
GET/health健康检查({ status, server, version, requestCount })
GET/activity最近的工具调用活动日志
POST/activity/clear清除活动日志
WSws://host:portWebSocket(仅限Node.js)

流式HTTP(推荐)

最简单的模式。客户端通过POST发送JSON-RPC请求,并在HTTP正文中接收响应。这是Cursor、Claude Code和大多数现代MCP客户端使用的传输方式。

// Client sends a request
const res = await fetch('http://localhost:3000/mcp', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    method: 'initialize',
    params: {
      protocolVersion: '2025-03-26',
      capabilities: {},
      clientInfo: { name: 'my-client', version: '1.0.0' }
    },
    id: 1
  })
})
const { result } = await res.json()
// result.serverInfo, result.capabilities, etc.

// Call a tool
const toolRes = await fetch('http://localhost:3000/mcp', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    method: 'tools/call',
    params: { name: 'greet', arguments: { name: 'World' } },
    id: 2
  })
})
const { result: toolResult } = await toolRes.json()
// toolResult.content[0].text === 'Hello, World!'

通知(否 id 字段)接收a 204 No Content 响应:

await fetch('http://localhost:3000/mcp', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    method: 'notifications/initialized'
  })
})
// 204 No Content

苏格兰和南方能源公司运输(传统)

MCP SSE传输是双向的。客户端打开SSE流,接收 POST 端点URL,然后向该URL发送JSON-RPC请求。响应和服务器通知到达SSE流。

// 1. Open SSE connection
const events = new EventSource('http://localhost:3000/sse')

let messageEndpoint = null

// 2. Wait for the endpoint event (sent immediately on connect)
events.addEventListener('endpoint', (e) => {
  messageEndpoint = e.data
  // e.g. "http://localhost:3000/message?sessionId=client-1-1234567890"
})

// 3. Listen for responses and notifications on the SSE stream
events.addEventListener('message', (e) => {
  const msg = JSON.parse(e.data)

  if (msg.id) {
    // Response to a request you sent
    console.log('Response:', msg.result)
  } else if (msg.method) {
    // Server-initiated notification
    console.log('Notification:', msg.method, msg.params)
  }
})

// 4. Send JSON-RPC requests by POSTing to the endpoint
await fetch(messageEndpoint, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    method: 'tools/call',
    params: { name: 'greet', arguments: { name: 'World' } },
    id: 1
  })
})
// HTTP response is 202 Accepted — the actual result arrives on the SSE stream

WebSocket(仅限Node.js)

基于WebSocket的全双向JSON-RPC。支持订阅和实时通知。

const ws = new WebSocket('ws://localhost:3000')

ws.onopen = () => {
  // Initialize
  ws.send(JSON.stringify({
    jsonrpc: '2.0',
    method: 'initialize',
    params: {
      protocolVersion: '2025-03-26',
      capabilities: {},
      clientInfo: { name: 'ws-client', version: '1.0.0' }
    },
    id: 1
  }))

  // Call a tool
  ws.send(JSON.stringify({
    jsonrpc: '2.0',
    method: 'tools/call',
    params: { name: 'greet', arguments: { name: 'World' } },
    id: 2
  }))

  // Subscribe to resource updates
  ws.send(JSON.stringify({
    jsonrpc: '2.0',
    method: 'resources/subscribe',
    params: { uri: 'stats://live' },
    id: 3
  }))
}

ws.onmessage = (e) => {
  const msg = JSON.parse(e.data)

  if (msg.type === 'connected') {
    // Initial connection status
    console.log('Connected as:', msg.clientId)
  } else if (msg.id) {
    // Response to a request
    console.log('Response:', msg.result)
  } else if (msg.method) {
    // Server notification (resource updates, progress, etc.)
    console.log('Notification:', msg.method, msg.params)
  }
}

运输退货对象

createHttpTransport() 返回:

属性类型描述
portnumber绑定端口
hoststring绑定主机
httpServerhttp.Server底层HTTP服务器
wss`WebSocketServer \null`WebSocket服务器(Node.js,如果启用)
wsClientsMap已连接的WebSocket客户端
sseClientsMap连接的SSE客户端
activityLogArray最近的工具调用活动
requestCount()function返回请求总数
broadcast(msg)function发送给所有客户端(WS+SSE)
close()async function优雅的关机

stdio运输

对于Claude Desktop和通过stdin/stdout通信的类似客户端:

import { createMCPServer } from 'bare-mcp'
import { createStdioTransport } from 'bare-mcp/stdio'

const mcp = createMCPServer({ name: 'my-server', version: '1.0.0' })
mcp.addTool({ /* ... */ })

await createStdioTransport(mcp, {
  onActivity: (entry) => console.error('Tool:', entry.tool),
  onClose: () => process.exit(0)
})

配置MCP客户端

克劳德桌面(stdio)

~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "my-server": {
      "command": "node",
      "args": ["/path/to/server.js"]
    }
  }
}

克劳德桌面(HTTP)

{
  "mcpServers": {
    "my-server": {
      "url": "http://localhost:3000/mcp"
    }
  }
}

游标(HTTP)

在光标设置中→ MCP,添加服务器URL:

http://localhost:3000/mcp

Cursor使用流式HTTP传输(POST /mcp).

通用MCP客户端(HTTP)

任何支持Streamable HTTP的MCP客户端都可以通过指向 /mcp 端点:

http://your-host:3000/mcp

使用传统SSE传输的客户端应连接到:

http://your-host:3000/sse

MCP方法

方法说明
initialize初始化连接,获取功能
tools/list列出可用工具
tools/call执行工具
resources/list列出可用资源
resources/templates/list列出资源模板
resources/read按URI读取资源
resources/subscribe订阅资源更新
resources/unsubscribe取消订阅更新
ping健康检查

通知类型

方法说明
notifications/resources/updated资源的内容已更改
notifications/resources/list_changed添加/删除资源
notifications/tools/list_changed添加/删除工具
notifications/progress进度更新

API 参考

createMCPServer(options)

创建MCP服务器实例。

const mcp = createMCPServer({
  name: 'my-server',           // Server name
  version: '1.0.0',            // Server version
  protocolVersion: '2025-11-25' // MCP protocol version
})

返回一个具有以下属性的对象:

  • addTool(tool) / addTools(tools[]) --注册工具
  • addResource(resource) / addResources(resources[]) --注册资源
  • addResourceTemplate(template) --注册URI模板
  • readResource(uri) --阅读资源
  • notify(method, params) --发送通知
  • notifyResourceUpdated(uri) --通知资源已更改
  • notifyResourceListChanged() --通知添加/删除的资源
  • notifyToolListChanged() --通知添加/删除的工具
  • notifyProgress(token, progress, total?) --发送进度
  • handleRequest(method, params) --处理JSON-RPC请求

createHttpTransport(mcp, options)

启动支持WebSocket和SSE的HTTP服务器。

const transport = await createHttpTransport(mcp, {
  port: 3000,
  host: '0.0.0.0',
  websocket: true,
  onActivity: (entry) => {}
})

退货:

  • port, host --绑定地址
  • httpServer --Node.js HTTP服务器
  • wss --WebSocket服务器
  • broadcast(message) --发送给所有客户
  • close() --关闭服务器

createStdioTransport(mcp, options)

启动stdio传输以供CLI使用。

const transport = await createStdioTransport(mcp, {
  onActivity: (entry) => {},
  onClose: () => {}
})

裸运行时(Pear)

这个图书馆使用 which-runtime 自动检测您是使用Node.js还是Bare,并加载正确的传输实现。无论哪种方式,你的代码都是一样的:

import { createMCPServer } from 'bare-mcp'
import { createHttpTransport } from 'bare-mcp/http'  // Auto-detects runtime
import { createStdioTransport } from 'bare-mcp/stdio'  // Auto-detects runtime

const mcp = createMCPServer({ name: 'my-app' })
mcp.addTool({ /* ... */ })

await createHttpTransport(mcp, { port: 3000 })

显式导入

如果需要绕过运行时检测并针对特定实现:

传输Node.js
HTTPbare-mcp/http-nodebare-mcp/http-bare
站立bare-mcp/stdio-nodebare-mcp/stdio-bare

运输差异

Node.js
超文本传输协议node:http + ws --流式HTTP、SSE、WebSocketbare-http1 --流式HTTP、SSE(无WebSocket)
标准node:readline生的 process.stdin/stdout

依赖项

对于Node.js:

npm install bare-mcp ws

对于裸/梨:

npm install bare-mcp bare-http1

许可证

麻省理工学院

目录标签

目录标签

资源管理WebSocketJavaScriptClaudeAI工具集成本地部署JSON-RPCSSE

支持客户端

Claude DesktopClaudeCursor

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

token

工具数量(toolCount,工具数)

4

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明token部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP