npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@baixiaogou/mcp-server-demo

v1.0.1

Published

一个示例的MCP服务端包

Readme

mcp-server-demo

项目简介

本项目基于 Model Context Protocol (MCP) 协议,演示如何通过 MCP Server 向大语言模型(LLM)或客户端暴露自定义工具(Tool)和资源(Resource),并通过标准输入输出(stdio)作为通信通道进行集成。

主要功能

  • 注册自定义工具(registerTool)
  • 注册自定义资源(registerResource)
  • 通过 StdioServerTransport 启动服务,适合本地开发和集成到 LLM 工具链

registerTool API

作用

注册一个“工具”(Tool),即带参数的接口,供 LLM 或客户端调用,实现自定义功能。

用法

server.registerTool(
  "hello", // 工具名称
  {
    title: "Hello Tool", // 工具标题
    description: "返回 hello, {name}", // 工具描述
    inputSchema: { name: z.string() }, // 输入参数校验(zod schema)
  },
  async ({ name }) => ({
    content: [{ type: "text", text: `hello, ${name}` }], // 返回内容
  })
);

返回内容格式

返回值需为对象,通常包含 content 字段,内容为数组,每项为:

  • type: 内容类型(如 "text")
  • text: 文本内容

示例返回:

{
  "content": [
    { "type": "text", "text": "hello, 张三" }
  ]
}

registerResource API

作用

注册一个“资源”(Resource),即只读数据接口,供 LLM 加载到上下文,实现动态数据获取。

用法

server.registerResource(
  "hello", // 资源名称
  new ResourceTemplate("hello://{name}", { list: undefined }), // 资源模板
  {
    title: "Hello Resource", // 资源标题
    description: "动态 hello 资源", // 资源描述
  },
  async (uri, { name }) => ({
    contents: [
      {
        uri: uri.href,
        text: `hello, ${name}`,
      },
    ],
  })
);

返回内容格式

返回值需为对象,通常包含 contents 字段,内容为数组,每项为:

  • uri: 资源唯一标识
  • text: 文本内容

示例返回:

{
  "contents": [
    {
      "uri": "hello://张三",
      "text": "hello, 张三"
    }
  ]
}

什么是 StdioServerTransport

StdioServerTransport 是 MCP 协议中用于通信的标准输入输出(stdio)传输方式。其特点如下:

  • 通过进程的标准输入(stdin)和标准输出(stdout)进行 JSON-RPC 消息的收发
  • 适合本地开发、调试和集成到 LLM 工具链
  • 启动后,客户端可作为子进程启动本服务,通过 stdio 通道与其通信
  • 保证所有消息为 UTF-8 编码的 JSON-RPC 格式

示例代码:

const transport = new StdioServerTransport();
await server.connect(transport);

参考资料