Skip to content

Conversation

@strawgate
Copy link
Collaborator

Enhancement

Adds list_resources(), list_prompts(), and get_prompt() methods to the Context class to improve API consistency.

Previously, the Context object had read_resource() but lacked list_resources() and list_prompts() methods. This created an inconsistency where middleware/tools needed to create temporary Client instances to list resources/prompts but could use context methods to read/get them.

Changes

  • Added Context.list_resources() to list all available resources
  • Added Context.list_prompts() to list all available prompts
  • Added Context.get_prompt() to get a specific prompt with arguments
  • Updated ToolInjectionMiddleware to use new Context methods instead of creating temporary Client instances
  • Removed unused Client and FastMCPTransport imports

These methods follow the same pattern as read_resource() and delegate to the FastMCP server's internal MCP methods.

Example

@server.tool
async def my_tool(ctx: Context) -> str:
    # List available resources
    resources = await ctx.list_resources()
    
    # List available prompts
    prompts = await ctx.list_prompts()
    
    # Get a specific prompt
    result = await ctx.get_prompt("my_prompt", {"arg": "value"})
    
    return "Done"

Fixes #2245


🤖 Generated with Claude Code

- Add Context.list_resources() to list all available resources
- Add Context.list_prompts() to list all available prompts
- Add Context.get_prompt() to get a specific prompt with arguments
- Update ToolInjectionMiddleware to use new Context methods instead of creating temporary Client instances
- Remove unused Client and FastMCPTransport imports from tool_injection.py

This improves API consistency by allowing middleware/tools to use Context methods directly without needing to create temporary Client instances.

Fixes #2245

Co-authored-by: William Easton <[email protected]>
@strawgate strawgate requested a review from Copilot October 24, 2025 22:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds three new methods to the Context class (list_resources(), list_prompts(), and get_prompt()) to improve API consistency and eliminate the need for middleware to create temporary Client instances. The change streamlines the API by allowing all resource and prompt operations to be performed directly through the Context object.

Key changes:

  • Added list_resources(), list_prompts(), and get_prompt() methods to the Context class
  • Updated ToolInjectionMiddleware to use the new Context methods instead of creating temporary Client instances
  • Removed unused Client and FastMCPTransport imports from the middleware

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/fastmcp/server/context.py Added three new methods to Context class for listing resources, listing prompts, and getting prompts with proper error handling and documentation
src/fastmcp/server/middleware/tool_injection.py Replaced temporary Client instance creation with direct Context method calls and removed unused imports

@marvin-context-protocol marvin-context-protocol bot added enhancement Improvement to existing functionality. For issues and smaller PR improvements. server Related to FastMCP server implementation or server-side functionality. labels Oct 24, 2025
@strawgate
Copy link
Collaborator Author

/marvin the ctx.fastmcp property can't return none anyway

@marvin-context-protocol
Copy link
Contributor

marvin-context-protocol bot commented Oct 24, 2025

Claude finished @strawgate's task —— View job


Task completed

  • Review the code to find unnecessary None checks for ctx.fastmcp
  • Remove unnecessary None checks
  • Run validation (pre-commit, tests)
  • Commit and push changes

Removed unnecessary None checks from these methods:

  • list_resources()
  • list_prompts()
  • get_prompt()
  • read_resource()

The fastmcp property raises RuntimeError if the FastMCP instance is None, so these checks were redundant.

All tests and linting passed. Changes have been committed and pushed.

--- |

@jlowin jlowin merged commit 1e5776f into main Oct 24, 2025
10 checks passed
@jlowin jlowin deleted the claude/issue-2245-20251024-2225 branch October 24, 2025 23:24
@jlowin
Copy link
Owner

jlowin commented Oct 24, 2025

🤦 sorry marvin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to existing functionality. For issues and smaller PR improvements. server Related to FastMCP server implementation or server-side functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add list_resources and list_prompts methods to Context

3 participants