Skip to content

Commit edd7b70

Browse files
committed
feat(router): update MCP routes to support multiple HTTP methods and improve handler retrieval
1 parent 1538b34 commit edd7b70

File tree

4 files changed

+75
-27
lines changed

4 files changed

+75
-27
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,38 @@ If you're using Hyperf framework, integration is extremely simple:
3232

3333
```php
3434
// Just one line of code!
35-
Router::post('/mcp', function () {
36-
return di(HyperfMcpServer::class)->handler();
35+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
36+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler();
3737
});
3838
```
3939

40+
**Annotation-Based Registration**:
41+
```php
42+
class CalculatorService
43+
{
44+
#[McpTool(description: 'Mathematical calculations')]
45+
public function calculate(string $operation, int $a, int $b): array
46+
{
47+
return ['result' => match($operation) {
48+
'add' => $a + $b,
49+
'multiply' => $a * $b,
50+
default => 0
51+
}];
52+
}
53+
54+
#[McpResource(description: 'System information')]
55+
public function systemInfo(): TextResourceContents
56+
{
57+
return new TextResourceContents('mcp://system/info',
58+
json_encode(['php' => PHP_VERSION]), 'application/json');
59+
}
60+
}
61+
```
62+
4063
**Advanced Options**:
4164
- 🔐 **AuthenticatorInterface** - Custom authentication
4265
- 📊 **HttpTransportAuthenticatedEvent** - Dynamic tool/resource registration
66+
- 📝 **Annotation System** - Auto-register tools, resources and prompts
4367

4468
👉 [View Complete Hyperf Integration Guide](./docs/en/server/hyperf-integration.md)
4569

README_CN.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,38 @@ composer require dtyq/php-mcp
3232

3333
```php
3434
// 只需一行代码!
35-
Router::post('/mcp', function () {
36-
return di(HyperfMcpServer::class)->handler();
35+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
36+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler();
3737
});
3838
```
3939

40+
**基于注解的注册**
41+
```php
42+
class CalculatorService
43+
{
44+
#[McpTool(description: '数学计算')]
45+
public function calculate(string $operation, int $a, int $b): array
46+
{
47+
return ['result' => match($operation) {
48+
'add' => $a + $b,
49+
'multiply' => $a * $b,
50+
default => 0
51+
}];
52+
}
53+
54+
#[McpResource(description: '系统信息')]
55+
public function systemInfo(): TextResourceContents
56+
{
57+
return new TextResourceContents('mcp://system/info',
58+
json_encode(['php' => PHP_VERSION]), 'application/json');
59+
}
60+
}
61+
```
62+
4063
**高级选项**
4164
- 🔐 **AuthenticatorInterface** - 自定义认证
4265
- 📊 **HttpTransportAuthenticatedEvent** - 动态工具/资源注册
66+
- 📝 **注解系统** - 自动注册工具、资源和提示
4367

4468
👉 [查看完整 Hyperf 集成指南](./docs/cn/server/hyperf-integration.md)
4569

docs/cn/server/hyperf-integration.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ composer require dtyq/php-mcp
1919
use Hyperf\HttpServer\Router\Router;
2020
use Dtyq\PhpMcp\Server\Framework\Hyperf\HyperfMcpServer;
2121

22-
Router::post('/mcp', function () {
23-
return di(HyperfMcpServer::class)->handler();
22+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
23+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler();
2424
});
2525
```
2626

@@ -274,18 +274,18 @@ public function processUser(
274274

275275
```php
276276
// 只注册数学相关工具
277-
Router::post('/mcp/math', function () {
278-
return di(HyperfMcpServer::class)->handler('math');
277+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp/math', function () {
278+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler('math');
279279
});
280280

281281
// 注册开发工具
282-
Router::post('/mcp/dev', function () {
283-
return di(HyperfMcpServer::class)->handler('development');
282+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp/dev', function () {
283+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler('development');
284284
});
285285

286286
// 注册所有工具(默认分组)
287-
Router::post('/mcp', function () {
288-
return di(HyperfMcpServer::class)->handler();
287+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
288+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler();
289289
});
290290
```
291291

@@ -622,8 +622,8 @@ use Hyperf\HttpServer\Router\Router;
622622
use Dtyq\PhpMcp\Server\Framework\Hyperf\HyperfMcpServer;
623623

624624
// MCP 服务端点 - 只需一行代码!
625-
Router::post('/mcp', function () {
626-
return di(HyperfMcpServer::class)->handler();
625+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
626+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler();
627627
});
628628
```
629629

docs/en/server/hyperf-integration.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Add MCP route in your route file (e.g., `config/routes.php`):
1919
use Hyperf\HttpServer\Router\Router;
2020
use Dtyq\PhpMcp\Server\Framework\Hyperf\HyperfMcpServer;
2121

22-
Router::post('/mcp', function () {
23-
return di(HyperfMcpServer::class)->handler();
22+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
23+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler();
2424
});
2525
```
2626

@@ -174,7 +174,7 @@ class SystemService
174174

175175
return new TextResourceContents(
176176
'mcp://system/info',
177-
json_encode($info, JSON_PRETTY_PRINT),
177+
json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE),
178178
'application/json'
179179
);
180180
}
@@ -195,7 +195,7 @@ class SystemService
195195

196196
return new TextResourceContents(
197197
'mcp://system/config',
198-
json_encode($config, JSON_PRETTY_PRINT),
198+
json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE),
199199
'application/json'
200200
);
201201
}
@@ -274,18 +274,18 @@ You can organize your annotations using groups and load specific groups:
274274

275275
```php
276276
// Register only math-related tools
277-
Router::post('/mcp/math', function () {
278-
return di(HyperfMcpServer::class)->handler('math');
277+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp/math', function () {
278+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler('math');
279279
});
280280

281281
// Register development tools
282-
Router::post('/mcp/dev', function () {
283-
return di(HyperfMcpServer::class)->handler('development');
282+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp/dev', function () {
283+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler('development');
284284
});
285285

286286
// Register all tools (default group)
287-
Router::post('/mcp', function () {
288-
return di(HyperfMcpServer::class)->handler();
287+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
288+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler();
289289
});
290290
```
291291

@@ -341,7 +341,7 @@ class McpDemoService
341341

342342
return new TextResourceContents(
343343
'mcp://server/status',
344-
json_encode($status, JSON_PRETTY_PRINT),
344+
json_encode($status, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE),
345345
'application/json'
346346
);
347347
}
@@ -622,8 +622,8 @@ use Hyperf\HttpServer\Router\Router;
622622
use Dtyq\PhpMcp\Server\Framework\Hyperf\HyperfMcpServer;
623623

624624
// MCP server endpoint - just one line of code!
625-
Router::post('/mcp', function () {
626-
return di(HyperfMcpServer::class)->handler();
625+
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
626+
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler();
627627
});
628628
```
629629

0 commit comments

Comments
 (0)