Skip to content

Conversation

@jamesev15
Copy link

Motivation and Context

Add include and exclude parameters to the list_tools request to enable programmatic filtering of tools on the server side. This enhancement addresses a key limitation where MCP clients must retrieve all available tools from a server and then filter them client side, leading to unnecessary data transfer and larger context windows for LLMs.

While MCP compliant UIs like Claude Desktop or Continue(VS Code) provide manual tool selection, programmatic use cases (such as LangChain integrations) would benefit from server side filtering capabilities. This is particularly important for LLM applications where reducing the number of tools in the context helps minimize token usage and improves model performance by avoiding overwhelming the LLM with non used tools.

For example, when using MCP servers with LangChain:

# Current approach - gets ALL tools
# Create server parameters for stdio connection
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

from langchain_mcp_adapters.tools import load_mcp_tools
from langgraph.prebuilt import create_react_agent

server_params = StdioServerParameters(...)

async with stdio_client(server_params) as (read, write):
    async with ClientSession(read, write) as session:
        # Initialize the connection
        await session.initialize()

        # Get tools
        tools = await load_mcp_tools(session)

        # Create and run the agent
        agent = create_react_agent("openai:gpt-4.1", tools)
        ....


# Desired approach, get only specific tools
# This would be possible with server side filtering, but it could be done with this PR as well.

How Has This Been Tested?

  • Added comprehensive unit tests for the new filtering functionality in ToolManager
  • Tested both include and exclude parameter scenarios
  • Verified error handling for non existent tool names
  • Tested mutual exclusivity of include and exclude parameters
  • Validated backward compatibility with existing list_tools implementations

Breaking Changes

None. This is a backward compatible addition. Existing clients that don't use the new parameters will continue to work unchanged, receiving all available tools as before.

Types of changes

  • New feature (non breaking change which adds functionality)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling

Additional context

The implementation follows the existing MCP patterns:

  • Uses PaginatedRequestParams as the base for ListToolsRequest to include the new filtering parameters
  • Provides clear error messages when specified tools don't exist
  • Ensures mutual exclusivity between include and exclude to prevent ambiguous requests

@maxisbey maxisbey marked this pull request as draft October 23, 2025 04:44
@maxisbey
Copy link
Contributor

maxisbey commented Oct 23, 2025

Thank you for submitting the PR, I appreciate the effort to try and get some new functionality in! Unfortunately, as this changes the underlying MCP specification itself it would first require an SEP to be submitted and accepted into the protocol specification before we can make any changes to the SDK.

There is an existing SEP with a proposal for tool filtering being discussed which you're welcome to add your opinions on if you'd like here: modelcontextprotocol/modelcontextprotocol#1300

As the changes in this PR would require a whole new SEP to be submitted/accepted before it could be merged, and are already quite different to the current leading SEP (linked above) I'm gong to close the PR for now.

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