Skip to content

Commit abd0ddf

Browse files
authored
Remove ability to use parameter name _session and _request (#206)
* Remove ability to use parameter name _session and _request These are reserved for internal use. This will fix #168 * fix cs * add changelog * add test
1 parent 6d74dce commit abd0ddf

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to `mcp/sdk` will be documented in this file.
44

5+
0.2.2
6+
-----
7+
8+
* Throw exception when trying to inject parameter with the unsupported names `$_session` or `$_request`.
9+
510
0.2.1
611
-----
712

src/Capability/Discovery/SchemaGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Mcp\Capability\Discovery;
1313

1414
use Mcp\Capability\Attribute\Schema;
15+
use Mcp\Exception\InvalidArgumentException;
1516
use Mcp\Server\RequestContext;
1617
use phpDocumentor\Reflection\DocBlock\Tags\Param;
1718

@@ -421,6 +422,9 @@ private function parseParametersInfo(\ReflectionMethod|\ReflectionFunction $refl
421422
}
422423

423424
$paramName = $rp->getName();
425+
if (\in_array(strtolower($paramName), ['_session', '_request'], true)) {
426+
throw new InvalidArgumentException(\sprintf('Handler method "%s::%s" has parameter named "%s" which is not allowed. Please change the name of that parameter.', $reflection->class, $reflection->name, $paramName));
427+
}
424428
$paramTag = $paramTags['$'.$paramName] ?? null;
425429

426430
$typeString = $this->getParameterTypeString($rp, $paramTag);

tests/Unit/Capability/Discovery/SchemaGeneratorFixture.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,16 @@ public function parameterSchemaInferredType(
412412
$inferredParam,
413413
): void {
414414
}
415+
416+
public function withParameterNamedSession(string $_session): void
417+
{
418+
}
419+
420+
public function withParameterNamedSessionWithWeirdCase(string $_sesSion): void
421+
{
422+
}
423+
424+
public function withParameterNamedRequest(string $_request): void
425+
{
426+
}
415427
}

tests/Unit/Capability/Discovery/SchemaGeneratorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Mcp\Capability\Discovery\DocBlockParser;
1515
use Mcp\Capability\Discovery\SchemaGenerator;
16+
use Mcp\Exception\InvalidArgumentException;
17+
use PHPUnit\Framework\Attributes\DataProvider;
1618
use PHPUnit\Framework\TestCase;
1719

1820
final class SchemaGeneratorTest extends TestCase
@@ -327,4 +329,21 @@ public function testInfersParameterTypeAsAnyIfOnlyConstraintsAreGiven()
327329
$this->assertEquals(['description' => 'Some parameter', 'minLength' => 3], $schema['properties']['inferredParam']);
328330
$this->assertEquals(['inferredParam'], $schema['required']);
329331
}
332+
333+
public static function methodsWithForbiddenParameter(): array
334+
{
335+
return [
336+
['withParameterNamedSession'],
337+
['withParameterNamedSessionWithWeirdCase'],
338+
['withParameterNamedRequest'],
339+
];
340+
}
341+
342+
#[DataProvider('methodsWithForbiddenParameter')]
343+
public function testGenerateWithForbiddenParameterNames(string $methodName)
344+
{
345+
$method = new \ReflectionMethod(SchemaGeneratorFixture::class, $methodName);
346+
$this->expectException(InvalidArgumentException::class);
347+
$this->schemaGenerator->generate($method);
348+
}
330349
}

0 commit comments

Comments
 (0)