8000 feat: add authentication context to resources and resource templates by rclayton-godaddy · Pull Request #137 · punkpeye/fastmcp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add authentication context to resources and resource templates #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally s 8000 end you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

rclayton-godaddy
Copy link
@rclayton-godaddy rclayton-godaddy commented Jun 26, 2025

This commit enhances the FastMCP framework by passing authentication context to resource and resource template load functions, enabling resources to access user-specific data based on authentication state.

Changes:

Core Implementation:

  • Add generic type parameter T extends FastMCPSessionAuth to Resource and ResourceTemplate types
  • Update Resource.load() signature to accept optional auth?: T parameter
  • Update ResourceTemplate.load() signature to accept both args and optional auth?: T parameter
  • Pass this.#auth to resource load functions in setupResourceHandlers()
  • Update type definitions throughout to maintain type safety with generics

Tests:

  • Add "provides auth to resources" test to verify auth context is passed to resource.load()
  • Add "provides auth to resource templates" test to verify auth context is passed with template args
  • Add "provides auth to resource templates returning arrays" test to verify auth works with array returns
  • All tests follow existing patterns using vi.fn() mocks and HTTP Stream setup

Breaking Changes:

None - the auth parameter is optional, maintaining backward compatibility

Use Cases:

  • Resources can now filter or customize content based on user permissions
  • Resource templates can generate user-specific content
  • Authentication state can be used for access control within resources

🤖 Generated with Claude Code

This commit enhances the FastMCP framework by passing authentication context to resource and resource template load functions, enabling resources to access user-specific data based on authentication state.

## Changes:

### Core Implementation:
- Add generic type parameter `T extends FastMCPSessionAuth` to `Resource` and `ResourceTemplate` types
- Update `Resource.load()` signature to accept optional `auth?: T` parameter
- Update `ResourceTemplate.load()` signature to accept both args and optional `auth?: T` parameter
- Pass `this.#auth` to resource load functions in `setupResourceHandlers()`
- Update type definitions throughout to maintain type safety with generics

### Type System Fixes:
- Fix TypeScript error TS2769 by casting session objects to `FastMCPSession<FastMCPSessionAuth>` in emit calls
- Remove duplicate `Authenticate<T>` type definition
- Ensure consistent generic type constraints across the codebase

### Tests:
- Add "provides auth to resources" test to verify auth context is passed to resource.load()
- Add "provides auth to resource templates" test to verify auth context is passed with template args
- Add "provides auth to resource templates returning arrays" test to verify auth works with array returns
- All tests follow existing patterns using vi.fn() mocks and HTTP Stream setup

## Breaking Changes:
None - the auth parameter is optional, maintaining backward compatibility

## Use Cases:
- Resources can now filter or customize content based on user permissions
- Resource templates can generate user-specific content
- Authentication state can be used for access control within resources

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@punkpeye
Copy link
Owner

Thank you for your contribution. Please ensure that all checks are passing.

@rclayton-godaddy
Copy link
Author

Many apologies, fixed the lint issues. Did you have any issues with the changes?

… load

- Make ArgumentValueCompleter generic to accept optional auth parameter
  - Changed signature from (value: string) => Promise<Completion>
  - To: (value: string, auth?: T) => Promise<Completion>

- Update Prompt load method to accept optional auth parameter
  - Changed signature to include auth?: T parameter
  - Pass auth context when calling prompt.load()

- Propagate generic type parameter T through all related types:
  - InputPromptArgument<T>
  - Prompt<T>
  - PromptArgument<T>
  - ResourceTemplate<T>
  - ResourceTemplateArgument<T>

- Update complete functions to pass auth context to completers
  - Prompt argument completers now receive auth
  - Resource template argument completers now receive auth

- Fix TypeScript errors by casting session to FastMCPSession<FastMCPSessionAuth>
  in emit calls to ensure type safety with StrictEventEmitter

- Add comprehensive test coverage for auth context propagation:
  - Auth passed to resources during load
  - Auth passed to resource templates during load
  - Auth passed to resource templates returning arrays
  - Auth passed to prompt argument completion functions
  - Auth passed to prompt load functions
  - Auth passed to resource template argument completion functions

- Update eslint config to ignore dist/** directory

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@rclayton-godaddy
Copy link
Author

@punkpeye - I expanded the availability of auth to prompts and argument loaders. I'm building an MCP that needs these capabilities. Let me know if it's too much, I'd be happy to back out the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0