Skip to content

[FEATURE] Enable Resources for MCP server #16

@dbschmigelski

Description

@dbschmigelski

Problem Statement

Add MCP resource operations to MCPClient. The Model Context Protocol supports resource operations that allow servers to expose data and content that can be read by clients. This feature will enable Strands SDK users to interact with MCP resources.

Proposed Solution

Implement three new synchronous methods in the MCPClient class:

  1. list_resources_sync(pagination_token) - List available resources from the MCP server
  2. read_resource_sync(uri) - Read specific resource content by URI
  3. list_resource_templates_sync(pagination_token) - List resource templates with URI patterns

Implementation Status

Branch: 1117

Completed:

  • ✅ Implementation of three resource methods in src/strands/tools/mcp/mcp_client.py
  • ✅ Comprehensive unit tests in tests/strands/tools/mcp/test_mcp_client.py
  • ✅ Proper error handling and session validation
  • ✅ Pagination support
  • ✅ Type hints and documentation

Remaining Work:

  1. Resolve Merge Conflicts

    • Conflict in tests/strands/tools/mcp/test_mcp_client.py
    • Main branch added tests for metadata and error handling
    • Branch 1117 added resource operation tests
    • Resolution: Merge main into 1117, preserve all tests from both branches
  2. Add Integration Tests

    • Currently only unit tests exist (with mocks)
    • Need end-to-end tests with actual MCP server
    • Must test all three operations: list, read, templates
    • Should test pagination, error handling, and resource templates
    • Location: tests_integ/mcp/test_mcp_client.py

Implementation Requirements

Files to Modify

  1. tests/strands/tools/mcp/test_mcp_client.py (merge conflict resolution)

    • Merge main branch changes
    • Keep both sets of tests
    • Ensure no test functionality is lost
  2. tests_integ/mcp/test_mcp_client.py (add integration tests)

    • Add @mcp.resource decorators to start_comprehensive_mcp_server()
    • Create test resources (text files, JSON, CSV examples)
    • Add resource templates with URI patterns
    • Implement test_mcp_client_resources() function
    • Implement test_mcp_client_resources_error_handling() function
    • Remove TODO comment at lines 145-147

Integration Test Requirements

Test Coverage Must Include:

  • ✅ List all available resources
  • ✅ Read specific resource by URI (string and AnyUrl)
  • ✅ List resource templates
  • ✅ Use resource templates with parameters
  • ✅ Pagination support (if server supports it)
  • ✅ Error handling for invalid URIs
  • ✅ Verify resource content matches expectations
  • ✅ Test multiple resource types (text, JSON, CSV)

Test Server Requirements:

  • At least 3 different resources with different URIs
  • At least 1 resource template with URI pattern
  • Different content types (plain text, JSON, CSV)
  • Should integrate with existing SSE/streamable-http transport

Acceptance Criteria

  • Merge conflicts in test file are resolved
  • Main branch is merged into branch 1117
  • All unit tests pass (hatch test)
  • Integration tests are implemented
  • Integration tests pass (hatch run test-integ)
  • Code follows style guide (hatch fmt --formatter and hatch fmt --linter)
  • All three resource operations work end-to-end with test server
  • Documentation is clear and follows existing patterns
  • Error handling is tested

Technical Notes

Development Commands:

# Format code
hatch fmt --formatter

# Run linting
hatch fmt --linter

# Run unit tests
hatch test

# Run integration tests
hatch run test-integ

MCP Server Resource Pattern (FastMCP):

from mcp.server import FastMCP
mcp = FastMCP("Server Name")

@mcp.resource(uri="file://path/to/resource")
def resource_function() -> str:
    return "resource content"

@mcp.resource(uri="file://path/{param}")
def resource_template(param: str) -> str:
    return f"resource with {param}"

Use Case

Enable Strands SDK users to:

  • Discover available resources from MCP servers
  • Read resource content by URI
  • Use resource templates for dynamic resource access
  • Integrate MCP resource data into agent workflows

Additional Context

This feature aligns with the Model Context Protocol specification for resources. The implementation follows existing patterns in the MCPClient class for tools and prompts, maintaining consistency in the SDK's API design.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions