sphinx-needs-mcp
# sphinx-needs-mcp
sphinx-needs が出力する `needs.json` を読み、要件・設計・検証ケースなどの need を、
AI エージェント(Claude Code など)から取得・検索・トレースできるようにする MCP サーバーです。
> sphinx-needs の開発元(useblocks)とは関係のない、非公式のツールです。
## 何ができるか
ツールはすべて読み取り専用で、ファイルを書き換えません。
| ツール | できること |
| --- | --- |
| `describe_project` | プロジェクトにどんな need の型・リンク・フィールドがあるかを返します。最初にこれを呼びます |
| `get_need` | need 1 件の詳細(本文、フィールド、出ているリンクと向かってくるリンク、原稿の場所)を返します |
| `search_needs` | 型・ステータス・フィールドの値・文字で絞り込んだ一覧を返します |
| `trace` | リンクをたどって、上流(リンクの先)と下流(リンクの元)の need を返します |
| `status_report` | 型ごと・ステータスごとの件数と、リンクの張られ具合を返します。設定ファイルの `[watch]` に書いたもの(TBD の一覧など)も足します |
| `check_rules` | 設定ファイルに書いた網羅の規則を調べ、守れていない need と理由を返します([網羅の規則](#網羅の規則check_rules)) |
need の型やリンクの名前はコードに決め打ちしていません。`needs.json` に入っているスキーマ
(`needs_schema`)から読み取るので、どのプロジェクトでもそのまま使えます。
`ubproject.toml` を渡すと、取り得る値の一覧(列挙値)やリンクの向きの呼び名も補います。
## 使う前に:needs.json を作る
sphinx-needs のドキュメントをビルドすると `needs.json` ができます。次のどちらかで作れます。
- `conf.py`(または `ubproject.toml`)で `needs_build_json = True` を設定して、ふつうにビルドする
- needs ビルダーを使う:`sphinx-build -b needs docs _build/needs`
原稿を直したら、ビルドし直してから問い合わせてください。
`--docs-dir` を指定しておくと、原稿が `needs.json` より新しいときに、結果へ警告(`warning`)が付きます。
動作を確かめている sphinx-needs の版は **8.0** と **9.3** です。
## Claude Code に登録する
リポジトリのルートに `.mcp.json` を置きます。`uvx` が、このリポジトリから直接インストールして起動します。
```json
{
"mcpServers": {
"needs": {
"type": "stdio",
"command": "uvx",
"args": [
"--from", "git+https://github.com/harukikiya/sphinx-needs-mcp",
"sphinx-needs-mcp",
"--needs-json", "_build/html/needs.json",
"--ubproject", "docs/ubproject.toml"
]
}
}
}
```
コマンドで登録することもできます。
```bash
claude mcp add needs -- uvx --from git+https://github.com/harukikiya/sphinx-needs-mcp \
sphinx-needs-mcp --needs-json _build/html/needs.json --ubproject docs/ubproject.toml
```
版を固定したいときは、URL の後ろに `@<タグかコミット>` を付けてください。
### 引数
| 引数 | 意味 | 既定 |
| --- | --- | --- |
| `--needs-json` | 読み込む `needs.json` | `_build/html/needs.json` |
| `--ubproject` | `ubproject.toml`。列挙値やリンクの呼び名を補います | なし(なくても動きます) |
| `--docs-dir` | 原稿のフォルダ。鮮度の確認と、原稿の場所の表示に使います | `ubproject.toml` のあるフォルダ |
| `--config` | 網羅の規則などを書いた設定ファイル | 起動したフォルダの `sphinx-needs-mcp.toml`(あれば) |
パスは、サーバーを起動したフォルダ(ふつうはリポジトリのルート)からの相対パスで書けます。
設定ファイルをリポジトリのルートに `sphinx-needs-mcp.toml` という名前で置けば、`--config` は要りません。
## 返す内容の例
`get_need` で `SWR_001` を引いたときの結果です(テスト用のサンプルプロジェクトより)。
```json
{
"id": "SWR_001",
"type": "swreq",
"title": "温度センサを読む",
"status": "approved",
"type_title": "ソフトウェア要件",
"tags": ["sensor"],
"content": "センサドライバは、温度センサから温度の生値を読み出すこと。",
"fields": {"layer": "driver"},
"links": {
"outgoing": {"refines": ["SYS_001"]},
"incoming": {"verifies": ["SWV_001", "SWV_002"]}
},
"source": "docs/software.md:3"
}
```
- `links.outgoing` は、この need から出ているリンクです(SWR_001 は SYS_001 を refines している)
- `links.incoming` は、この need に向かってくるリンクです(SWV_001 と SWV_002 が SWR_001 を verifies している)
- 値が空のフィールドは省きます
## 網羅の規則(check_rules)
設定ファイル(`sphinx-needs-mcp.toml`)に、「この need は、このようにつながっていること」という規則を書けます。
`check_rules` は規則を 1 つずつ調べ、守れていない need と、その理由を返します。
```toml
[[rules]]
id = "swreq-is-verified"
description = "廃止していないソフトウェア要件は、検証ケースから参照されていること"
select = { type = "swreq", exclude_status = "obsolete" }
require = { incoming = "verifies", linked_type = "swverif" }
[[rules]]
id = "approved-has-no-structural-tbd"
description = "承認済みの need に、構造的 TBD(tbd_items)を残さないこと"
select = { status = "approved" }
require = { field = "tbd_items", empty = true }
[watch]
# 値が入っていたら、status_report の watched に一覧で出すフィールドです
fields = ["tbd_items", "open_params"]
# 型ごとに、フィールドの値の内訳(件数)を status_report の breakdowns に出します
[[watch.breakdowns]]
type = "swverif"
field = "environment"
```
規則 1 つは、`select`(どの need に当てはめるか)と `require`(何を満たすべきか)の組です。
### select:規則を当てはめる need
書いた条件はすべて「かつ」で組み合わせます。何も書かなければ全件が対象です。
値は文字列 1 つでも、文字列のリスト(どれか 1 つに当てはまればよい)でも書けます。
| キー | 意味 | 例 |
| --- | --- | --- |
| `type` | need の型 | `type = "swreq"` |
| `status` | ステータス | `status = ["draft", "approved"]` |
| `exclude_status` | 外すステータス | `exclude_status = "obsolete"` |
| `where` | フィールドの値(`tags` は「含むか」で見ます) | `where = { layer = "driver" }` |
### require:満たすべき条件
次のうち、どれか 1 つだけを書きます。
| 書き方 | 意味 |
| --- | --- |
| `incoming = "verifies"` | そのリンクで、ほかの need から参照されていること |
| `outgoing = "refines"` | そのリンクで、ほかの need を参照していること |
| `field = "rationale"` | そのフィールドに値が入っていること |
| `field = "tbd_items", empty = true` | そのフィールドが空であること |
`incoming` と `outgoing` には、次を足せます。
- `min = 2`:最低限必要な本数(既定は 1)
- `linked_type = "swverif"`:相手の need の型を限る(違う型からのリンクは数えません)
リンク先が存在しないリンク(dead link)は、つながっているとは数えません。
### 結果の例
```json
{
"id": "swreq-is-verified",
"description": "廃止していないソフトウェア要件は、検証ケースから参照されていること",
"checked": 3,
"passed": false,
"violation_count": 1,
"violations": [
{
"id": "SWR_002",
"type": "swreq",
"title": "測定の周期を守る",
"status": "approved",
"reason": "swverif からの verifies で参照されていません(1 件以上必要です)"
}
]
}
```
対象の need が 0 件だった規則には `note` が付きます。合格と言うより、`select` の書き間違いが疑われるためです。
### ビルドの needs_warnings との関係
合否を決めるのは、これまでどおりビルドの `needs_warnings` です。`check_rules` は、
「どの need が、なぜ守れていないか」を調べたり、承認より前(draft のうち)に手当てが必要なものを
見つけたりするために使います。
`needs_warnings` は Python の式で書くので、MCP サーバーの中でそのまま実行(eval)するのは安全ではありません。
そのため、規則はこの設定ファイルに、実行しなくても意味が分かる形で書くようにしています。
rp2040-temp-monitor の `status_report` にあった集計(検証ケースのない SWR、TBD の一覧、検証環境ごとの件数)を
この形で書き直した例が、`tests/fixtures/sample_project/sphinx-needs-mcp.toml` にあります。
## 設計の方針
- **読み取り専用です。** 合否の判定は、これまでどおり Sphinx のビルド(`needs_warnings` と `-W`)に
任せます。この MCP は、AI が調べるための道具にとどめます
- **呼ばれるたびに `needs.json` と設定ファイルを読み直します。** ビルドし直した結果や、直した規則が、すぐに反映されます
- **名前を間違えたら、候補を添えてエラーにします。** 「0 件」とだけ返すと、間違いに気づきにくいためです
## 開発
[uv](https://docs.astral.sh/uv/) を使います。
```bash
uv sync --group dev # 開発用の環境を作る
uv run pytest # テスト
uv run ruff check # lint
```
テスト用の `needs.json`(`tests/fixtures/needs-sphinx-needs-*.json`)は、手で書かずに、
小さなサンプルプロジェクト(`tests/fixtures/sample_project/`)を本物の sphinx-needs でビルドして作ります。
作り直し方は `scripts/make_fixtures.py` の先頭に書いてあります。
## ライセンス
BSD-3-Clause です。詳しくは [LICENSE](LICENSE) を見てください。
TDQS
Scored across 6 tools
Each tool targets a distinct operation: schema discovery (describe_project), single-record detail (get_need), filtered listing (search_needs), link traversal (trace), aggregate stats (status_report), and rule validation (check_rules). The only mild overlap is between status_report and check_rules, both of which surface project-health issues (broken links vs. rule violations), but their descriptions draw a clear line between counts and rule compliance.
Four of six tools follow a clean verb_noun pattern (describe_project, get_need, search_needs, check_rules). The bare verb 'trace' and the noun-first 'status_report' are minor deviations but remain readable and unambiguous in context.
Six tools is well-scoped for a read-oriented requirements-inspection server. Each tool covers a distinct facet (schema, search, detail, tracing, stats, rules) with no redundancy or filler.
The read/analysis surface is comprehensive: schema discovery, search, detail, link tracing, project status, and rule checking form a coherent workflow with no dead ends for inspection. The main gap is the absence of any write/mutation tools (create/update/delete needs), though the server appears intentionally read-only, so this is a minor limitation rather than a failure.