d3 mcp服务器
MCP服务器,为AI代理提供D3.js API文档和示例代码。从以下位置获取文档 d3js.org 以及来自 可观察的D3画廊,通过可搜索的工具为他们提供服务。
设置
需要Python 3.14+和 紫外线.
克劳德代码
claude mcp add -t stdio -s user d3 -- uvx --from git+https://github.com/jakeb-grant/d3-mcp-server d3-mcp-server克劳德桌面/光标等。
{
"mcpServers": {
"d3": {
"command": "uvx",
"args": ["--from", "git+https://github.com/jakeb-grant/d3-mcp-server", "d3-mcp-server"]
}
}
}更新中
uvx 缓存已安装的版本。要获取最新数据,请清除缓存并重新运行:
uv cache clean d3-mcp-serverMCP检查员
uv run fastmcp dev inspector d3_mcp_server/server.py --with-editable .工具
find_module(query?)
探索D3模块。不带查询,列出所有30个模块。通过查询,返回按名称、标签和描述相关性排名的前5个匹配项。
find_module() → list all modules
find_module("scale") → modules related to scales
find_module("color") → d3-color, d3-scale-chromatic, d3-interpolateget_docs(module_name, page?)
获取模块的文档。默认情况下返回模块概述或特定子页面。
get_docs("d3-scale") → scale overview + list of sub-pages
get_docs("scale") → same (short names work)
get_docs("d3-scale", "linear") → linear scales sub-page
get_docs("d3-array", "ticks") → ticks sub-pagesearch_docs(query, module_name?)
在文档内容中搜索特定主题或方法。可选范围为单个模块。
search_docs("scaleLinear") → searches across relevant modules
search_docs("domain", "d3-scale") → searches within d3-scale onlyfind_example(query?, category?)
从Observable库中浏览和搜索14个类别(条形图、线条、地图、层次结构等)的约170个D3示例。
find_example() → list all categories with counts
find_example(query="treemap") → search examples by keyword
find_example(category="Bars") → list all examples in a categoryget_example(path)
从Observable笔记本中获取示例源代码。从笔记本格式中提取干净、独立的D3代码,以及描述和数据文件URL。
get_example("@d3/bar-chart/2") → bar chart source code
get_example("@d3/force-directed-graph/2") → force-directed graph source建筑
d3_mcp_server/
├── __init__.py # Entry point (main)
├── server.py # FastMCP server, tools, resource template
├── modules.py # D3Module model + 30-module registry
├── examples.py # Observable gallery scraping + notebook code extraction
├── cache.py # File cache (~/.cache/d3-mcp-server/) + HTML→markdown
├── search.py # Module scoring + markdown section parsing/search
└── sync.py # Registry drift detection (see below)
tests/
├── test_examples.py # Gallery parsing, scoring, notebook extraction tests
├── test_search.py # Search, parsing, module resolution tests
├── test_cache.py # HTML conversion, caching, fetch error handling
└── test_server.py # Tool integration tests文档页面从d3js.org获取,剥离到 `` 内容,通过markdownify转换为markdown,并以24小时TTL缓存到磁盘。
注册表同步
中的模块注册表 modules.py 是硬编码的。如果d3js.org添加、删除或重命名模块或页面,注册表将漂移。同步实用程序检测到这一点。
检查漂移情况
uv run python -m d3_mcp_server.sync这会抓取d3js.org/api侧边栏,并将其与硬编码的进行比较 D3_MODULES 列表、报告:
- d3js.org上的新模块不在注册表中
- 注册表中不再存在于d3js.org上的模块
- 添加到现有模块的新子页面
- d3js.org上不再存在的注册表子页
更新注册表
当检测到漂移时,更新 d3_mcp_server/modules.py 遵循这些规则:
D3_MODULES 是要编辑的列表。 每个条目都是一个 D3Module 与:
name--模块名称与d3js.org上显示的完全一致(例如。"d3-array")description--简短摘要(从d3js.org侧栏或模块索引页面复制)tags-用于搜索评分的小写关键字(包括简短名称、API关键术语和相关概念)pages--页面路径的有序列表;第一个条目始终是模块索引页("/d3-array"),后面是子页面("/d3-array/ticks")
对于新模块,添加a D3Module 进入 D3_MODULES。将其按字母顺序放置在现有条目中,或将其与相关模块分组。填充 tags 具有简短的模块名称和来自模块描述和API方法的3-8个相关关键字。
对于已拆卸的模块,删除整个 D3Module 进入。
对于新页面,将页面路径附加到模块的 pages 列表。路径格式为 "/{module_name}/{page_slug}" 其中slug与d3js.org URL匹配。
对于已删除的页面,从模块中删除页面路径 pages 列表。
编辑后,验证:
uv run python -m d3_mcp_server.sync # should report no drift
uv run pytest tests/ -v # all tests should pass
uvx ruff check d3_mcp_server/ tests/ # no lint errors发展
uv run pytest tests/ -v # run tests
uvx ruff check d3_mcp_server/ tests/ # lint
uvx ruff format d3_mcp_server/ tests/ # format
uv run python -m d3_mcp_server.sync # check registry drift
uv run fastmcp dev inspector d3_mcp_server/server.py --with-editable . # MCP inspector