klapaudius / symfony-mcp-server
Build your own LLM tools inside your symfony project by adding to it a Model Context Protocol Server
Installs: 282
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 1
Forks: 2
Open Issues: 2
Type:symfony-bundle
Requires
- php: ^8.2
- ext-fileinfo: *
- ext-json: *
- psr/log: ^3.0
- symfony/cache: ~7.0|^6.4
- symfony/config: ~7.0|^6.4
- symfony/console: ~7.0|^6.4
- symfony/dependency-injection: ~7.0|^6.4
- symfony/finder: ~7.0|^6.4
- symfony/http-foundation: ~7.0|^6.4
- symfony/http-kernel: ~7.0|^6.4
- symfony/routing: ~7.0|^6.4
Requires (Dev)
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- phpunit/phpunit: ~11.0|~10.0
- symfony/phpunit-bridge: ~7.0|^6.4
Suggests
- ext-redis: To enable redis SSE Adapter
- klapaudius/oauth-server-bundle: For a complete OAuth2 Authentication mechanism
README
A powerful Symfony package to build a Model Context Protocol Server seamlessly
Overview
Symfony MCP Server is a powerful package designed to streamline the implementation of Model Context Protocol (MCP) servers in Symfony applications. This package utilizes StreamableHTTP and/or Server-Sent Events (SSE) transport, providing a secure and controlled integration methods.
Why not STDIO?
While stdio is straightforward and widely used in MCP implementations, it has significant security implications for enterprise environments:
- Security Risk: STDIO transport potentially exposes internal system details and API specifications
- Data Protection: Organizations need to protect proprietary API endpoints and internal system architecture
- Control: StreamableHTTP or SSE offers better control over the communication channel between LLM clients and your application
By implementing the MCP server with StreamableHTTP or SSE transport, enterprises can:
- Expose only the necessary tools and resources while keeping proprietary API details private
- Maintain control over authentication and authorization processes
Key benefits:
- Seamless and rapid implementation of StreamableHTTP and/or SSE in existing Symfony projects
- Support for the latest Symfony and PHP versions
- Efficient server communication and real-time data processing
- Enhanced security for enterprise environments
Key Features
- Real-time communication support through StreamableHTTP and/or Server-Sent Events (SSE) integration
- Implementation of tools and resources compliant with Model Context Protocol specifications
- Support of streaming tools with progres notifications
- Support different types of tool results such as Text, Image, Audio, or Resource
- Adapter-based design architecture with Pub/Sub messaging pattern
Requirements
- PHP >=8.2
- Symfony >=6.4
Installation
-
Create the configuration file config/packages/klp_mcp_server.yaml and paste into it:
klp_mcp_server: enabled: true server: name: 'My MCP Server' version: '1.0.0' default_path: 'mcp' ping: enabled: true # Read the warning section in the default configuration file before disable it interval: 30 server_providers: ['streamable_http','sse'] sse_adapter: 'cache' adapters: cache: prefix: 'mcp_sse_' ttl: 100 tools: - KLP\KlpMcpServer\Services\ToolService\Examples\HelloWorldTool - KLP\KlpMcpServer\Services\ToolService\Examples\ProfileGeneratorTool - KLP\KlpMcpServer\Services\ToolService\Examples\StreamingDataTool - KLP\KlpMcpServer\Services\ToolService\Examples\VersionCheckTool prompts: - KLP\KlpMcpServer\Services\PromptService\Examples\HelloWorldPrompt resources: - KLP\KlpMcpServer\Services\ResourceService\Examples\HelloWorldResource resources_templates: - KLP\KlpMcpServer\Services\ResourceService\Examples\McpDocumentationResource
For more detailed explanations, you can open the default configuration file from that link.
-
Install the package via Composer:
composer require klapaudius/symfony-mcp-server
-
Add routes in your
config/routes.yaml
klp_mcp_server: resource: '@KlpMcpServerBundle/Resources/config/routes.php' type: php
You're all done! Upon completing this setup, your project will include 3 new API endpoints:
- Streaming Endpoint for MCP Clients:
GET /{default_path}/sse
- Request Submission Endpoint:
POST /{default_path}/messages
- Streamable HTTP Endpoint:
GET|POST /{default_path}
Docker Setup (Optional)
The project includes a Docker setup that can be used for development. The Docker setup includes Nginx, PHP-FPM with Redis extension, and Redis server.
For detailed instructions on how to set up and use the Docker containers, please refer to the Development Guidelines.
Strongly Recommended
Enhance your application's security by implementing OAuth2 Authentication. You can use the klapaudius/oauth-server-bundle or any other compatible OAuth2 solution.
Basic Usage
Creating and Adding Custom Tools
The package provides convenient commands to generate new tools:
php bin/console make:mcp-tool MyCustomTool
This command:
- Handles various input formats (spaces, hyphens, mixed case)
- Automatically converts the name to the proper case format
- Creates a properly structured tool class in
src/MCP/Tools
- Offers to automatically register the tool in your configuration
You can also manually create and register tools in config/packages/klp_mcp_server.yaml
:
use KLP\KlpMcpServer\Services\ProgressService\ProgressNotifierInterface; use KLP\KlpMcpServer\Services\ToolService\Annotation\ToolAnnotation; use KLP\KlpMcpServer\Services\ToolService\Result\ToolResultInterface; use KLP\KlpMcpServer\Services\ToolService\StreamableToolInterface; class MyCustomTool implements StreamableToolInterface { // Tool implementation }
Testing MCP Tools
The package includes a special command for testing your MCP tools without needing a real MCP client:
# Test a specific tool interactively php bin/console mcp:test-tool MyCustomTool # List all available tools php bin/console mcp:test-tool --list # Test with specific JSON input php bin/console mcp:test-tool MyCustomTool --input='{"param1":"value"}'
This helps you rapidly develop and debug tools by:
- Showing the tool's input schema and validating inputs
- Executing the tool with your provided input
- Displaying formatted results or detailed error information
- Displaying progress notifications for streaming tool
- Supporting complex input types including objects and arrays
For deep diving into tools creation please take a look at dedicated documentation Here
Creating and Adding Custom Prompts
The package provides convenient commands to generate new prompts:
php bin/console make:mcp-prompt MyCustomPrompt
This command:
- Handles various input formats (spaces, hyphens, mixed case)
- Automatically converts the name to the proper kebab-case format
- Creates a properly structured prompt class in
src/MCP/Prompts
- Offers to automatically register the prompt in your configuration
You can also manually create and register prompts in config/packages/klp_mcp_server.yaml
:
use KLP\KlpMcpServer\Services\PromptService\PromptInterface; use KLP\KlpMcpServer\Services\PromptService\Message\TextPromptMessage; class MyCustomPrompt implements PromptInterface { // Prompt implementation }
Testing MCP Prompts
The package includes a command for testing your MCP prompts without needing a real MCP client:
# Test a specific prompt interactively php bin/console mcp:test-prompt MyCustomPrompt # List all available prompts php bin/console mcp:test-prompt --list # Test with specific arguments php bin/console mcp:test-prompt MyCustomPrompt --arguments='{"topic":"AI","tone":"professional"}'
This helps you rapidly develop and debug prompts by:
- Showing the prompt's argument schema and validating inputs
- Executing the prompt with your provided arguments
- Displaying formatted message results with proper role assignments
- Supporting complex argument types including objects and arrays
- Demonstrating multi-modal content (text, images, audio, resources)
For deep diving into prompts creation please take a look at dedicated documentation Here
Visualizing with Inspector
You can also use the Model Context Protocol Inspector to visualize and test your MCP tools:
# Run the MCP Inspector without installation
npx @modelcontextprotocol/inspector node build/index.js
This will typically open a web interface at localhost:6274
. To test your MCP server:
-
Warning:
symfony server:start
CANNOT be used with this package because it cannot handle multiple PHP connections simultaneously. Since MCP SSE requires processing multiple connections concurrently, you must use one of these alternatives:- Nginx + PHP-FPM
- Apache + PHP-FPM
- Custom Docker setup
- Any web server that properly supports SSE streaming
-
In the Inspector interface, chose the protocol and enter the corresponding endpoint url
MCP Specification version | Connection Url pattern |
---|---|
2024-11-05 (SSE) | http(s)://[your-web-server]/[default_path]/sse |
2025-03-26 (Streamable HTTP) | http(s)://[your-web-server]/[default_path] |
default_path is defined in your config/packages/klp_mcp_server.yaml file. |
- Connect and explore available items visually
Advanced Features
Pub/Sub Architecture with Adapters
The package implements a publish/subscribe (pub/sub) messaging pattern through its adapter system:
-
Publisher (Server): When clients send requests (e.g.
/messages
endpoint for SSE connection), the server processes these requests and publishes responses through the configured adapter. -
Message Broker (Adapter): The adapter maintains message queues for each client, identified by unique client IDs. This provides a reliable asynchronous communication layer.
-
Subscriber (SSE Connection): Long-lived SSE connections subscribe to messages for their respective clients and deliver them in real-time.
This architecture enables:
- Scalable real-time communication
- Reliable message delivery even during temporary disconnections
- Efficient handling of multiple concurrent client connections
- Potential for distributed server deployments
Redis Adapter Configuration (Optional)
A Redis adapter can be configured as follows:
klp_mcp_server: # ... sse_adapter: 'redis' adapters: redis: prefix: 'mcp_sse_' # Prefix for Redis keys host: 'localhost' # Change it as needed ttl: 100 # Message TTL in seconds
Resources
The package provides a flexible resource management system that allows you to store and retrieve resources from different providers (file system, database, etc.).
Configuration
Configure resources in your config/packages/klp_mcp_server.yaml
file:
klp_mcp_server: # ... resources: - App\MCP\Resources\MyCustomResource resources_templates: - App\MCP\Resources\MyCustomResourceTemplate
Usage
Creating Custom Resource
use KLP\KlpMcpServer\Services\ResourceService\ResourceInterface; class MyCustomResource implements ResourceInterface { // Resource implementation }
Then register your resource in the configuration:
klp_mcp_server: # ... resources: - App\MCP\Resources\MyCustomResource
Creating Custom Resource Template
You can create custom resource templates by implementing the ResourceTemplateInterface
:
use KLP\KlpMcpServer\Services\ResourceService\ResourceTemplateInterface; class MyCustomResourceTemplate implements ResourceTemplateInterface { // Implement the required methods }
Then register your resource template in the configuration:
klp_mcp_server: # ... resources_templates: - App\MCP\Resources\MyCustomResourceTemplate
For deep diving into resources' management, please take a look at dedicated documentation Here
MCP Registries referencing
https://mcpreview.com/mcp-servers/klapaudius/symfony-mcp-server
https://mcp.so/server/symfony-mcp-server/klapaudius
Credits
- Boris AUBE and all contributors
- Inspired by OP.GG/laravel-mcp-server
License
This project is distributed under the MIT license.