静态MCP服务器生成工具

将结构化内容转换为静态MCP服务器的工具,支持从CMS(如Contentful)拉取内容并生成Markdown和JSON文件,为AI代理提供即时内容访问。

  • by megazear7
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 megazear7/static-mcpify

使用说明

静态mcpify

将任何结构化内容转换为静态内容 MCP(模型上下文协议) 服务器。

static-mcpify 从CMS(当前为Contentful)中提取内容,将其构建为静态Markdown和JSON文件,然后将这些文件作为功能齐全的MCP服务器提供。你的AI代理可以即时访问你的内容——没有数据库,没有运行时依赖关系。

网站 · 文档 · 例子 · 部署指南 · npm · GitHub

Netlify入门模板

  • 静态/文件系统启动器: 从以下位置创建新的repo starters/netlify-static-starter 并立即部署捆绑的Markdown/JSON示例内容
  • 令人满意的开场白: 从以下位置创建新的repo starters/netlify-contentful-starter,立即部署捆绑的示例内容,并在以下情况下从Contentful重建 CONTENTFUL_API_TOKEN 和 SPACE_ID 已配置
  • 两个起动器均自动连接Netlify功能 /mcp,船a netlify.toml,并包括一个小型登录页面和示例MCP客户端JSON

快速开始

1.安装

npm install static-mcpify

2.初始化

在 .env 文件:

CONTENTFUL_API_TOKEN=your_token_here
SPACE_ID=your_space_id_here

运行init向导以选择内容类型和配置工具:

npx smcp init --output my-mcp

3.建造

从Contentful中提取内容并生成静态文件:

npx smcp build --output my-mcp

仅构建特定内容类型:

npx smcp build --output my-mcp --content-type blog --content-type author

4.发球

无服务器(Netlify Functions、Cloudflare Workers、Deno、Bun)

使用web标准处理程序——它需要一个 Request 并返回a Response:

import { handleMcpRequest } from 'static-mcpify/web-handler';

export default async (req: Request) => {
  if (req.method === 'GET') {
    return new Response(JSON.stringify({ status: 'ok' }), {
      headers: { 'Content-Type': 'application/json' },
    });
  }

  return handleMcpRequest('./my-mcp/content', req);
};

Express/Node.js HTTP

在传统服务器环境中使用Node.js处理程序:

import { handleMcpRequest } from 'static-mcpify/handler';
import express from 'express';

const app = express();
app.use(express.json());

app.all('/mcp', async (req, res) => {
  await handleMcpRequest('./my-mcp/content', req, res);
});

app.listen(3000);

运作原理

内容结构

构建后,您的输出目录如下:

my-mcp/
├── config.json
└── content/
    ├── assets/
    │   └── photo.json
    └── entries/
        ├── person/
      │   ├── config.json           # defaultTool + tools: biography, skills
        │   ├── bob-smith/
        │   │   ├── data.json         # All non-rich-text fields
        │   │   └── tools/
      │   │       ├── _default.md   # Output for get_person
      │   │       ├── biography.md  # Output for get_person_biography
        │   │       └── skills.md
        │   └── steve-baker/
        │       ├── data.json
        │       └── tools/
      │           ├── _default.md
      │           ├── biography.md
        │           └── skills.md
        └── place/
            ├── config.json
            └── work-site/
                ├── data.json
                └── tools/
                    └── description.md

_default.md 仅在以下情况下生成 defaultTool 为该条目类型配置。命名工具文件,如 biography.md 或 description.md 仅为中列出的条目生成 tools.

自动生成的工具

MCP服务器根据内容结构动态创建工具:

工具 说明
list_assets 列出资产,带可选过滤器
get_asset 按名称获取资产详细信息
list_<type> 列出内容类型的条目,带有可选筛选器
get_<type> 按标题段获取配置的默认工具内容
get_<type>_metadata 启用时,通过标题slug获取条目元数据JSON
get_<type>_<tool> 按标题slug获取工具的标记

工具名称是MCP安全的标识符,因此内容类型ID或工具名称中的破折号被规范化为下划线。例如, agent-skill 成为 get_agent_skill.

所有列表工具返回MCP structuredContent 呈...形状 { "titles": ["...", "..."] } 并做广告 outputSchema 对于这个回应。

例如,对于内容类型 person (默认工具已启用,工具:传记、技能)和 place (工具:说明):

  • list_person / get_person / get_person_metadata / get_person_biography / get_person_skills
  • list_place / get_place / get_place_description
  • list_assets / get_asset

配置

输出配置(config.json)

{
  "source": "contentful",
  "instructions": [
    "Use list_* tools before get_* tools when you need to discover available titles."
  ]
}

instructions 是可选的。如果省略,则默认为 "Use list_* tools before get_* tools when you need to discover available titles.".

这些指令在MCP中发送 initialize 响应作为客户端的服务器级指导。

条目配置(content/entries/<type>/config.json)

{
  "contentType": "person",
  "format": "json",
  "listTool": {
    "description": "List all available people."
  },
  "includeMetadataTool": true,
  "defaultTool": {
    "description": "Get the primary biography for a specific person.",
    "fields": ["biography"]
  },
  "tools": [
    {
      "name": "biography",
      "fields": ["biography"]
    },
    {
      "name": "skills",
      "fields": ["skills", "certifications"]
    }
  ]
}

format 是可选的,默认为 "string".

listTool 是可选的。如果存在,它允许您覆盖显示的描述 list_<type>.

includeMetadataTool 是可选的,默认为 false.

defaultTool 是可选的。当存在时,它控制由返回的内容 get_<type>当省略时, get_<type> 未注册。

tools 是可选的。每个命名工具都定义了一个名称和要包含的Contentful字段。

当 format 是 "string",工具输出呈现为连接的Markdown,并在MCP中返回 content 现场。

当 format 是 "json",每个配置的字段都作为MCP上的属性返回 structuredContent,生成的工具定义包括 outputSchema.

列出始终使用的工具 structuredContent,不管 format.

现场实例

通过将这些添加到您的VS代码中,尝试托管示例MCP服务器 mcp.json:

静态内容示例 (人物和地点):

{
  "static-example": {
    "type": "http",
    "url": "https://static-mcpify.alexlockhart.me/example/static/mcp"
  }
}

有内容的例子 (奇幻冒险和战役):

{
  "contentful-example": {
    "type": "http",
    "url": "https://static-mcpify.alexlockhart.me/example/contentful/mcp"
  }
}

添加新的源适配器

要添加对新CMS(例如Sanity、Strapi)的支持:

  1. 创建 module/src/cli/sources/<name>/index.ts
  2. 实施 SourceAdapter 接口来自 module/src/cli/sources/adapter.ts
  3. 在中注册 module/src/cli/sources/index.ts
  4. 将源名称添加到Zod枚举中 module/src/types/config.ts

发展

先决条件

  • Node.js 22+
  • npm

项目结构

module/                # Publishable npm package (static-mcpify)
├── src/
│   ├── types/         # Zod schemas and TypeScript types
│   ├── cli/           # smcp CLI tool (init + build commands)
│   │   └── sources/   # Source adapters (pluggable)
│   │       └── contentful/
│   └── server/        # MCP server + handlers
├── package.json
└── tsconfig.json
netlify/               # Brand website + hosted examples
├── brand/             # Static HTML/CSS brand website
├── functions/         # Netlify serverless function handlers
└── package.json
examples/              # Example content and configs
├── static/            # Pre-built static content (no CMS needed)
└── contentful/        # Contentful-backed content (built at deploy time)
test/                  # Sanity tests

这是一个包含两个包的npm工作区:

  • module/ --可出版的 static-mcpify npm包(CLI+MCP服务器)
  • netlify/ --用于托管示例的品牌网站和Netlify无服务器功能

命令

命令 描述
npm start 启动MCP服务器(静态+内容)和品牌网站
npm run build TypeScript编译+构建内容丰富的示例
npm run build:ts 仅编译TypeScript
npm test 健全性检查MCP服务器端点
npm run lint 运行ESLint
npm run fix 运行ESLint并自动修复

本地开发

npm start 同时运行三个服务:

  • 静态MCP服务器 在 http://localhost:3100/mcp
  • 内容丰富的MCP服务器 在 http://localhost:3101/mcp
  • 品牌官网 在 http://localhost:3102

测试

运行健全性测试,启动两个MCP服务器并验证健康状况和MCP初始化响应:

npm test

在提交之前,始终运行lint和TypeScript编译:

npm run lint
npm run build:ts

Netlify部署

项目部署到 静态mcpify.alexlockhart.me 通过Netlify。

  • 推到 main 触发自动部署
  • 构建命令: npm run build:ts 加 npm run build:contentful 只有当 CONTENTFUL_API_TOKEN 和 SPACE_ID 已设定
  • node_bundler = "nft" (节点文件跟踪)解析函数中的工作区包
  • 环境变量 CONTENTFUL_API_TOKEN 和 SPACE_ID 在Netlify上被设置为秘密

Netlify函数使用 static-mcpify/web-handler 它封装了MCP SDK WebStandardStreamableHTTPServerTransport 随着 enableJsonResponse: true。这将返回JSON响应,而不是无状态无服务器环境所需的SSE流。

许可证

国际学生委员会


由……制造 亚历克斯·洛克哈特