Skip to content

[Server] Handler context #108

Description

@soyuka

Is your feature request related to a problem? Please describe.

I'd like to implement a generic handler, for this I'm registering programmatically a few tools and resources. Basically a tool gets registered like this:

yield [
    'type' => 'resource',
    'definition' => [
        'handler' => self::HANDLER,
        'uri' => $uri,
        'name' => $mcpName,
        'description' => $operation->getDescription(),
    ],
];

// later we decorate the server builder and add them

I'd like for the handler to be aware of a few of these parameters. Currently the CallToolHandler does:

$toolName = $request->name;
$arguments = $request->arguments ?? [];

$reference = $this->referenceProvider->getTool($toolName);
$result = $this->referenceHandler->handle($reference, $arguments);

Describe the solution you'd like

I'm wondering if there would be ways to be aware of a given static context.

Describe alternatives you've considered

  1. Implement my own reference provider (I'll do this as I can return a wrapper around my handler with the toolName which is sufficient (for now)) edit: [Server] Add getRegistry() method to Server class for debugging access #75 registry isn't available therefore hard to hook on that
  2. Add an interface ContextAwareHandlerInterface, userland can implement this and the sdk calls withContext on the handler
  3. Use a closure as a handler that has use ($toolName) and then to call the handler with that argument and the actual arguments of the tool (closures are hard to cache for now and need to be hold in memory at rutime, opposed to an array-callable)
  4. declare one service per toolName (which overloads the container for no reason)

Additional context

As I needed that I thought that maybe others will. I'm willing to contribute to a patch if one of the proposed alternatives is considered.

Thanks for reading.

Activity

  1. changed the title [-]Handler context[/-] [+][Server] Handler context[/+] on Oct 20, 2025
  2. added
    ServerIssues & PRs related to the Server component
    on Oct 20, 2025
  3. chr-hertel commented on Oct 20, 2025

    @chr-hertel
    Member

    Is context only toolName or basically the entire Tool & Request object?

    Can you check if partially reverting #100 would help with your situation?
    cc @CodeWithKyrian

  4. soyuka commented on Oct 24, 2025

    @soyuka
    ContributorAuthor

    Probably that #111 helps with that I'll report back once I'm done with that part.

  5. CodeWithKyrian commented on Oct 25, 2025

    @CodeWithKyrian
    Contributor

    Still doesn't does it?

  6. soyuka commented on Oct 25, 2025

    @soyuka
    ContributorAuthor

    With the registry loader I now have access to the registry it may help I need to try this.

  7. chr-hertel commented on Jan 23, 2026

    @chr-hertel
    Member

    Hey @soyuka, can we close here? your use case covered or still sth to follow up on?

  8. soyuka commented on Jan 24, 2026

    @soyuka
    ContributorAuthor

    Completely resolved! For anyone interested in how I did check out api-platform/core#7595

  9. rvanlaak commented on Jan 24, 2026

    @rvanlaak

    Exciting, congrats! Any ETA when a RC of APIP 4.3 could be expected?

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

    ServerIssues & PRs related to the Server component

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions