Skip to content

Pass the middlware auth user id into the Tool #73

@nadar

Description

@nadar

I have installed the dev-main because of middlware function, because we need auth in order to change the tools response.

d2c5833

Using StreamableHttpServerTransport with PSR-7 middleware that sets:

class AuthMiddleware
{
    public function __invoke(ServerRequestInterface $request, callable $next)
    {
        // here how can i access this user_id inside a tool?
        $request = $request->withAttribute('user_id', $mySuperDuperUserId);

        $result = $next($request);
        
        return match (true) {
            $result instanceof PromiseInterface => $result->then(fn($response) => $this->handle($response)),
            $result instanceof ResponseInterface => $this->handle($result),
            default => $result
        };
    }

What’s the supported approach to read this user_id inside a Tool handler?

Constraints

  • (a) Constructor/DI injection won’t work because the PSR-7 request is per-request and can’t be bound at configuration time.
  • (b) Adding a ServerRequestInterface parameter to the handler also won’t work because it becomes part of the Tool’s schema (clients would have to pass it).

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