Repository navigation
feat(tools): add typephp-mcp, a TypePHP-built MCP dev assistant (Windows + macOS) - #174
Open
yangweijie wants to merge 3 commits into
Open
yangweijie wants to merge 3 commits into
yangweijie wants to merge 3 commits into
Conversation
Windows was the only platform with an entry point, so the tool could neither be compiled nor tested here: build.bat is cmd-only, McpBinaryTest hard-coded typephp_mcp.exe and therefore always skipped, and the README documented only Windows paths and clients. Add build.sh. There is no tpc package directory on Unix, so it locates a host embed runtime instead (PHP_HOME/PHPX_HOME, then the newest php-builder private runtime, then vendor/swoole/phpx) and derives the link libraries from the archive: a cached private runtime carries extensions this project does not declare, and tpc does not add their system libraries for a static libphp.a (-lonig -lxml2 -lsqlite3 -lz -lresolv -lcurl -lssl -lcrypto -liconv, the last three behind keg-only brew prefixes). It also rejects a phpx older than the php::empty_string API the compiler now emits, which otherwise fails in every translation unit with a C++ error that does not point at the version gap. McpBinaryTest picks the binary by platform, accepts TYPEPHP_MCP_BINARY, and now drives the compiled artifact through both exec() paths: get_index_info's tpc+git and check_code's tpc --dry. Validation: 36 tests / 383 assertions pass with one platform-scoped skip (vswhere); all 12 tools answer byte-identically from the AOT binary and from interpreted PHP apart from check_code's uniqid() build directory; pointing PHP_HOME at a runtime without an embed library turns all three check_code assertions red together, which is the control that they own the compiler path. Linux shares the script but is not yet measured.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
结论
新增
tools/typephp-mcp:用 TypePHP 自己(AOT)实现的 MCP server,通过 stdio 以换行分隔的 JSON-RPC 2.0 对外提供 12 个工具,替代设计文档里原计划的 Python/FastMCP
实现——协议层、检索层、编译器 oracle 全是编译成原生二进制的 PHP 代码。
22 files changed, +3482/-1。除
tools/typephp-mcp/**外只有 3 处顺带改动(见文末)。它是什么
check_codetpc --dry验证 TypePHP 子集合法性,返回file:line诊断或 OKlist_restrictionsget_symbol/search_codefile:line+ 所在声明list_stdlib/get_example/get_stub_api.stub.phpABIsearch_docs/get_changelog/list_topics/get_build_config_helpget_index_info设计取舍(详见
tools/typephp-mcp/README.md):initialize的 instructions 与check_code/list_restrictions的 tooldescription 内置约 800 token 红线摘要,不调工具也能压住头号失败模式。
(
vendor/swoole/typephp/ composer 依赖 /project.yml)并入默认检索集;客户端不发roots 时用
TYPEPHP_MCP_PROJECT_ROOT兜底。构建
tools/typephp-mcp/build.bat(vswhere 定位vcvars64.bat,tpc 发行包路径相对脚本解析,产物
typephp_mcp.exe+ 同目录运行时 DLL)。tools/typephp-mcp/build.sh(本 PR 新增)。Unix 侧没有 tpc 发行包目录可默认,所以脚本改为定位宿主 embed 运行时:显式
PHP_HOME/PHPX_HOME→~/.typephp/php-builder/php-*/install里最新的私有运行时 → 仓库vendor/swoole/phpx,都找不到时给出可执行的下一步提示,而不是让人对着 tpc 那句
The host PHP embed library is missing猜。macOS 实测(Apple Silicon,PHP 8.5.7 + 私有 8.5.11 embed 运行时 + phpx 2.9.4)
三道只有换平台才暴露的墙,逐个实测定位:
libphp.{a,dylib}都不存在,tpc --dry在prepareRuntimeDependencies()直接 Fatal——check_code在 mac 上因此全红(报的是编译器,不是工具逻辑)。
php::empty_string,而composer.lock已不入库,本地vendor/swoole/phpx停在 v2.7.0(头文件里没有该符号)→ 16 个翻译单元全在编译期死。CI 的macos-arm64 作业躲过了是因为它 checkout phpx master。
build.sh:62现在提前校验并直接报「太老,去
composer update swoole/phpx」,不再让人对着 C++ 报错猜。libphp.a时 tpc 不按运行时实际扩展集推导系统库,需应用侧补
-lonig -lxml2 -lsqlite3 -lz -lresolv -lcurl -lssl -lcrypto -liconv(iconv/openssl 还是brew keg-only,要带
-L)。build.sh用ar -t探测归档对象后补全,不靠手抄清单。读数:
php vendor/bin/phpunit tools/typephp-mcp/tests/→ 36 用例 / 383 断言全绿,唯一 skip是
testMsvcBinDirDiscovery(vswhere 只在 Windows 存在)。typephp_mcp(Mach-O arm64,25 MB),真实 stdio 会话跑通initialize/tools/list(12) /tools/call;两条exec()通路(get_index_info的 tpc+git、check_code的 tpc --dry)在编译产物里正常。check_code里那个uniqid()临时构建目录名。PHP_HOME指向无 embed 库的目录时,3 条check_code断言(两个单测 + 产物会话各一)一起变红——这几条断言确实在管编译器通路。
McpBinaryTest原来硬编码typephp_mcp.exe,在 mac 上恒 skip;现在按平台取名并支持TYPEPHP_MCP_BINARY=<path>覆盖,另补了上面两条exec断言。复现
顺带的 3 处改动
phpunit.xml:注册TypePhpMcpTests套件(./tools/typephp-mcp/tests)。.gitignore:忽略/tools/typephp-mcp/build/;工具目录内另忽略产物名typephp_mcp(.exe)。tests/windows/clang/README.md:一处 BuildTools 路径笔误(C:→ 该机实际的D:),与本PR 主题无关,介意可以拆走。
已知边界(想请维护者定夺)
check_code只跑--dry,覆盖 C++ 生成、不覆盖链接期,输出里已明确写这句。要不要再加一条链接期档位?
STDIN/STDOUT/STDERR常量在某些项目形态下被 -O2 折叠成字面量0(
src/Parser/ConstantExpressionTrait.php的常量折叠路径),运行时fwrite()/fgets()抛TypeError。本工具用fopen('php://stdin')绕开,规避写在main.php注释里,但根因在编译器侧,另开 issue 更合适。
libphp.dylib所以躲过了,只有静态 embed 运行时才需要那一串
-l。本 PR 只在build.sh里绕过,没动src/Build/NativeBuildConfigurationTrait.php。build.sh同时按 Linux 写(uname分支只影响 libiconv 那条),Linux 未实跑验证。TYPEPHP_MCP_DEFAULT_ROOT(src/Config.php:10)是开发机绝对路径,在别的机器上永远命中不到,只在「客户端从仓库外拉起且没设
TYPEPHP_MCP_ROOT」时才暴露;是否改成按产物同目录推导,听各位意见。