Skip to content

[Server] Provide custom injection functionality #167

Description

@luoyue712

Is your feature request related to a problem? Please describe.
My goal is to apply McpTool annotations to controllers, but the framework uses custom injection, similar to how ClientGateway is injected in the SDK, which prevents it from working in controllers.This type of injection typically does not occur in the Container, as it is associated with the request.

Describe the solution you'd like
Provide custom injection functionality and expose it in Builder.

Describe alternatives you've considered
Rewrite the prepareArguments method and set the ReferenceHandler in the Builder.

Additional context
sample code:

<?php
namespace app\controller;

use Mcp\Capability\Attribute\McpTool;
use support\Request;

class UserController
{
    #[McpTool(name: 'hello')]
    public function hello(Request $request)
    {
        $default_name = 'webman';
        $name = $request->get('name', $default_name);
        return 'hello ' . $name;
    }
}

Activity

  1. changed the title [-][server]Provide custom injection functionality[/-] [+][server] Provide custom injection functionality[/+] on Nov 23, 2025
  2. changed the title [-][server] Provide custom injection functionality[/-] [+][Server] Provide custom injection functionality[/+] on Nov 23, 2025
  3. Nyholm commented on Nov 30, 2025

    @Nyholm
    Contributor

    The Server can be built with a PSR-11 container. If you build that container yourself (or use the one from your framework), we could allow it to detect a way to inject services in the tool like that.

    I need to think how this will play with example #161

  4. Nyholm commented on Dec 27, 2025

    @Nyholm
    Contributor

    Since 0.2.0 you may use RequestContext to get the request.

    Note that the RequestContext is bound to a request, it should not be added to the class. If you want to inject other services, feel free to use any PSR-11 container.

    <?php
    namespace app\controller;
    
    use Mcp\Capability\Attribute\McpTool;
    use Mcp\Server\RequestContext;
    
    class UserController
    {
        #[McpTool(name: 'hello')]
        public function hello(RequestContext $context)
        {
            $request = $context->getRequest();
            $default_name = 'webman';
            $name = $request->get('name', $default_name);
            return 'hello ' . $name;
        }
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions