Skip to content

[FEATURE] Support for multiple MCP servers (and loading from config file) #482

@jer96

Description

@jer96

Problem Statement

I would like strands to support multiple MCP servers. One of the mechanisms to provide this may be loading from a config file.

As outlined in the parent issue #198 the DX for adding MCP tools to an agent can be improved, alongside these improvements it would be great to configure multiple MCP servers and have the lifecycle of client connections be managed automatically.


Refined Requirements

Based on repository analysis and community feedback, here's the refined scope:

Current State ✅

Multiple MCP servers and lifecycle management are already supported programmatically:

from strands import Agent
from strands.tools.mcp import MCPClient
from mcp.client.stdio import stdio_client, StdioServerParameters

client1 = MCPClient(lambda: stdio_client(StdioServerParameters(command="uvx", args=["server1"])), prefix="server1")
client2 = MCPClient(lambda: stdio_client(StdioServerParameters(command="uvx", args=["server2"])), prefix="server2")

agent = Agent(tools=[client1, client2])

Gap ❌

Configuration file support for MCP servers - Users must currently define MCP clients programmatically. The experimental agent_config.py does not support MCP configuration.


Proposed Solution

Extend src/strands/experimental/agent_config.py to support MCP server configuration.

Configuration Format

Use JSON format (matching existing agent_config.py pattern):

{
  "name": "my_agent",
  "model": "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
  "prompt": "You are a helpful assistant.",
  "tools": ["strands_tools.file_read"],
  "mcp_servers": [
    {
      "name": "aws_docs",
      "transport": "stdio",
      "command": "uvx",
      "args": ["awslabs.aws-documentation-mcp-server@latest"],
      "env": {},
      "prefix": "aws",
      "startup_timeout": 30,
      "tool_filter": {
        "allowed": ["search_.*", "get_docs"],
        "rejected": ["dangerous_tool"]
      }
    },
    {
      "name": "local_agent",
      "transport": "sse",
      "url": "http://localhost:8000/sse",
      "headers": {
        "Authorization": "Bearer ${MCP_AUTH_TOKEN}"
      },
      "timeout": 300,
      "prefix": "local"
    },
    {
      "name": "remote_service",
      "transport": "streamable-http",
      "url": "https://api.example.com/mcp",
      "headers": {
        "X-API-Key": "${API_KEY}"
      }
    }
  ]
}

Transport Types

Transport Required Fields Optional Fields
stdio command args, env, cwd
sse url headers, timeout
streamable-http url headers, timeout

Strands-Specific Options (per server)

Option Type Description
name string Server identifier (required)
prefix string Prefix for tool names to avoid collisions
startup_timeout int Timeout for server initialization (default: 30)
tool_filter object Filter which tools to load (allowed/rejected patterns)

Features

  1. Environment Variable Interpolation: Support ${VAR_NAME} syntax for secrets
  2. Per-Server Headers: Allow different auth headers per MCP server
  3. Tool Filtering: Per-server tool allow/reject lists (supports regex patterns)
  4. Graceful Failures: Opt-in behavior to continue if some servers fail (see [FEATURE] Graceful MCP server startup failures - fail open #1481)

Dependencies


Use Cases

  1. Config-driven agents: Load agent configuration including MCP servers from a single JSON file
  2. Multi-server setups: Seamlessly interact with multiple MCP servers (AWS docs, local tools, remote services)
  3. Environment-specific configs: Different MCP servers per environment using env var interpolation
  4. Selective tool loading: Filter which tools to expose from each server

Implementation Approach

  1. Extend AGENT_CONFIG_SCHEMA in agent_config.py to include mcp_servers property
  2. Create helper to parse MCP config and instantiate MCPClient instances
  3. Support stdio transport first, then add SSE and streamable-http
  4. Add validation for transport-specific required fields
  5. Implement environment variable interpolation for sensitive values

Acceptance Criteria

  • JSON configuration file can define multiple MCP servers
  • All three transport types supported (stdio, sse, streamable-http)
  • Per-server options work: prefix, startup_timeout, tool_filter, headers
  • Environment variable interpolation works for secrets
  • Integration with [FEATURE] Graceful MCP server startup failures - fail open #1481 for graceful failure handling
  • Documentation with examples
  • Unit and integration tests

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions