Skip to main content

mcp-export

mcp-export 用于扫描 Python 项目中通过 FastMCP 注册的工具、资源和提示词,并生成 OpenAPI 3.x JSON / YAML 文档或 MCP JSON 能力清单。

仅导出通过 @mcp.tool()、@mcp.resource() 和 @mcp.prompt() 注册的内容,普通函数不会被自动导出。支持 FastMCP 2.x 和 4.x。

安装

需要 Python 3.10 或更高版本。

推荐使用 pipx 将 CLI 安装到独立环境:

pipx install mcp-export

也可以使用 uv:

uv tool install mcp-export

作为当前 Python 环境的依赖安装:

pip install mcp-export

如果需要 YAML 输出,请安装 yaml 额外依赖:

pip install "mcp-export[yaml]"
# 使用 pipx 时也可以在首次安装时执行:
pipx install "mcp-export[yaml]"

安装后,在需要扫描的项目根目录运行命令。被扫描项目自身的 Python 依赖仍需在运行环境中可用。

快速开始

扫描当前项目,并按源文件拆分输出到 dist/:

mcp-export

将所有能力合并为一份 OpenAPI JSON 文档:

mcp-export . -o openapi.json

生成 OpenAPI YAML 文档:

mcp-export . --format yaml -o openapi.yaml

生成 MCP JSON 能力清单:

mcp-export . --format mcp -o tools.mcp.json

扫描指定目录:

mcp-export /path/to/project -o openapi.json

未显式指定 -o 时,默认按源文件拆分输出;显式指定 -o 文件 时,合并为单文档。

默认导出文件

默认输出格式为 OpenAPI JSON(OpenAPI 3.0.3),默认输出目录为当前工作目录下的 dist/。

假设项目根目录包含 search.py 和 weather.py:

mcp-export .

将生成:

dist/
├── search.openapi.json
└── weather.openapi.json

如果源文件位于嵌套目录,输出会保留相对目录结构。例如 pkg/tools/search.py 对应:

dist/pkg/tools/search.openapi.json

不指定 -o 时,其他格式同样默认按源文件拆分:

mcp-export . --format yaml  # dist/<源文件名>.openapi.yaml
mcp-export . --format mcp   # dist/<源文件名>.mcp.json

单文件与多文件导出

合并为单文件

显式使用 -o 指定文件路径,会将扫描到的能力合并为一份文档:

# 单个 OpenAPI JSON 文件
mcp-export . -o openapi.json

# 单个 OpenAPI YAML 文件
mcp-export . --format yaml -o openapi.yaml

# 单个 MCP JSON 文件
mcp-export . --format mcp -o tools.mcp.json

输出目录不存在时会自动创建。

按源文件拆分为多文件

省略 -o 时自动使用多文件模式并输出到 dist/:

mcp-export .

也可以指定扫描目录:

mcp-export src
# 默认输出到 dist/,每个源文件生成一份文档

使用 --split 强制启用多文件模式,此时 -o 表示输出目录而不是文件:

mcp-export . --split -o exported

例如:

exported/
├── search.openapi.json
└── weather.openapi.json

多文件模式同样支持 YAML 和 MCP 格式:

mcp-export . --split --format yaml -o exported
mcp-export . --split --format mcp -o exported

输出文件名规则:

<源文件名>.openapi.json
<源文件名>.openapi.yaml
<源文件名>.mcp.json

单文件与多文件模式对比:

命令 模式 输出位置
mcp-export . 多文件,默认行为 dist/<源文件名>.openapi.json
mcp-export . -o openapi.json 单文件 openapi.json
mcp-export . --split -o exported 多文件,指定目录 exported/<源文件名>.openapi.json
mcp-export . --format mcp 多文件,MCP 格式 dist/<源文件名>.mcp.json
mcp-export . --format mcp -o tools.json 单文件,MCP 格式 tools.json

常用示例

按名称过滤

只导出指定工具:

mcp-export . --include "search"

使用 glob 匹配多个能力:

mcp-export . --include "search*,lookup*"

排除内部能力:

mcp-export . --exclude "internal*"

同时使用 include 和 exclude,exclude 优先级更高:

mcp-export . --include "tools.*" --exclude "tools.internal*"

--include 和 --exclude 支持逗号分隔的多个模式,也适用于资源、资源模板和提示词名称。

只处理 Git 变更涉及的模块

mcp-export . --diff -o openapi.diff.json

--diff 基于当前 Git 工作区变更筛选模块;不在 Git 仓库中时会告警并回退到全量导出。

添加 OpenAPI 路径前缀和服务地址

mcp-export . --path-prefix /v1 --server https://api.example.com

--server 可以重复传入:

mcp-export . --server https://api.example.com --server https://staging.example.com

这些参数仅作用于 OpenAPI 格式;MCP 格式会提示并忽略它们。

预览执行计划

mcp-export . --dry-run

--dry-run 只打印导出计划,不扫描模块或写入文件。

禁用缓存

mcp-export . --no-cache

启用严格模式

mcp-export . --strict

严格模式下,模块导入失败或工具重名会返回非零退出码。OpenAPI 文档校验失败时,无论是否启用严格模式都会返回非零退出码。

参数说明

参数 默认值 说明
root(位置参数) . 要扫描的项目或目录路径
--out / -o dist/ 单文件模式下表示输出文件;多文件模式下表示输出目录
--format json 输出格式:json(OpenAPI JSON)、yaml(OpenAPI YAML)或 mcp(MCP JSON)
--split 未指定 -o 时自动启用 强制按源文件拆分,此时 -o 必须按输出目录理解
--config 无 指定配置文件路径
--openapi-version / -V 3.0.3 OpenAPI 版本,支持 3.0.0、3.0.1、3.0.2、3.0.3、3.1.0
--title mcp-export 设置 OpenAPI info.title
--api-version 1.0.0 设置所导出 OpenAPI 文档的 info.version,不是 CLI 自身版本
--path-prefix 空 为所有 OpenAPI 路径添加前缀,例如 /v1,必须以 / 开头
--server 无 添加 OpenAPI servers URL,可重复传入;传入后覆盖配置文件中的 servers
--include 无 逗号分隔的 include glob,按模块名或能力名筛选;命中即保留
--exclude 无 逗号分隔的 exclude glob,按模块名或能力名排除,优先级高于 --include
--diff 关闭 只导出 Git 变更涉及的模块
--dry-run 关闭 只打印导出计划,不扫描模块、不写文件
--no-cache 关闭 忽略 .mcp-export-cache.json,重新导入并提取模块能力
--strict / --no-strict 关闭 严格模式下,导入失败或工具重名会返回非零退出码
--verbose 关闭 输出 debug 日志,便于排查问题
--version 无 显示 mcp-export CLI 自身版本并退出

--openapi-version、--title、--api-version、--path-prefix 和 --server 仅对 OpenAPI 格式生效,MCP 格式会提示并忽略这些参数。

配置文件

工具会自动查找项目根目录下的 mcp-export.toml,然后查找 pyproject.toml 中的 [tool.mcp-export]。也可以通过 --config 显式指定配置文件。

mcp-export.toml 示例:

root = "."
out = "dist"
version = "3.0.3"
title = "my-gateway"
api_version = "1.0.0"
strict = true
include = []
exclude = ["internal*"]
cache = true
split = true
path_prefix = "/v1"
servers = ["https://api.example.com"]
cache_dependencies = ["conf/*.yaml"]

使用指定配置文件:

mcp-export --config mcp-export.toml

配置优先级:

命令行参数 > 配置文件 > 内置默认值

cache_dependencies 用于声明会影响缓存的额外文件;匹配文件的内容发生变化时,缓存会自动失效。

查看帮助

mcp-export --help
mcp-export --version

退出码

退出码 含义
0 执行成功
1 无匹配内容、文档校验失败,或严格模式下出现导入失败 / 工具重名
2 配置错误、路径错误或输出写入失败

注意事项

  • 扫描过程会真实导入项目模块,请只对可信代码运行。
  • 被扫描模块应避免在顶层启动服务或执行其他副作用操作,可使用 if __name__ == "__main__": 保护启动逻辑。
  • OpenAPI 输出支持 3.0.0、3.0.1、3.0.2、3.0.3 和 3.1.0,默认版本为 3.0.3。
  • MCP 输出固定为 JSON,并保留原始 JSON Schema。

Metadata

Release files for mcp-export 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mcp-export 1.0.1
File Size Uploaded Provenance Actions
mcp_export-1.0.1.tar.gz 42.1 kB Without provenance Details for mcp_export-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mcp-export 1.0.1
File Interpreter ABI Platform Actions
mcp_export-1.0.1-py3-none-any.whl Python 3 none any Details for mcp_export-1.0.1-py3-none-any.whl

Total release size: 70.1 kB

Release files / mcp_export-1.0.1.tar.gz

Download URL mcp_export-1.0.1.tar.gz
Size 42.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f57b093f01bb2b20be1d8141a6c20251c2e479c5d65f377726e371dd11ac2fa2
BLAKE2b-256 checksum
How to use checksums
d1f1190844d9104f896cc7bc2ebeb06a487d07535bccae01107deb46669f0456
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / mcp_export-1.0.1-py3-none-any.whl

Download URL mcp_export-1.0.1-py3-none-any.whl
Size 28.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88d2e75975198eb991f7a0b6e2daf00104ceee555da3da9aaf34868ff1f31046
BLAKE2b-256 checksum
How to use checksums
01cc2244b3339d1a3e29710737d67b641bea7ba294d350de71d72b6aa644cf86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for mcp-export 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page