声明式MCP服务器工具

用于通过声明式配置创建MCP协议服务器的工具模块,支持工具、提示模板和资源的快速定义与部署。

  • by johnhenry
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 johnhenry/mcp-declarative-server

使用说明

MCP声明服务器

用于以声明方式创建模型上下文协议(MCP)服务器的实用模块。

安装

npm install mcp-client-router

用法

import { MCPDeclarativeServer } from "mcp-client-router/declarative-server";

// Create a server declaratively
const server = new MCPDeclarativeServer({
  name: "my-server",
  version: "1.0.0",

  // Define tools as arrays of arguments
  tools: [
    [
      "greeting",
      { message: "string" },
      async ({ message }) => ({
        content: [{ type: "text", text: `Hello, ${message}!` }],
      }),
    ],
    [
      "farewell",
      { name: "string" },
      async ({ name }) => ({
        content: [{ type: "text", text: `Goodbye, ${name}!` }],
      }),
    ],
  ],

  // Define prompts
  prompts: [
    [
      "welcome",
      { name: "string", formality: { type: "string", default: "CASUAL" } },
      async ({ name, formality }) => {
        const text =
          formality === "FORMAL"
            ? `Dear ${name}, welcome to our service.`
            : `Hi ${name}! Welcome aboard!`;

        return {
          messages: [{ role: "assistant", content: { text } }],
        };
      },
      "A welcome prompt template",
    ],
  ],

  // Define resources
  resources: [
    [
      "docs/readme",
      async () => ({
        contents: [
          {
            uri: "docs/readme",
            text: "This is the documentation readme file.",
          },
        ],
      }),
    ],
  ],
});

// Connect to a transport
await server.connect(transport);

api参考

MCPDeclarativeServer

new MCPDeclarativeServer(options);

选项

  • name (string):服务器的名称
  • version (string):服务器的版本
  • tools (array):工具定义数组
  • prompts (array):提示定义数组
  • resources (array):资源定义数组

工具定义格式

[
  name, // string: name of the tool
  paramSchema, // object: parameter schema
  handler, // function: async function to handle the tool call
  description, // string (optional): description of the tool
];

提示定义格式

[
  name, // string: name of the prompt
  paramSchema, // object: parameter schema
  handler, // function: async function to handle the prompt
  description, // string (optional): description of the prompt
];

资源定义格式

[
  uri, // string: URI of the resource
  handler, // function: async function to handle the resource request
];

许可证

国际协调委员会