Skip to content

Conversation

@CodeWithKyrian
Copy link
Contributor

This PR introduces several significant enhancements and refactors, aimed at increasing flexibility and improving robustness.

Key Changes:

  • Manual Element Registration: Added fluent methods (withTool, withResource, withPrompt, withResourceTemplate) to the Server class, allowing programmatic definition of MCP elements alongside attribute discovery. Supports [Class::class, 'method'] array handlers and invokable class string handlers.
  • Invokable Class Attribute Discovery: Enabled the Discoverer to find #[Mcp*] attributes placed directly on invokable class definitions (classes with a public __invoke method), using the __invoke method as the handler.
  • Dependency Injection Overhaul: Refactored internal dependency management. Core components (Processor, Registry, TransportState, Discoverer, etc.) now resolve essential dependencies (LoggerInterface, CacheInterface, ConfigurationRepositoryInterface) Just-In-Time from the configured PSR-11 container.
  • DI Behavior Clarification: When using the default internal container, withLogger/Cache/Config replace defaults. When using a custom container via withContainer(), providing these core interfaces (LoggerInterface, CacheInterface, ConfigurationRepositoryInterface) within the custom container is mandatory, and the with* methods on the Server instance do not override the container's configuration for these services during runtime.
  • Improved Discovery Control: Introduced withBasePath(), withScanDirectories(), and withExcludeDirectories() methods on the Server for more precise configuration of directory scanning.
  • Transport Handler Updates: Updated Transport Handler constructors (e.g., HttpTransportHandler, StdioTransportHandler) to primarily accept the Server instance, simplifying instantiation.
  • Documentation & Testing: Updated README.md significantly to cover all changes. Added new unit tests for manual registration and invokable discovery, and updated existing tests for DI refactoring.

Breaking Changes:

  • The dependency injection mechanism is refactored. Users providing a custom container via withContainer() MUST ensure LoggerInterface, CacheInterface, and ConfigurationRepositoryInterface are registered within their container. The behavior of withLogger/Cache/Config is altered when a custom container is present; configuration should happen directly in the container.
  • TransportHandler constructors have changed, now primarily accepting the Server instance. Integrations (especially for HTTP transports) will need to update handler instantiation.

Implements several enhancements to the MCP server:

- Adds manual registration methods to the `Server` class (`withTool`, `withResource`, `withPrompt`, `withResourceTemplate`), allowing programmatic definition of MCP elements alongside attribute discovery. Supports array `[Class::class, 'method']` and invokable class string handlers.

- Enables the `Discoverer` to find `#[Mcp*]` attributes placed directly on invokable classes (classes with a public `__invoke` method). Attributes (`McpTool`, `McpResource`, etc.) updated accordingly.

- Refactors dependency injection within the `Server`. Core components like `Processor`, `Discoverer`, `Registry`, and `TransportState` now resolve dependencies (Logger, Cache, Config) Just-In-Time from the PSR-11 container.
  - Clarifies behavior: `withLogger/Cache/Config` methods primarily affect the default `BasicContainer`. When a custom container is used via `withContainer`, these services MUST be registered directly in the custom container.

- Adds configuration options to control discovery paths (`withBasePath`, `withScanDirectories`, `withExcludeDirectories`).

- Updates `README.md` extensively to document these new features and the updated DI behavior.

- Adds comprehensive tests for manual registration and invokable class discovery, and updates existing tests to reflect the DI refactoring.

BREAKING CHANGE: The dependency injection mechanism within the `Server` has been refactored. Components now resolve PSR-3/11/16 dependencies JIT from the container. Users providing a custom container via `withContainer()` MUST ensure `LoggerInterface`, `CacheInterface`, and `ConfigurationRepositoryInterface` are registered. The behavior of `withLogger()`, `withCache()`, and `withConfig()` is altered when a custom container is present; configuration should happen directly within the custom container itself. TransportHandler constructors now primarily accept the `Server` instance.
@CodeWithKyrian CodeWithKyrian merged commit 2ff0f7d into main May 1, 2025
8 checks passed
@CodeWithKyrian CodeWithKyrian deleted the feature/enhanced-element-registration branch May 11, 2025 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants