Skip to content

Commit 88ef002

Browse files
committed
refactor: clean up code formatting and improve readability across multiple files
1 parent 426467c commit 88ef002

File tree

9 files changed

+90
-80
lines changed

9 files changed

+90
-80
lines changed

examples/10-cached-discovery-stdio/CachedCalculatorElements.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
4+
declare(strict_types=1);
5+
36
/*
47
* This file is part of the official PHP MCP SDK.
58
*
@@ -9,15 +12,13 @@
912
* file that was distributed with this source code.
1013
*/
1114

12-
declare(strict_types=1);
13-
1415
namespace Mcp\Example\CachedDiscoveryExample;
1516

1617
use Mcp\Capability\Attribute\McpTool;
1718

1819
/**
1920
* Example MCP elements for demonstrating cached discovery.
20-
*
21+
*
2122
* This class contains simple calculator tools that will be discovered
2223
* and cached for improved performance on subsequent server starts.
2324
*/
@@ -38,16 +39,16 @@ public function multiply(int $a, int $b): int
3839
#[McpTool(name: 'divide_numbers')]
3940
public function divide(int $a, int $b): float
4041
{
41-
if ($b === 0) {
42+
if (0 === $b) {
4243
throw new \InvalidArgumentException('Division by zero is not allowed');
4344
}
44-
45+
4546
return $a / $b;
4647
}
4748

4849
#[McpTool(name: 'power')]
4950
public function power(int $base, int $exponent): int
5051
{
51-
return (int) pow($base, $exponent);
52+
return (int) $base ** $exponent;
5253
}
53-
}
54+
}

examples/10-cached-discovery-stdio/server.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33

44
declare(strict_types=1);
55

6-
require_once __DIR__ . '/../bootstrap.php';
6+
/*
7+
* This file is part of the official PHP MCP SDK.
8+
*
9+
* A collaboration between Symfony and the PHP Foundation.
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
require_once __DIR__.'/../bootstrap.php';
716

817
use Mcp\Server;
918
use Mcp\Server\Transport\StdioTransport;
@@ -20,4 +29,4 @@
2029
->withLogger(logger())
2130
->withCache(new Psr16Cache(new ArrayAdapter())) // Enable discovery caching
2231
->build()
23-
->connect(new StdioTransport());
32+
->connect(new StdioTransport());

src/Capability/Discovery/CachedDiscoverer.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
/**
1818
* Cached decorator for the Discoverer class.
19-
*
19+
*
2020
* This decorator caches the results of file system operations and reflection
2121
* to improve performance when discovery is called multiple times.
22-
*
22+
*
2323
* @author Xentixar <[email protected]>
2424
*/
2525
class CachedDiscoverer
@@ -45,16 +45,16 @@ public function __construct(
4545
public function discover(string $basePath, array $directories, array $excludeDirs = []): DiscoveryState
4646
{
4747
$cacheKey = $this->generateCacheKey($basePath, $directories, $excludeDirs);
48-
48+
4949
// Check if we have cached results
5050
$cachedResult = $this->cache->get($cacheKey);
51-
if ($cachedResult !== null) {
51+
if (null !== $cachedResult) {
5252
$this->logger->debug('Using cached discovery results', [
5353
'cache_key' => $cacheKey,
5454
'base_path' => $basePath,
5555
'directories' => $directories,
5656
]);
57-
57+
5858
// Restore the discovery state from cache
5959
return $this->restoreDiscoveryStateFromCache($cachedResult);
6060
}
@@ -67,10 +67,10 @@ public function discover(string $basePath, array $directories, array $excludeDir
6767

6868
// Perform fresh discovery
6969
$discoveryState = $this->discoverer->discover($basePath, $directories, $excludeDirs);
70-
70+
7171
// Cache the results
7272
$this->cacheDiscoveryResults($cacheKey, $discoveryState);
73-
73+
7474
return $discoveryState;
7575
}
7676

@@ -87,8 +87,8 @@ private function generateCacheKey(string $basePath, array $directories, array $e
8787
'directories' => $directories,
8888
'exclude_dirs' => $excludeDirs,
8989
];
90-
91-
return self::CACHE_PREFIX . md5(serialize($keyData));
90+
91+
return self::CACHE_PREFIX.md5(serialize($keyData));
9292
}
9393

9494
/**
@@ -99,10 +99,10 @@ private function cacheDiscoveryResults(string $cacheKey, DiscoveryState $state):
9999
try {
100100
// Convert state to array for caching
101101
$stateData = $state->toArray();
102-
102+
103103
// Store in cache
104104
$this->cache->set($cacheKey, $stateData, $this->cacheTtl);
105-
105+
106106
$this->logger->debug('Cached discovery results', [
107107
'cache_key' => $cacheKey,
108108
'ttl' => $this->cacheTtl,
@@ -142,8 +142,8 @@ public function clearCache(): void
142142
// This is a simple implementation that clears all discovery cache entries
143143
// In a more sophisticated implementation, we might want to track cache keys
144144
// and clear them selectively
145-
145+
146146
$this->cache->clear();
147147
$this->logger->info('Discovery cache cleared');
148148
}
149-
}
149+
}

src/Capability/Discovery/Discoverer.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ public function applyDiscoveryState(DiscoveryState $state): void
138138
/**
139139
* Process a single PHP file for MCP elements on classes or methods.
140140
*
141-
* @param DiscoveredCount $discoveredCount
142-
* @param array<string, ToolReference> $tools
143-
* @param array<string, ResourceReference> $resources
144-
* @param array<string, PromptReference> $prompts
141+
* @param DiscoveredCount $discoveredCount
142+
* @param array<string, ToolReference> $tools
143+
* @param array<string, ResourceReference> $resources
144+
* @param array<string, PromptReference> $prompts
145145
* @param array<string, ResourceTemplateReference> $resourceTemplates
146146
*/
147147
private function processFile(SplFileInfo $file, array &$discoveredCount, array &$tools, array &$resources, array &$prompts, array &$resourceTemplates): void
@@ -217,13 +217,13 @@ private function processFile(SplFileInfo $file, array &$discoveredCount, array &
217217
* Process a method with a given MCP attribute instance.
218218
* Can be called for regular methods or the __invoke method of an invokable class.
219219
*
220-
* @param \ReflectionMethod $method The target method (e.g., regular method or __invoke).
221-
* @param DiscoveredCount $discoveredCount pass by reference to update counts
222-
* @param \ReflectionAttribute<McpTool|McpResource|McpPrompt|McpResourceTemplate> $attribute the ReflectionAttribute instance found (on method or class)
223-
* @param array<string, ToolReference> $tools
224-
* @param array<string, ResourceReference> $resources
225-
* @param array<string, PromptReference> $prompts
226-
* @param array<string, ResourceTemplateReference> $resourceTemplates
220+
* @param \ReflectionMethod $method The target method (e.g., regular method or __invoke).
221+
* @param DiscoveredCount $discoveredCount pass by reference to update counts
222+
* @param \ReflectionAttribute<McpTool|McpResource|McpPrompt|McpResourceTemplate> $attribute the ReflectionAttribute instance found (on method or class)
223+
* @param array<string, ToolReference> $tools
224+
* @param array<string, ResourceReference> $resources
225+
* @param array<string, PromptReference> $prompts
226+
* @param array<string, ResourceTemplateReference> $resourceTemplates
227227
*/
228228
private function processMethod(\ReflectionMethod $method, array &$discoveredCount, \ReflectionAttribute $attribute, array &$tools, array &$resources, array &$prompts, array &$resourceTemplates): void
229229
{

src/Capability/Discovery/DiscoveryState.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818

1919
/**
2020
* Represents the state of discovered MCP capabilities.
21-
*
21+
*
2222
* This class encapsulates all discovered elements (tools, resources, prompts, resource templates)
2323
* and provides methods to apply this state to a registry.
24-
*
24+
*
2525
* @author Xentixar <[email protected]>
2626
*/
2727
class DiscoveryState
2828
{
2929
/**
30-
* @param array<string, ToolReference> $tools
31-
* @param array<string, ResourceReference> $resources
32-
* @param array<string, PromptReference> $prompts
30+
* @param array<string, ToolReference> $tools
31+
* @param array<string, ResourceReference> $resources
32+
* @param array<string, PromptReference> $prompts
3333
* @param array<string, ResourceTemplateReference> $resourceTemplates
3434
*/
3535
public function __construct(
@@ -88,34 +88,34 @@ public function isEmpty(): bool
8888
*/
8989
public function getElementCount(): int
9090
{
91-
return count($this->tools)
92-
+ count($this->resources)
93-
+ count($this->prompts)
94-
+ count($this->resourceTemplates);
91+
return \count($this->tools)
92+
+ \count($this->resources)
93+
+ \count($this->prompts)
94+
+ \count($this->resourceTemplates);
9595
}
9696

9797
/**
9898
* Get a breakdown of discovered elements by type.
99-
*
99+
*
100100
* @return array{tools: int, resources: int, prompts: int, resourceTemplates: int}
101101
*/
102102
public function getElementCounts(): array
103103
{
104104
return [
105-
'tools' => count($this->tools),
106-
'resources' => count($this->resources),
107-
'prompts' => count($this->prompts),
108-
'resourceTemplates' => count($this->resourceTemplates),
105+
'tools' => \count($this->tools),
106+
'resources' => \count($this->resources),
107+
'prompts' => \count($this->prompts),
108+
'resourceTemplates' => \count($this->resourceTemplates),
109109
];
110110
}
111111

112112
/**
113113
* Create a new DiscoveryState by merging with another state.
114114
* Elements from the other state take precedence.
115115
*/
116-
public function merge(DiscoveryState $other): DiscoveryState
116+
public function merge(self $other): self
117117
{
118-
return new DiscoveryState(
118+
return new self(
119119
tools: array_merge($this->tools, $other->tools),
120120
resources: array_merge($this->resources, $other->resources),
121121
prompts: array_merge($this->prompts, $other->prompts),
@@ -125,7 +125,7 @@ public function merge(DiscoveryState $other): DiscoveryState
125125

126126
/**
127127
* Convert the state to an array for serialization.
128-
*
128+
*
129129
* @return array<string, mixed>
130130
*/
131131
public function toArray(): array
@@ -140,16 +140,16 @@ public function toArray(): array
140140

141141
/**
142142
* Create a DiscoveryState from an array (for deserialization).
143-
*
143+
*
144144
* @param array<string, mixed> $data
145145
*/
146-
public static function fromArray(array $data): DiscoveryState
146+
public static function fromArray(array $data): self
147147
{
148-
return new DiscoveryState(
148+
return new self(
149149
tools: $data['tools'] ?? [],
150150
resources: $data['resources'] ?? [],
151151
prompts: $data['prompts'] ?? [],
152152
resourceTemplates: $data['resourceTemplates'] ?? [],
153153
);
154154
}
155-
}
155+
}

src/Capability/Registry.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,10 @@ public function getResourceTemplates(): array
340340
public function exportDiscoveryState(): DiscoveryState
341341
{
342342
return new DiscoveryState(
343-
tools: array_filter($this->tools, fn($tool) => !$tool->isManual),
344-
resources: array_filter($this->resources, fn($resource) => !$resource->isManual),
345-
prompts: array_filter($this->prompts, fn($prompt) => !$prompt->isManual),
346-
resourceTemplates: array_filter($this->resourceTemplates, fn($template) => !$template->isManual),
343+
tools: array_filter($this->tools, fn ($tool) => !$tool->isManual),
344+
resources: array_filter($this->resources, fn ($resource) => !$resource->isManual),
345+
prompts: array_filter($this->prompts, fn ($prompt) => !$prompt->isManual),
346+
resourceTemplates: array_filter($this->resourceTemplates, fn ($template) => !$template->isManual),
347347
);
348348
}
349349

src/Server/ServerBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,14 @@ public function build(): Server
238238

239239
if (null !== $this->discoveryBasePath) {
240240
$discoverer = new Discoverer($registry, $logger);
241-
241+
242242
// Use cached discoverer if cache is provided
243243
if (null !== $this->cache) {
244244
$discovery = new CachedDiscoverer($discoverer, $this->cache, $logger);
245245
} else {
246246
$discovery = $discoverer;
247247
}
248-
248+
249249
// Discover elements and apply them to the registry
250250
$discoveryState = $discovery->discover($this->discoveryBasePath, $this->discoveryScanDirs, $this->discoveryExcludeDirs);
251251
$discoverer->applyDiscoveryState($discoveryState);

tests/Capability/Discovery/CachedDiscovererPerformanceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CachedDiscovererPerformanceTest extends TestCase
2525
public function testCachedDiscoveryIsFasterThanUncached(): void
2626
{
2727
// Create a temporary directory with some PHP files for testing
28-
$tempDir = sys_get_temp_dir() . '/mcp_discovery_test_' . uniqid();
28+
$tempDir = sys_get_temp_dir().'/mcp_discovery_test_'.uniqid();
2929
mkdir($tempDir, 0755, true);
3030

3131
// Create some test PHP files with MCP attributes
@@ -110,7 +110,7 @@ public function testResource(): string {
110110
];
111111

112112
foreach ($files as $filename => $content) {
113-
file_put_contents($tempDir . '/' . $filename, $content);
113+
file_put_contents($tempDir.'/'.$filename, $content);
114114
}
115115
}
116116

@@ -122,7 +122,7 @@ private function removeDirectory(string $dir): void
122122

123123
$files = array_diff(scandir($dir), ['.', '..']);
124124
foreach ($files as $file) {
125-
$path = $dir . '/' . $file;
125+
$path = $dir.'/'.$file;
126126
if (is_dir($path)) {
127127
$this->removeDirectory($path);
128128
} else {

0 commit comments

Comments
 (0)