You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Server] Allow overriding the default file name pattern for Discovery (#292)
* Add optional namePatterns capability to discovery so that the default scan for `*.php` files can be overridden.
* add default to namePatterns docs
* add an error check if we manage to pass in an empty list of patterns
* add unit tests (and a static entry to classmap for composer.json so it autoloads from a non .php file for the test)
* run php-cs-fixer
* change how default namePatterns are set and make sure all the annotations are correct
* Fix bad whitespace
Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
* switch to a const default instead of nullable with a check in the function to convert null to a default
* make sure we handle empty arrays
* formatting fixes
* Apply suggestion from @chr-hertel
Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
* Add changelog entry
---------
Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
Copy file name to clipboardExpand all lines: docs/server-builder.md
+19-17Lines changed: 19 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,8 +99,9 @@ $server = Server::builder()
99
99
->setDiscovery(
100
100
basePath: __DIR__,
101
101
scanDirs: ['.', 'src', 'lib'], // Where to look for MCP attributes
102
-
excludeDirs: ['vendor', 'tests'], // Where NOT to look
103
-
cache: $cacheInstance // Optional: cache discovered elements
102
+
excludeDirs: ['vendor', 'tests'], // Where NOT to look
103
+
cache: $cacheInstance, // Optional: cache discovered elements
104
+
namePatterns: ['*.php', '*.inc'], // Optional: list of filename patterns to match
104
105
);
105
106
```
106
107
@@ -109,6 +110,7 @@ $server = Server::builder()
109
110
-`$scanDirs` (array): Directories to recursively scan for `#[McpTool]`, `#[McpResource]`, etc. All subdirectories are included. (default: `['.', 'src']`)
110
111
-`$excludeDirs` (array): Directory names to exclude **within** the scanned directories during recursive scanning
111
112
-`$cache` (CacheInterface|null): Optional PSR-16 cache to store discovered elements for performance
113
+
-`$namePatterns` (array): Optional list of patterns (regexp, glob, or string) for file names (default: `['*.php']`)
112
114
113
115
**Basic Discovery (scans current directory and `src/`):**
114
116
```php
@@ -137,7 +139,7 @@ $server = Server::builder()
137
139
138
140
**How `excludeDirs` works:**
139
141
- If scanning `src/` and there's `src/vendor/`, it will be excluded
140
-
- If scanning `lib/` and there's `lib/tests/`, it will be excluded
142
+
- If scanning `lib/` and there's `lib/tests/`, it will be excluded
141
143
- But if `vendor/` and `tests/` are at the same level as `src/`, they're not scanned anyway (not in `scanDirs`)
142
144
143
145
> **Performance**: Always use a cache in production. The first run scans and caches all discovered MCP elements, making
@@ -255,19 +257,19 @@ $server = Server::builder()
255
257
name: 'add_numbers',
256
258
description: 'Adds two numbers together'
257
259
)
258
-
260
+
259
261
// Using class method pair
260
262
->addTool(
261
263
handler: [Calculator::class, 'multiply'],
262
264
name: 'multiply_numbers'
263
265
// name and description are optional - derived from method name and docblock
264
266
)
265
-
267
+
266
268
// Using instance method
267
269
->addTool(
268
270
handler: [$calculatorInstance, 'divide']
269
271
)
270
-
272
+
271
273
// Using invokable class
272
274
->addTool(
273
275
handler: InvokableCalculator::class
@@ -421,17 +423,17 @@ $server = Server::builder()
421
423
individual JSON-RPC messages. They do not receive the builder's registry, container, or discovery output unless you pass
0 commit comments