A powerful Symfony bundle for handling MCP (Message Control Protocol) server implementations, providing tools for JSON-RPC request handling and tool management.
Read the official MCP specification.
Warning
The specification of the Model Context Protocol (MCP) changes frequently.
This bundle will evolve along with the specification, so please ensure you are using the latest version of the bundle.
The CHANGELOG can be found here.
The MCP Server Bundle provides a structured way to create and manage tools that can be used by clients via JSON-RPC requests.
It includes features for MCP tool management, and JSON-RPC method handling.
This bundle is designed to be flexible and extensible, allowing developers to create custom tool handlers and method handlers as needed.
MethodHandlers and ToolHandlers are registered and autowired using attributes, making it easy to define and manage your own tools.
- Install the MCP Server Bundle via Composer:
- Add the bundle to your config/bundles.php (if not using Symfony Flex):
- Configure the routes in config/routes/mcp.yaml:
Tools are the core components of the MCP Server Bundle. They allow you to define and manage custom logic that can be triggered by clients.
- Create a new class that will handle your tool logic
- Use the #[AsTool] attribute to register your tool
- Define the input schema for your tool using a class with validation constraints and OpenAPI attributes
- Implement the __invoke method to handle the tool logic and return a ToolResult
As Tool classes are services within the Symfony application, any dependency can be injected in it, using the constructor, like any other service.
Example:
- Tool input schema class:
- Tool class:
The bundle provides several events that you can listen to:
- ToolCallEvent: Dispatched before a tool is called, contains the tool name and input data
- ToolResultEvent: Dispatched after a tool has been called, contains the result of the tool call
- ToolCallExceptionEvent: Dispatched when a tool throws an exception, contains the tool name, input data and throwable
Example of event listener:
The MCP specification states that tool results should consist of an array of objects.
The bundle provides several result types that can be combined in a single ToolResult object:
- TextToolResult: For text-based results
- ImageToolResult: For image results
- AudioToolResult: For audio results
- ResourceToolResult: For file or resource results
All tool results must be wrapped in a ToolResult object, which can contain multiple results and handle error state.
Example:
Error handling example:
The ToolResult class provides the following features:
- Combine multiple results of different types
- Handle error state
- Automatic serialization to the correct format
- Type safety for all results
The bundle provides robust input validation and sanitization through schema-based deserialization.
Input schemas are extracted from the __invoke method of classes with the #[AsTool] attribute, allowing you to define the expected input structure and validation rules.
- Define your input schema class:
- The bundle will automatically:
- Understand the OA attributes for OpenAPI-based documentation in tools/list
- Deserialize incoming JSON data into your schema class
- Validate all constraints defined in your schema
- Sanitize input data
This ensures that your tool handlers always receive properly validated and sanitized data.
The bundle provides a robust system for handling JSON-RPC requests.
-
initialize
- Called when a client first connects to the server
- Returns server information and capabilities
- Essential for client-server handshake
-
tools/list
- Lists all available tools on the server
- Returns tool metadata including names, descriptions, and input schemas
- Used by clients to discover available tools
-
tools/call
- Executes a specific tool
- Handles input validation and tool execution
- Returns the tool's result or error information
These methods are automatically registered and handled by the bundle. You don't need to implement them yourself.
You can create your own JSON-RPC method handlers for additional functionality:
- Create a new class that implements the MethodHandlerInterface
- Use the #[AsMethodHandler] attribute to register your handler
Example:
The #[AsMethodHandler] attribute supports:
- method (string, required): The JSON-RPC method name
The bundle provides several tools to help you during development:
The debug:mcp-tools command helps you inspect and debug your MCP tools:
This command is particularly useful for:
- Verifying tool registration
- Checking input schemas
- Validating tool annotations
Contributions to the MCP Server Bundle are welcome! Here's how you can help:
- Fork the repository
- Create a new branch for your feature
- Make your changes
- Submit a pull request
Please ensure your code follows our coding standards and includes appropriate tests.
- Fork and clone the repository
- Install dependencies
-
Make your chages
-
Fix the code style and run PHPStan
- Run the tests
This bundle is licensed under the MIT License. See the LICENSE file for details.