Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MCP server for Phan#5459

Open
rlerdorf wants to merge 5 commits intov6from
phan-mcp
Open

MCP server for Phan#5459
rlerdorf wants to merge 5 commits intov6from
phan-mcp

Conversation

@rlerdorf
Copy link
Member

@rlerdorf rlerdorf commented Mar 2, 2026

Adds a Model Context Protocol (MCP) server that exposes Phan's analysis capabilities as tools for AI coding agents. The server is a small Go binary that routes queries to the Phound SQLite database (instant lookups) or the Phan daemon (live analysis).

Architecture

AI Agent ─── stdio (NDJSON) ──→ phan-mcp binary
                                     │
                   ┌─────────────────┼─────────────────┐
                   ▼                                   ▼
           SQLite (read-only)                  Phan daemon (TCP)
           ├─ references                       ├─ analyze
           ├─ signature                        └─ type_at
           ├─ hierarchy
           ├─ implementations
           ├─ symbols
           ├─ file_symbols
           └─ unused

Tools (9)

Tool Backend Description
analyze Daemon Run Phan on a file, get diagnostics
type_at Daemon Get Phan's inferred type for a variable at a line
references SQLite Find all callsites of a method/property/constant
signature SQLite Get full signature with params, types, docblock
hierarchy SQLite Class parents, children, interfaces, traits
implementations SQLite Classes implementing an interface or extending a class
symbols SQLite Search symbols by name pattern
file_symbols SQLite List all symbols defined in a file
unused SQLite Find dead code (symbols with no callsites)

New files

  • tool/phan-mcp/main.go — Entry point, startup logging, NDJSON stdio loop
  • tool/phan-mcp/protocol.go — JSON-RPC 2.0 + MCP type definitions
  • tool/phan-mcp/tools.go — Tool definitions (JSON Schema) and dispatch
  • tool/phan-mcp/sqlite.go — 7 SQLite-backed tools
  • tool/phan-mcp/daemon.go — 2 daemon-backed tools (analyze, type_at)
  • tool/phan-mcp/build.sh — Build script (auto-downloads Go if needed)
  • tool/phan-mcp/ensure-go.sh — Portable Go toolchain bootstrapper (Linux + macOS)
  • .mcp.json — Claude Code integration config
  • .gitignore — Added generated artifacts (phound.sqlite, binary, .goroot/)

Dependencies

  • modernc.org/sqlite — pure Go SQLite driver (no CGo, no C compiler)
  • No other external dependencies

Usage

# 1. Build the Phound SQLite database (requires PhoundPlugin changes)
./phan --plugin src/Phan/Plugin/Internal/PhoundPlugin.php

# 2. Build the MCP server (downloads Go if needed)
./tool/phan-mcp/build.sh

# 3. Claude Code auto-discovers via .mcp.json
# For live analysis, also start the daemon:
./phan --daemonize-tcp-port 4846

Startup output

$ ./tool/phan-mcp/phan-mcp --db .phan/phound.sqlite --daemon 127.0.0.1:4846
phan-mcp v0.1.0 — MCP server for Phan static analysis
Transport: stdio (JSON-RPC 2.0, newline-delimited)
SQLite: .phan/phound.sqlite (20352 signatures, 48759 callsites)
Daemon: 127.0.0.1:4846
Ready — reading from stdin

@rlerdorf rlerdorf mentioned this pull request Mar 2, 2026
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 43214e9c49

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an MCP (Model Context Protocol) stdio server (phan-mcp) to expose Phan capabilities to AI agents, backed by (1) a read-only Phound SQLite database for fast symbol queries and (2) the Phan daemon for live analysis.

Changes:

  • Introduces a Go-based MCP server with JSON-RPC/MCP protocol handling plus tool dispatch for SQLite- and daemon-backed operations.
  • Extends PhoundPlugin to persist signature/parameter data into SQLite to support symbol/signature/file-based queries.
  • Updates Language Server diagnostics to populate LSP Diagnostic.code with Phan’s check name (string), and updates integration tests accordingly.

Reviewed changes

Copilot reviewed 13 out of 16 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
tool/phan-mcp/main.go MCP server entrypoint, stdio loop, flag parsing, SQLite/daemon initialization logging
tool/phan-mcp/protocol.go JSON-RPC + MCP type definitions and helper result builders
tool/phan-mcp/tools.go MCP tool schemas and dispatch routing to server handlers
tool/phan-mcp/sqlite.go SQLite-backed tool implementations (references, signature, hierarchy, implementations, symbols, file_symbols, unused)
tool/phan-mcp/daemon.go Daemon-backed tool implementations (analyze, type_at) and TCP request/response plumbing
tool/phan-mcp/go.mod Go module definition and dependency on modernc.org/sqlite
tool/phan-mcp/go.sum Go dependency lockfile for the MCP server
tool/phan-mcp/build.sh Build wrapper that bootstraps Go and compiles phan-mcp
tool/phan-mcp/ensure-go.sh Portable Go toolchain bootstrap/download script
src/Phan/Plugin/Internal/PhoundPlugin.php Adds signatures/parameters tables and writes signature data during finalizeProcess
src/Phan/LanguageServer/LanguageServer.php Includes check_name as LSP diagnostic code
src/Phan/LanguageServer/Protocol/Diagnostic.php Allows Diagnostic.code to be `int
tests/Phan/LanguageServer/LanguageServerIntegrationTest.php Updates expected diagnostics to include code as check name
.mcp.json Adds repo MCP server config for Claude Code integration
.gitignore Ignores generated phound DB, built binary, and cached Go toolchain

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 16 changed files in this pull request and generated 10 comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants