12
12
use PhpMcp \Server \Definitions \ResourceDefinition ;
13
13
use PhpMcp \Server \Definitions \ResourceTemplateDefinition ;
14
14
use PhpMcp \Server \Definitions \ToolDefinition ;
15
- use PhpMcp \Server \Exceptions \ McpException ;
15
+ use PhpMcp \Server \Exception \ McpServerException ;
16
16
use PhpMcp \Server \Registry ;
17
17
use Psr \Log \LoggerInterface ;
18
18
use ReflectionAttribute ;
@@ -64,7 +64,7 @@ public function discover(string $basePath, array $directories, array $excludeDir
64
64
$ finder = new Finder ();
65
65
$ absolutePaths = [];
66
66
foreach ($ directories as $ dir ) {
67
- $ path = rtrim ($ basePath , '/ ' ). '/ ' . ltrim ($ dir , '/ ' );
67
+ $ path = rtrim ($ basePath , '/ ' ) . '/ ' . ltrim ($ dir , '/ ' );
68
68
if (is_dir ($ path )) {
69
69
$ absolutePaths [] = $ path ;
70
70
}
@@ -84,7 +84,6 @@ public function discover(string $basePath, array $directories, array $excludeDir
84
84
foreach ($ finder as $ file ) {
85
85
$ this ->processFile ($ file , $ discoveredCount );
86
86
}
87
-
88
87
} catch (Throwable $ e ) {
89
88
$ this ->logger ->error ('Error during file finding process for MCP discovery ' , [
90
89
'exception ' => $ e ->getMessage (),
@@ -149,8 +148,10 @@ private function processFile(SplFileInfo $file, array &$discoveredCount): void
149
148
150
149
if (! $ processedViaClassAttribute ) {
151
150
foreach ($ reflectionClass ->getMethods (ReflectionMethod::IS_PUBLIC ) as $ method ) {
152
- if ($ method ->getDeclaringClass ()->getName () !== $ reflectionClass ->getName () ||
153
- $ method ->isStatic () || $ method ->isAbstract () || $ method ->isConstructor () || $ method ->isDestructor () || $ method ->getName () === '__invoke ' ) {
151
+ if (
152
+ $ method ->getDeclaringClass ()->getName () !== $ reflectionClass ->getName () ||
153
+ $ method ->isStatic () || $ method ->isAbstract () || $ method ->isConstructor () || $ method ->isDestructor () || $ method ->getName () === '__invoke '
154
+ ) {
154
155
continue ;
155
156
}
156
157
$ attributeTypes = [McpTool::class, McpResource::class, McpPrompt::class, McpResourceTemplate::class];
@@ -166,7 +167,6 @@ private function processFile(SplFileInfo $file, array &$discoveredCount): void
166
167
}
167
168
}
168
169
}
169
-
170
170
} catch (ReflectionException $ e ) {
171
171
$ this ->logger ->error ('Reflection error processing file for MCP discovery ' , ['file ' => $ filePath , 'class ' => $ className , 'exception ' => $ e ->getMessage ()]);
172
172
} catch (Throwable $ e ) {
@@ -211,7 +211,7 @@ private function processMethod(ReflectionMethod $method, array &$discoveredCount
211
211
212
212
case McpResource::class:
213
213
if (! isset ($ instance ->uri )) {
214
- throw new McpException ("McpResource attribute on {$ className }:: {$ methodName } requires a 'uri'. " );
214
+ throw new McpServerException ("McpResource attribute on {$ className }:: {$ methodName } requires a 'uri'. " );
215
215
}
216
216
$ definition = ResourceDefinition::fromReflection (
217
217
$ method ,
@@ -240,7 +240,7 @@ private function processMethod(ReflectionMethod $method, array &$discoveredCount
240
240
241
241
case McpResourceTemplate::class:
242
242
if (! isset ($ instance ->uriTemplate )) {
243
- throw new McpException ("McpResourceTemplate attribute on {$ className }:: {$ methodName } requires a 'uriTemplate'. " );
243
+ throw new McpServerException ("McpResourceTemplate attribute on {$ className }:: {$ methodName } requires a 'uriTemplate'. " );
244
244
}
245
245
$ definition = ResourceTemplateDefinition::fromReflection (
246
246
$ method ,
@@ -255,8 +255,7 @@ private function processMethod(ReflectionMethod $method, array &$discoveredCount
255
255
$ discoveredCount ['resourceTemplates ' ]++;
256
256
break ;
257
257
}
258
-
259
- } catch (McpException $ e ) {
258
+ } catch (McpServerException $ e ) {
260
259
$ this ->logger ->error ("Failed to process MCP attribute on {$ className }:: {$ methodName }" , ['attribute ' => $ attributeClassName , 'exception ' => $ e ->getMessage (), 'trace ' => $ e ->getPrevious () ? $ e ->getPrevious ()->getTraceAsString () : $ e ->getTraceAsString ()]);
261
260
} catch (Throwable $ e ) {
262
261
$ this ->logger ->error ("Unexpected error processing attribute on {$ className }:: {$ methodName }" , ['attribute ' => $ attributeClassName , 'exception ' => $ e ->getMessage (), 'trace ' => $ e ->getTraceAsString ()]);
@@ -344,7 +343,7 @@ private function getClassFromFile(string $filePath): ?string
344
343
for ($ j = $ i + 1 ; $ j < $ tokenCount ; $ j ++) {
345
344
if (is_array ($ tokens [$ j ]) && $ tokens [$ j ][0 ] === T_STRING ) {
346
345
$ className = $ tokens [$ j ][1 ];
347
- $ potentialClasses [] = $ namespace ? $ namespace. '\\' . $ className : $ className ;
346
+ $ potentialClasses [] = $ namespace ? $ namespace . '\\' . $ className : $ className ;
348
347
$ i = $ j ;
349
348
break ;
350
349
}
0 commit comments