Skip to content

Conversation

@CodeWithKyrian
Copy link
Contributor

This PR brings support for the 2025-03-26 revision of the MCP Protocol with significant architectural improvements, enhanced schema generation, and new transport capabilities.

✨ New Features

  • StreamableHttpServerTransport: New transport with resumability, event sourcing, and JSON response mode
  • Comprehensive Schema System: Enhanced #[Schema] attribute with full JSON Schema validation support
  • Completion Providers: #[CompletionProvider] attribute for auto-completion in resource templates and prompts
  • Batch Request Processing: Full support for JSON-RPC 2.0 batch requests
  • PSR-20 Clock Interface: Time management with SystemClock and FixedClock for testing
  • Event Store Interface: Pluggable event storage with InMemoryEventStore implementation

🔧 Enhanced Features

  • Smart Schema Generation: Automatic schema generation from method signatures with optional attribute enhancements
  • Session Persistence: Multiple session backends (array, cache, custom) with garbage collection
  • Improved Error Handling: Better JSON-RPC error responses with proper status codes
  • Enhanced Discovery: More robust element discovery with better attribute handling
  • Enum Support: Improved handling for PHP 8.1+ unit enums

🏗️ Architecture Improvements

  • Simplified Configuration: Streamlined session and cache configuration API
  • Better Type Safety: Enhanced type safety throughout with schema package integration
  • Cleaner Separation: Clear separation between discovery attributes and execution logic
  • Unified Message Parsing: Single Parser::parse method across all transports

🔥 Breaking Changes

  • Schema Package Integration: Now uses php-mcp/schema package for all MCP related DTOs, requests, responses, and content types
  • Session Management: Replaced ClientStateManager with SessionManager and Session classes
  • Component Reorganization: Moved Support\* classes to Utils\* namespace
  • Request Processing: Replaced RequestHandler with Dispatcher for better semantic clarity

📚 Documentation

  • Comprehensive README: IMprove with more details about usage and example
  • Production Deployment Guide: VPS, Docker, and SSL deployment strategies
  • Schema Showcase Example: New example demonstrating all schema capabilities
  • Migration Guide: Detailed migration instructions from v2.x

🧪 Testing

  • Complete Test Refactor: Comprehensive unit and integration test suite
  • Transport Integration Tests: Full integration tests for all transport types
  • Schema Generation Tests: Extensive schema generation and validation testing
  • Mock Clients: HTTP, SSE, and Stream clients for testing

📦 Dependencies

  • Requires php-mcp/schema ^1.0
  • Enhanced PSR compliance (PSR-3, PSR-11, PSR-16, PSR-20)

… state

- Updated Protocol, Registry, Server, ServerBuilder, and RequestProcessor to use SessionManager for session handling.
- Adjusted methods to reflect the new session management approach, including cleanup and message queuing.
- Enhanced error handling in AudioContent for better file reading feedback.
- Introduced batch processing and better error handling.
- Refactored Protocol class to use sessionId instead of clientId for message handling.
- Updated ServerBuilder to initialize Protocol with RequestProcessor, streamlining session management.
- Cleaned up unused code and improved error logging for better maintainability.
…3-26 schema

Refactors core MCP element definitions (Tool, Resource, ResourceTemplate, Prompt)
and content types (Text, Audio, EmbeddedResource, ResourceContent) to
enhance type safety and schema compliance with the MCP 2025-03-26 specification.

Key changes include:
- Introduced `Model\Annotations` and `Model\ToolAnnotations` classes for
  structured annotation handling, replacing generic arrays.
- Updated `McpTool`, `McpResource`, and `McpResourceTemplate` attributes
  and their corresponding Definition classes to use these new Annotation types.
- Restructured `JsonRpc\Contents\EmbeddedResource` to be a wrapper for
  `ResourceContent`, correctly reflecting the protocol's embedded resource
  structure. `ResourceContent` now directly holds URI, MIME, and data.
- Added optional `annotations` property to `TextContent` and `AudioContent`.
- Removed individual getter methods from Definition classes, promoting
  reliance on public readonly properties or `toArray()` for MCP output.
- Updated `toArray()`, `fromArray()`, and `fromReflection()` methods across
  all affected Definition and Content classes to support these changes.
- New StreamableHttpTransport to handle post, get and delete requests and resumability
- updated JSON RPC objects to comply more closely with the specification
- added Eventstore interface and implementation for storing stream events to allow resumability
- new SessionIdGenerator Interface and implementation
- Updated Protocol class to utilize SessionInterface for session handling, improving type safety and consistency.
- Refactored message processing methods to accept SessionInterface instead of sessionId directly.
- Improved error handling in StdioServerTransport and StreamableHttpServerTransport for better logging and response management.
- Introduced new methods in SessionInterface for better session data management.
- Updated Protocol class to ensure session creation and deletion are logged correctly during client connection and disconnection.
- Enhanced StreamableHttpServerTransport by consolidating session context management and improving SSE stream handling.
- Removed unnecessary session context variables to streamline the code and improve clarity.
Replaces internal MCP data structures (Requests, Responses, Results,
Content types, Enums, and model definitions like Tool, Resource)
with their counterparts from the new `php-mcp/schema` package, enhancing type safety and compliance with the MCP specification.
…ments

- New `CompletionProviderInterface` and `CompletionProvider` attribute.
- Updated the Dispatcher to handle completion requests, integrating completion logic for prompts and resource templates.
- Enhanced the Registry to manage completion providers, allowing for better organization and retrieval of completion logic.
- Introduced methods in ServerBuilder and Discoverer to facilitate automatic registration of completion providers during element discovery and server setup.
- Replaced custom `parseRequest` methods in `HttpServerTransport`, `StdioServerTransport`, and `StreamableHttpServerTransport` with a unified `Parser::parse` method from the `php-mcp/schema` package.
- Removed redundant code for request parsing, enhancing maintainability and consistency across transport implementations.
- Updated error handling to utilize the new message parsing approach, improving logging and response management.
- Updated the Protocol class to set a 404 status code for invalid or expired sessions and improved logging for response sending and error handling.
- Adjusted response status code handling to utilize the context's status code, ensuring more accurate HTTP responses.
…lement logic

This commit introduces a new set of classes under the `PhpMcp\Server\Elements`
namespace (`RegisteredElement`, `RegisteredTool`, `RegisteredResource`,
`RegisteredPrompt`, `RegisteredResourceTemplate`). These classes now
encapsulate:
- The MCP schema DTO (e.g., `PhpMcp\Schema\Tool`).
- The handler invocation logic (previously in `Support\Handler` and
  `Support\ArgumentPreparer`), including argument preparation and type casting,
  now part of the `RegisteredElement` base class.
- Specific execution methods (e.g., `call()` on `RegisteredTool`, `read()` on
  `RegisteredResource`).
- Result formatting logic (previously in `Traits\ResponseFormatter`) moved
  into the respective `Registered*` classes.
- URI template matching logic is now self-contained within
  `RegisteredResourceTemplate`.
- Completion provider storage is now part of `RegisteredTool`,
  `RegisteredPrompt`, and `RegisteredResourceTemplate`.
- Introduced a new `SystemClock` class implementing `ClockInterface` for time management.
- Updated `ArraySessionHandler` and `CacheSessionHandler` to utilize the `SystemClock` for timestamping, enhancing testability and flexibility.
- Added a `FixedClock` mock for testing purposes, allowing controlled time manipulation in tests.
- Complete refactor of unit tests for all `src/` components.
- New robust integration tests for the transports
- Addressed bugs discovered when adding integration tests
Update documentation to reflect current architecture with proper schema generation,
deployment strategies, and corrected configuration examples.
@CodeWithKyrian CodeWithKyrian changed the title Feat!: MCP Protocol 2025-03-26 Support with Major Architecture Overhaul Feat!: MCP Protocol 2025-03-26 Support Jun 21, 2025
@CodeWithKyrian CodeWithKyrian merged commit c627581 into main Jun 21, 2025
8 checks passed
@CodeWithKyrian CodeWithKyrian deleted the feature/protocol-2025-03-26 branch June 21, 2025 18:59
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