wiseadmin / wise-mcp
WiseAdmin MCP (Model Context Protocol) 基础设施包 — 协议内核、stdio/Streamable HTTP 传输、Server/Client 双角色与能力提供者注册
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
- topthink/framework: ^8.0
- wiseadmin/wise-support: 1.0.*
Requires (Dev)
- phpunit/phpunit: ^9.0 || ^10.0 || ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
适用:WiseAdmin 1.0+ / PHP 8.1+ / ThinkPHP 8 状态:稳定(legacy
2025-11-25+ 无状态2026-07-28双模) 可独立使用:是 —— 只依赖wise-support,不认识 AI / Agent / LLM 定位:协议内核 + 传输 + Server/Client 双角色;领域能力由上层包或插件注册进来
它解决什么问题
把任意系统的能力以 MCP(Model Context Protocol)暴露给外部客户端,或让本系统消费外部 MCP 服务。本包只做协议化,不决定“暴露什么能力”。
L3 领域适配(wise-agent / wise-workflow / 插件…) ToolInterface → ToolDefinition
│ 实现
L2 能力契约(本包) ToolProviderInterface 等 + DTO
│ 使用
L1 协议与传输(本包) JSON-RPC / stdio / Streamable HTTP / Client
核心特性
- Server 双传输:stdio(本地 IDE / Claude Desktop)与 Streamable HTTP(远程客户端);
- Client 双传输:stdio 子进程与 HTTP,含连接池、发现缓存、工具白名单;
- 三大原语:Tools / Resources / Prompts,经 ExtPoint 聚合各子系统能力;
- 双协议模式:
protocol.mode=auto|v2025|v2026,逐请求分流 legacy 与无状态语义; - 安全默认值:HTTP 默认关闭,认证与限流默认开启,写操作默认禁止;
- 生态接入:插件
manifest.json与 Composerextra.think-mcp均可声明提供者; - 可观测:
wise:mcp:stats按天统计各协议纪元流量,为删除 legacy 分支提供证据。
安装
composer require wiseadmin/wise-mcp php think service:discover
5 分钟最小示例
1. 把能力暴露出去(stdio)
claude_desktop_config.json:
{
"mcpServers": {
"wise": { "command": "php", "args": ["think", "wise:mcp:serve"] }
}
}
2. 注册一个工具提供者
use wise\support\ExtPoint; use wise\support\ExtPoints; use wise\mcp\contract\ToolProviderInterface; use wise\mcp\dto\ToolDefinition; final class MyToolProvider implements ToolProviderInterface { public function getNamespace(): string { return 'my'; } public function listTools(): array { return [ new ToolDefinition( name: 'my.do_thing', description: '做某事', inputSchema: [ 'type' => 'object', 'properties' => ['id' => ['type' => 'integer']], 'required' => ['id'], ], handler: fn (array $a): array => ['ok' => true, 'id' => $a['id']], ), ]; } } ExtPoint::register(ExtPoints::MCP_TOOL_PROVIDER, 'my', MyToolProvider::class);
资源与提示模板分别用 MCP_RESOURCE_PROVIDER / MCP_PROMPT_PROVIDER,方式相同。
3. 消费外部 MCP 服务
// config/wise-mcp.php 'clients' => [ 'filesystem' => [ 'transport' => 'stdio', 'command' => 'npx', 'args' => ['-y', '@modelcontextprotocol/server-filesystem', '/data'], 'tools' => ['read_file', 'list_directory'], ], ], 'security' => [ 'allowed_servers' => ['filesystem'], 'allowed_commands' => ['npx'], ],
use wise\mcp\client\McpClientFactory; $factory = app(McpClientFactory::class); foreach ($factory->discoverTools() as $server => $tools) { /* ... */ } $client = $factory->getConnectionManager()->acquire('filesystem'); $client->callTool('read_file', ['path' => '/data/a.txt']);
配置项速查
| 键 | 默认 | 说明 |
|---|---|---|
enable |
false |
总开关;HTTP 端点默认不对外开放 |
web.enable / web.app / web.prefix |
false / admin / admin/mcp |
HTTP 路由配置 |
web.groups.admin.middleware |
McpGuardMiddleware |
包在路由层强制前置,宿主无法移除 |
protocol.mode |
auto |
auto 逐请求分流 / v2025 legacy / v2026 强制无状态 |
transports.stdio.enable / transports.http.enable |
各自独立开关 | 传输级二次开关 |
transports.http.sessions.enable |
true |
legacy 会话开关;无状态路径忽略会话头 |
security.require_auth |
true |
要求已登录,复用 wise-support AuthManager |
security.guard |
admin |
指定 Guard |
security.rate_limit.enable |
true |
按 IP 限流,返回 X-RateLimit-* / Retry-After |
security.allow_write |
false |
拒绝 requires_confirmation=true 的工具 |
security.allowed_origins |
[] |
Origin 白名单,fail-closed;为空拒绝一切带 Origin 的浏览器请求 |
security.max_body_bytes |
1048576 |
请求体上限,超限 413 |
protocol.headers.strict |
true |
无状态请求头严格校验;宽限期可设为 false 仅容忍缺失 |
protocol.metrics.store |
— | 纪元埋点存储,建议 Redis;文件缓存多进程为近似值 |
client.cache.enable / ttl |
true / 300 |
外部工具列表发现缓存 |
discovery.enable / scan_plugins / scan_composer |
开启 | 能力发现开关 |
API / CLI 速查
HTTP 端点同一路径按方法区分:
| 方法 | 作用 |
|---|---|
POST |
发送 JSON-RPC(单条/批量);Accept: text/event-stream 时以 SSE 返回并附带该请求的通知 |
GET |
打开 SSE 流投递服务端通知(需 Mcp-Session-Id)——仅 legacy 路径 |
DELETE |
终止会话(需 Mcp-Session-Id)——仅 legacy 路径 |
命令:
| 命令 | 作用 |
|---|---|
php think wise:mcp:serve |
启动 stdio 服务器 |
php think wise:mcp:discover [--force] |
扫描插件与 Composer 包中的能力提供者并刷新缓存 |
php think wise:mcp:clients [--refresh] |
列出外部 MCP 服务器与已发现工具 |
php think wise:mcp:stats [--days=30] |
查看协议纪元流量(默认 7 天) |
作为 Server
双协议模式(protocol.mode)
| 模式 | 行为 |
|---|---|
auto(默认) |
请求携带 _meta.protocolVersion 或 MCP-Protocol-Version 头 → 无状态;否则 → legacy |
v2025 |
强制 legacy:initialize 握手 + Mcp-Session-Id + GET SSE / DELETE |
v2026 |
强制无状态:仅接受 2026-07-28;GET / DELETE 返回 405 |
无状态路径的对外差异:
- 无
initialize握手、无会话头;请求须携带MCP-Protocol-Version/Mcp-Method(tools/call等另需Mcp-Name)且与请求体一致,否则400 + -32020; params._meta须含io.modelcontextprotocol/protocolVersion与.../clientCapabilities,否则400 + -32602;版本不支持400 + -32022;未知方法404 + -32601;- 新增
server/discover(规范 MUST),列表/读取结果携带ttlMs/cacheScope,所有结果携带resultType与_meta...serverInfo; initialize/ping/logging/setLevel已删除;日志级别由每请求_meta.logLevel声明;- 通知写入请求作用域缓冲,与该请求响应共用 SSE 流;
- 工具原始类型参数可用
x-mcp-header注解镜像为Mcp-Param-{name}请求头(校验一致性)。
legacy 路径由 initialize 响应头下发 Mcp-Session-Id;会话默认启用,可用 transports.http.sessions.enable 关闭。
能力提供者
提供者类实现 ToolProviderInterface / ResourceProviderInterface / PromptProviderInterface。工具执行器返回值支持 string | array | JsonSerializable | Stringable | scalar;数组自动 JSON 编码,错误统一用 ToolDefinition::error($msg),由 McpServer 映射为 isError=true。不要复用 wise-support 的 JsonResponse trait——那是 HTTP 信封,语义不同。
作为 Client
能力矩阵:
| 能力 | 说明 |
|---|---|
| 传输 | stdio 子进程与 Streamable HTTP(自动捕获 Mcp-Session-Id) |
| 出入站 | tools/resources/prompts/ping/notifications;入站应答 roots/list、sampling/createMessage、elicitation/create |
| 连接池 | ConnectionManager 按名称复用,不健康自动重建,gc() / reset() 回收 |
| 发现缓存 | DiscoveryCache 缓存外部工具列表,discoverTools(true) 强制刷新 |
| 白名单 | security.allowed_servers / allowed_commands 与单服务器 tools 白名单 |
wise-agent 侧导入外部工具(默认关闭,避免每请求 spawn 子进程):
// config/wise-agent.php 'mcp' => ['import_external' => true],
导入后外部工具以 mcp:{server} 命名空间进入 ToolRegistry,LLM 可像内置工具一样调用。
协议支持速览
legacy 协商首选
2025-11-25(可协商2025-06-18 / 2025-03-26 / 2024-11-05);无状态修订已实现2026-07-28。两代语义不同,不可混用一个版本列表。CURRENT仍为2025-11-25,须等会话与 GET SSE 移除后才升级声明,谎报会导致严格客户端被拒。
| 能力 | legacy 2025-11-25 |
无状态 2026-07-28 |
|---|---|---|
initialize / ping / logging/setLevel |
✅ | ➖(-32601) |
tools/*、resources/*、prompts/* |
✅ | ✅ |
server/discover |
➖ | ✅(MUST) |
逐请求 _meta 必填校验、结果 ttlMs/cacheScope/serverInfo |
➖ | ✅ |
请求头一致性校验、x-mcp-header 镜像 |
➖ | ✅ |
错误码 -32020 / -32021 / -32022 |
➖ | ✅ |
resultType |
向前兼容增量字段 | 规范必填 |
| 会话 + GET SSE + DELETE | ✅ | ➖(405,会话头被忽略) |
| 跨请求通知 | GET SSE 投递 | 仅请求作用域 |
MRTR / subscriptions/listen / tasks |
➖ | 计划中 |
服务端主动通知:notifications/tools/list_changed / resources/list_changed / prompts/list_changed(由对应 notify*Changed() 触发)、notifications/message(log())、notifications/progress(tools/call 带 params._meta.progressToken 时 0→1)。resultType: "complete" 作为向前兼容字段一并返回。
安全默认值
- HTTP 端点默认关闭(
enable=false);开启后默认挂载McpGuardMiddleware; security.require_auth=true,security.rate_limit.enable=true,security.allow_write=false;allowed_originsfail-closed:为空时拒绝一切携带 Origin 的浏览器请求(不比较可伪造的 Host),仅放行无 Origin 的原生客户端;- 请求体默认上限 1MB;未开启会话的 legacy 请求必须带有效
Mcp-Session-Id; - 无状态路径请求头与请求体不一致或缺失返回
400 + -32020; - 外部连接受
allowed_servers/allowed_commands白名单约束; - 跨请求 FPM 场景下会话与通知队列依赖真实缓存;长驻 SSE 建议 Swoole/Workerman,长驻模式下
HttpEnd事件会释放 stdio 子进程与缓存镜像。
生态接入
插件声明
{
"mcp": {
"tools": ["plugin\\demo\\mcp\\DemoToolProvider"],
"resources": ["plugin\\demo\\mcp\\DemoResourceProvider"],
"prompts": ["plugin\\demo\\mcp\\DemoPromptProvider"]
}
}
Composer 包声明
{
"extra": {
"think-mcp": {
"tool_providers": ["Vendor\\Pkg\\Mcp\\ToolProvider"],
"resource_providers": ["Vendor\\Pkg\\Mcp\\ResourceProvider"],
"prompt_providers": ["Vendor\\Pkg\\Mcp\\PromptProvider"]
}
}
}
内置提供者:wise-agent(全部 Agent 工具)、wise-workflow(workflow.list / workflow.definition)、wise-crontab(crontab.list / crontab.get)。
发现机制:首次扫描插件目录与 vendor/composer/installed.json,默认缓存 3600s;plugin. / composer. 前缀项每次发现前清理,插件卸载后能力自动消失;php think wise:mcp:discover --force 手动刷新。
纪元埋点与删除门槛
按天累计每个请求实际走的纪元:
| 指标 | 含义 |
|---|---|
era:modern / era:legacy |
分支命中次数(门槛只看 legacy) |
ev:{era}:{version} |
分支 × 客户端声明版本 |
method:{era}:{method} |
分支 × 方法 |
code:{code} |
错误码(成功不计数) |
判定规则:窗口内有流量且 legacy 为 0 才进入删除评估;无流量不构成证据。标签经白名单收敛避免缓存键空间爆炸;埋点异常被吞掉、不影响业务;不覆盖进入服务端前的拒绝(Origin / 体积 / JSON 解析 / 头部不一致)。
与 wise-agent 的兼容
| 旧入口 | 行为 |
|---|---|
php think wise:agent:mcp |
wise-mcp 存在时委托其 stdio 传输,否则回落内置实现 |
POST /admin/ai/mcp |
wise-mcp.enable=true 时委托,否则维持原行为 |
wise-agent.mcp.enable/stdio |
保持可用 |
与其他 wise-* 包的关系
wise-support:唯一直接依赖,提供 ExtPoint、AuthManager、RateLimiter 等;wise-agent:首个消费者,同时可把外部 MCP 工具导入 ToolRegistry;wise-rag/wise-workflow/wise-crontab:通过提供者注册暴露各自能力;wise-operation-log:配置检查项在 HTTP 开启但无认证/限流时给出 WARNING。
目录结构
wise-mcp/
├── config/wise-mcp.php
└── src/
├── McpServiceProvider.php # 合并配置、声明扩展点、绑定服务
├── RouteRegistrar.php # 配置驱动 HTTP 端点
├── protocol/ contract/ dto/
├── registry/CapabilityRegistry.php
├── server/McpServer.php
├── transport/ # stdio / StreamableHttp
├── client/ # McpClient / Factory / 连接池 / 缓存
├── controller/McpController.php
└── command/ # serve / discover / clients / stats
许可证
Apache-2.0