Skip to main content
Glama
theinfyark

mcp-server-plus

by theinfyark

mcp-server-plus

简介

mcp-server-plus 是一个 MCP 服务器工具包——一个用于构建 Model Context Protocol 服务器的小型 TypeScript 框架,无需重复编写样板代码。

包名说明:mcp-server-toolkit 在 npm 上已被占用,因此本包以 mcp-server-plus 发布。

Related MCP server: MCP Framework

为什么有这个包

开发者在启动 MCP 服务器时,反复地重新实现工具注册、提示词、资源、认证、日志和测试。Express 和 Hono 等流行库之所以成功,是因为它们让常规路径变得清晰明了。mcp-server-plus 的目标是在官方 @modelcontextprotocol/sdk 之上提供同样的开发体验。

安装

npm install mcp-server-plus zod

需要 Node.js 18+。

功能特性

  • 工具注册

  • 提示词注册表

  • 资源

  • 认证 / 授权

  • 日志

  • 指标

  • 流式传输(通过 MCP stdio 传输)

  • 中间件

  • CLI 脚手架

  • 测试辅助工具

快速开始

import { z } from "zod";
import { createServer, toolResult } from "mcp-server-plus";

const weatherTool = {
  description: "Get weather",
  inputSchema: { city: z.string() },
  async handler({ city }: { city: string }) {
    return toolResult(`Weather in ${city}: sunny`);
  },
};

const server = createServer({
  name: "demo",
  version: "1.0.0",
});

server.tool("weather", weatherTool);

await server.start(); // stdio

CLI

npx mcp-server-plus init my-weather-server
cd my-weather-server
npm install
npm start

API 参考

createServer(options) / createMcpServer(options)

创建一个 McpKitServer 实例。

选项

类型

描述

name

string

服务器名称

version

string

服务器版本

instructions

string?

可选的 MCP 指令

auth

AuthOptions?

API 密钥 / 自定义认证

middleware

Middleware[]?

全局中间件

logger

Logger?

自定义日志器

server.tool(name, definition)

注册一个工具(同时接入 MCP SDK)。

server.prompt(name, definition)

注册一个提示词模板。

server.resource(uri, definition)

注册一个资源。

server.use(middleware)

在工具调用周围添加中间件。

server.start()

连接 MCP stdio 传输(流式传输由 SDK 处理)。

server.invokeTool(name, args, meta?)

用于测试/脚本的进程内调用。

测试辅助工具

import { callTool, expectText } from "mcp-server-plus/testing";

示例

server.tool("weather", weatherTool);
server.prompt("greet", {
  description: "Greeting",
  arguments: [{ name: "name", required: true }],
  handler: async ({ name }) => ({
    messages: [
      { role: "user", content: { type: "text", text: `Hello ${name}` } },
    ],
  }),
});
server.resource("memo://hello", {
  mimeType: "text/plain",
  handler: async (uri) => ({
    contents: [{ uri: uri.href, text: "Hello", mimeType: "text/plain" }],
  }),
});

高级示例

认证 + RBAC

const server = createServer({
  name: "secure",
  version: "1.0.0",
  // MCP_API_KEY is the expected secret only. Callers must still send meta.apiKey.
  auth: { apiKey: process.env.MCP_API_KEY, required: true },
});

server.tool("deploy", {
  roles: ["admin"],
  scopes: ["deploy"],
  handler: async () => toolResult("deployed"),
});

中间件 + 指标

server.use(async (ctx, next) => {
  const started = Date.now();
  try {
    return await next();
  } finally {
    ctx.log.info("tool timing", ctx.toolName, Date.now() - started);
  }
});

console.log(server.metricsSnapshot());

框架集成

适用于任何支持 stdio 服务器的 MCP 主机。将主机指向你的 node dist/index.js(或 npm start)进程即可。

MCP 主机配置示例:

{
  "mcpServers": {
    "demo": {
      "command": "node",
      "args": ["/path/to/server/src/index.js"]
    }
  }
}

TypeScript 用法

一流的 TypeScript 支持。当你显式标注处理函数类型时,工具参数会从 Zod inputSchema 中推断出来。启用 strict 模式可获得最佳效果。

错误处理

类型化错误:McpKitErrorAuthErrorForbiddenError
工具失败时返回 { isError: true, content: [...] },以便主机安全地显示它们。

性能

  • 官方 SDK 之上的轻量封装(无额外网络跳数)

  • 中间件仅在工具调用时生效

  • 指标使用简单计数器(低开销)

最佳实践

  • 保持工具小巧并注意副作用

  • 使用 Zod schema 验证输入

  • 本地/开发环境使用 optional 认证,共享主机使用 required

  • 单元测试中优先使用 invokeTool;集成测试使用 stdio

常见问题

这是官方 SDK 吗?
不是——它构建在 @modelcontextprotocol/sdk 之上,提供更好的开发体验。

支持流式传输吗?
支持,通过 server.start() 使用的 MCP stdio 传输实现。

CJS 还是 ESM?
双格式发布;以 ESM 为主。

迁移指南

从原始 SDK McpServer 迁移

server.tool(name, definition) 替换 registerTool 样板代码,并保留 Zod schema。调用 server.start() 代替手动连接 StdioServerTransport

SemVer

破坏性变更会在主版本中发布,并记录在 CHANGELOG.md 中。

故障排查

症状

修复方法

主机无法启动服务器

确保调用了 start(),且 stdout 未被日志污染

工具调用未授权

发送 meta.apiKey(或 Bearer)。MCP_API_KEY 是唯一预期的密钥。

类型缺失

mcp-server-plus 导入并使用 Node 18+

贡献

参见 CONTRIBUTING.md

许可证

MIT

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections. Dates show when Glama detected each change.

No tool schema history has been recorded yet.

Maintenance

ActivityMaintained
ResponsivenessNo issues

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    A TypeScript implementation of a Model Context Protocol server that provides a frictionless framework for developers to build and deploy AI tools and prompts, focusing on developer experience with zero boilerplate and automatic tool registration.
    867
    14
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A TypeScript wrapper library for the Model Context Protocol SDK that provides a simplified interface for creating MCP servers with tools, resources, and prompts without needing to work directly with the protocol.
    23
    AGPL 3.0
  • F
    license
    Not graded
    quality
    D
    maintenance
    A clean, reusable TypeScript boilerplate for building Model Context Protocol servers with support for custom tools and resources.
    8
    -

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/theinfyark/mcp-server-plus'

If you have feedback or need assistance with the MCP directory API, please join our Discord server