Skip to content

Commit 4a8b33e

Browse files
committed
Exclude Auto injected client logger from generated input-schema
1 parent 2fdb8d8 commit 4a8b33e

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

src/Capability/Discovery/SchemaGenerator.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ private function parseParametersInfo(\ReflectionMethod|\ReflectionFunction $refl
409409
$parametersInfo = [];
410410

411411
foreach ($reflection->getParameters() as $rp) {
412+
if ($this->isAutoInjectedParameter($rp)) {
413+
continue;
414+
}
415+
412416
$paramName = $rp->getName();
413417
$paramTag = $paramTags['$'.$paramName] ?? null;
414418

@@ -784,4 +788,25 @@ private function mapSimpleTypeToJsonSchema(string $type): string
784788
default => \in_array(strtolower($type), ['datetime', 'datetimeinterface']) ? 'string' : 'object',
785789
};
786790
}
791+
792+
/**
793+
* Determines if a parameter was auto-injected and should be excluded from schema generation.
794+
*
795+
* Parameters that are auto-injected by the framework (like McpLogger) should not appear
796+
* in the tool schema since they're not provided by the client.
797+
*/
798+
private function isAutoInjectedParameter(\ReflectionParameter $parameter): bool
799+
{
800+
$type = $parameter->getType();
801+
802+
if (!$type instanceof \ReflectionNamedType) {
803+
return false;
804+
}
805+
806+
$typeName = $type->getName();
807+
808+
// Auto-inject for McpLogger or LoggerInterface types
809+
return 'Mcp\\Capability\\Logger\\McpLogger' === $typeName
810+
|| 'Psr\\Log\\LoggerInterface' === $typeName;
811+
}
787812
}

tests/Inspector/snapshots/StdioCalculatorExampleTest-tools_list.json

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
"type": "number",
1515
"description": "the second operand"
1616
},
17-
"logger": {
18-
"type": "object",
19-
"description": "Auto-injected MCP logger"
20-
},
2117
"operation": {
2218
"type": "string",
2319
"description": "the operation ('add', 'subtract', 'multiply', 'divide')"
@@ -26,8 +22,7 @@
2622
"required": [
2723
"a",
2824
"b",
29-
"operation",
30-
"logger"
25+
"operation"
3126
]
3227
}
3328
},
@@ -37,10 +32,6 @@
3732
"inputSchema": {
3833
"type": "object",
3934
"properties": {
40-
"logger": {
41-
"type": "object",
42-
"description": "Auto-injected MCP logger"
43-
},
4435
"setting": {
4536
"type": "string",
4637
"description": "the setting key ('precision' or 'allow_negative')"
@@ -51,8 +42,7 @@
5142
},
5243
"required": [
5344
"setting",
54-
"value",
55-
"logger"
45+
"value"
5646
]
5747
}
5848
}

tests/Unit/Capability/Discovery/SchemaGeneratorFixture.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,15 @@ public function parameterSchemaInferredType(
412412
$inferredParam,
413413
): void {
414414
}
415+
416+
/**
417+
* Method with McpLogger that should be excluded from schema.
418+
*
419+
* @param string $message The message to process
420+
* @param \Mcp\Capability\Logger\McpLogger $logger Auto-injected logger
421+
*/
422+
public function withMcpLogger(string $message, \Mcp\Capability\Logger\McpLogger $logger): string
423+
{
424+
return $message;
425+
}
415426
}

tests/Unit/Capability/Discovery/SchemaGeneratorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,20 @@ public function testInfersParameterTypeAsAnyIfOnlyConstraintsAreGiven()
327327
$this->assertEquals(['description' => 'Some parameter', 'minLength' => 3], $schema['properties']['inferredParam']);
328328
$this->assertEquals(['inferredParam'], $schema['required']);
329329
}
330+
331+
public function testExcludesMcpLoggerFromSchema()
332+
{
333+
$method = new \ReflectionMethod(SchemaGeneratorFixture::class, 'withMcpLogger');
334+
$schema = $this->schemaGenerator->generate($method);
335+
336+
// Should include the message parameter
337+
$this->assertArrayHasKey('message', $schema['properties']);
338+
$this->assertEquals(['type' => 'string', 'description' => 'The message to process'], $schema['properties']['message']);
339+
340+
// Should NOT include the logger parameter
341+
$this->assertArrayNotHasKey('logger', $schema['properties']);
342+
343+
// Required array should only contain client parameters
344+
$this->assertEquals(['message'], $schema['required']);
345+
}
330346
}

0 commit comments

Comments
 (0)