MCP tool filtering and tool search
A key with access to many MCP servers can see hundreds of tools. Sending them all on every request costs context, slows the model down and makes it pick badly. The gateway offers two remedies. The semantic tool filter embeds the user's message and sends the model only the tools that match it. Tool search goes further and replaces the catalogue with a handful of virtual tools through which the model searches for and calls tools on demand.
A real-world scenario
An engineering assistant's key can reach twelve MCP servers with about three hundred tools between them. Every request was carrying all of them, so answers were slow and the model kept picking a Confluence tool when a Jira one was wanted. The admin turns on the semantic filter with top_k: 10. A question about a failing deployment now travels with the ten deployment and incident tools that match it, and the response headers show which ones were kept. For the team's autonomous agent the admin turns on tool search instead, so the agent sees four tools, searches for what it needs each turn and calls it by name.
Semantic tool filter
The filter is a pre-call hook on /chat/completions and /v1/responses. When a request expands MCP servers into tools, the hook embeds the latest user message and every tool description with the configured embedding model, ranks the tools by cosine similarity, and keeps the top top_k above similarity_threshold. The response carries an x-spanway-semantic-filter header with how many tools there were and how many were kept, written as total->kept, and an x-spanway-semantic-filter-tools header listing the tools that were kept, cut to 150 characters.
-
Make sure an embedding model is in
model_list. In the Admin UI, open MCP Servers and the Semantic Filter tab. Turn on Enable Semantic Filtering, pick the Embedding Model, set Top K Results and the Similarity Threshold, and click Save Settings. Every instance reloads the settings within about ten seconds. -
Or set it in
config.yaml, or at runtime with the API:spanway_settings:mcp_semantic_tool_filter:enabled: trueembedding_model: text-embedding-3-smalltop_k: 10similarity_threshold: 0.3curl -X PATCH http://localhost:4000/update/mcp_semantic_filter_settings \-H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \-d '{"enabled": true, "embedding_model": "text-embedding-3-small", "top_k": 10, "similarity_threshold": 0.3}' -
Send a request that expands an MCP server into tools and look at the response headers:
curl -i -X POST http://localhost:4000/v1/responses \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"model": "claude-sonnet-5-5", "input": "Why did the checkout deployment fail last night?", "tools": [{"type": "mcp", "server_label": "ops", "server_url": "spanway_proxy/mcp/ops", "require_approval": "never"}]}'Expected result, among the response headers:
x-spanway-semantic-filter: 312->10x-spanway-semantic-filter-tools: ops-get_deployment,ops-list_incidents,ops-get_pipeline_run,... -
The Test Configuration card on the Semantic Filter tab does the same from the Admin UI. Enter a Test Query, pick a model and click Test Filter. It reads "N of M tools selected" and lists them.
| Key | Default | Meaning |
|---|---|---|
enabled | false | Turn the hook on. |
embedding_model | text-embedding-3-small | A model group from model_list used for embeddings. The default can be changed with the DEFAULT_MCP_SEMANTIC_FILTER_EMBEDDING_MODEL environment variable. |
top_k | 10 | Most tools kept per request. |
similarity_threshold | 0.3 | Lowest similarity a tool needs to be kept. |
| Method | Path | Summary |
|---|---|---|
| GET | /get/mcp_semantic_filter_settings | Read the MCP semantic filter settings |
| PATCH | /update/mcp_semantic_filter_settings | Change the MCP semantic filter settings |
The defaults for top_k and similarity_threshold come from the DEFAULT_MCP_SEMANTIC_FILTER_TOP_K (10) and DEFAULT_MCP_SEMANTIC_FILTER_SIMILARITY_THRESHOLD (0.3) environment variables, and MAX_MCP_SEMANTIC_FILTER_TOOLS_HEADER_LENGTH (150) caps the tools header. The filter settings live under spanway_settings.mcp_semantic_tool_filter, see the settings reference.
Tool search
Tool search is for keys whose users ask broad questions over many connected servers. Instead of sending the model every tool it may call, the gateway sends two small virtual tools. mcp_tool_search takes a plain language query and an optional top_k and returns the matching tools with their names, descriptions and input schemas. mcp_tool_call runs one of those tools by its exact tool_name with arguments. The model looks up what it needs, calls it and answers, and a key that can reach fifty servers costs the model two tool definitions per turn instead of hundreds.
The two tools work on three surfaces. On /mcp, a tools/list from a key with tool search on returns the virtual tools (agent_search and skill_search join them there) instead of the catalogue. On /chat/completions and /v1/responses, a request that references the gateway as an MCP server gets the same two tools in place of the expanded catalogue, and the gateway runs them itself when the request allows automatic execution. The /mcp/proxy endpoint is a stricter variant for agents that should never see raw tool names and serves search_tools, get_tool_schema and call_tool with opaque tool ids.
Scenario
A support team has one key that can reach the ticketing, billing, calendar and wiki servers, about three hundred tools in all. Their assistant calls /chat/completions with server_url: spanway_proxy and the model keeps picking the wrong tool because the tool list fills most of its context. The team turns tool search on for that key and caps it at three results, so each question now costs two tool definitions plus the three tools the search finds.
Steps
- Open the Admin UI at
http://localhost:4000/ui/, go to Virtual Keys and click Create New Key (or open an existing key and click Edit Settings). - On Create New Key, in MCP Settings, pick the servers or access groups the key may use, then switch Tool search on. On Edit Settings the same control sits under the MCP Servers / Access Groups field. Leave Top K and Similarity threshold empty to use the global defaults, or set Top K to
3to cap the results for this key. - Optional, for semantic ranking: go to MCP Servers, open the Tool Search tab and pick an embedding model from your
model_list. Without one the search ranks by keyword. - Save the key. The same switch and overrides exist on a team under Teams. Switching tool search on for a team turns it on for every key of that team. A key that leaves an override empty uses the team's override, and an override set on the key wins over the team's.
- Call chat completions with the gateway as an MCP server and automatic execution allowed.
curl -sS http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4.1",
"messages": [{"role": "user", "content": "Which open tickets mention refunds for order 4411?"}],
"tools": [{"type": "mcp", "server_url": "spanway_proxy", "require_approval": "never"}]
}'
The same works on the Responses API.
curl -sS http://localhost:4000/v1/responses \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4.1",
"input": "Which open tickets mention refunds for order 4411?",
"tools": [{"type": "mcp", "server_label": "spanway", "server_url": "spanway_proxy", "require_approval": "never"}]
}'
The equivalent without the Admin UI is one field on the key.
curl -sS http://localhost:4000/key/generate \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"key_alias": "support-assistant",
"object_permission": {
"mcp_servers": ["ticketing", "billing", "calendar", "wiki"],
"mcp_tool_search_enabled": true,
"mcp_tool_search_top_k": 3,
"mcp_tool_search_similarity_threshold": 0.2
}
}'
Expected result
The model receives only mcp_tool_search and mcp_tool_call. It searches for "refund tickets", the gateway answers from the catalogue the key may see, the model calls ticketing-search_issues through mcp_tool_call, and the final message quotes the tickets. On chat completions the message carries provider_specific_fields.mcp_list_tools with the two virtual tools, mcp_tool_calls with every virtual call the model made and mcp_call_results with the search results and the tool output. On the Responses API the output ends with an mcp_tools_fetched item listing the two virtual tools and a tool_execution_results item with the same results. Streaming requests stream each round, and mcp_list_tools arrives in the first chunk.
The gateway runs up to five rounds per request. If the model is still calling tools after the fifth round, the last follow-up is made without tools so the model answers in text. A tool_choice you send, such as required or a named function, applies to the first call only, so the follow-ups never force another tool call and the last one is accepted by the provider. A search returns at most the key's top_k, or the global top_k when the key has none, and a smaller top_k in the model's own call wins. A call that sends no top_k asks for 5, the default the tool advertises, so a cap above 5 only matters when the model asks for more. Core tools from the global settings are always returned first and do not count against the limit.
With require_approval set to anything other than never, the gateway still sends the two virtual tools but returns the model's calls to your application instead of running them. Run them yourself through POST /mcp-rest/tools/call with the name mcp_tool_search or mcp_tool_call, which honours the same key flag.
Settings
The global defaults live in spanway_settings.mcp_tool_search and on the Tool Search tab of the MCP Servers page.
spanway_settings:
mcp_tool_search:
embedding_model: text-embedding-3-small
top_k: 5
similarity_threshold: 0.2
core_tools: ["jira-search_issues"]
| Key | Default | Meaning |
|---|---|---|
embedding_model | unset | Model group used to rank tools by meaning. Unset keeps keyword matching. |
top_k | 5 | Most ranked tools a search returns. A smaller top_k in the tool call wins. |
similarity_threshold | 0.0 | Lowest cosine similarity for semantic results. |
core_tools | [] | Tool names always returned first when the caller may access them. They do not count towards top_k. |
| Method | Path | Summary |
|---|---|---|
| GET | /get/mcp_tool_search_settings | Read the MCP tool search settings |
| PATCH | /update/mcp_tool_search_settings | Change the MCP tool search settings |
A key or team overrides two of them in its object_permission.
| Field | Range | Meaning |
|---|---|---|
mcp_tool_search_enabled | true or false | Turns tool search on for the key or team. Off, the model gets the full catalogue. On for a team, it is on for every key of the team. |
mcp_tool_search_top_k | 1 to 100 | Most tools one search returns for this key or team. Empty keeps the global top_k. It applies whenever tool search is on for the key or its team, so a key can keep its own value inside a tool search team. |
mcp_tool_search_similarity_threshold | 0.0 to 1.0 | Lowest similarity a tool needs in semantic results for this key or team. Empty keeps the global threshold. |
Errors
| Where it shows | Text | Meaning and fix |
|---|---|---|
POST /key/generate, /key/update, /team/new, /team/update | HTTP 422, mcp_tool_search_top_k or mcp_tool_search_similarity_threshold out of range | The override is outside 1 to 100 or 0.0 to 1.0. Send a value in range or leave the field empty. |
| Tool result the model sees | Unknown tool <name>, use mcp_tool_search to find the exact name of a tool you can call | The model called mcp_tool_call with a name outside the catalogue the key may see. The model usually recovers by searching again. If it keeps happening, grant the server to the key or check allowed_tools on the request. |
| Tool result the model sees | mcp_tool_call needs a tool_name string, use mcp_tool_search to find one or mcp_tool_call arguments must be an object | The model sent a malformed call. Nothing to fix on the gateway. |
| Tool result the model sees | spanway_settings.mcp_tool_search is invalid: ... | The global settings in the config file or the key's stored overrides fail validation. Fix the value named in the message. |
| Tool result the model sees | spanway_settings.mcp_tool_search.embedding_model needs a model_list so it can be called | An embedding model is set but the gateway has no model_list. Add the model or unset embedding_model to fall back to keyword ranking. |
| Tool result the model sees | An embedding failure reason naming the embedding model | The embedding call failed or exceeded the model's context. Check the model's provider and the tool descriptions' size. |
PATCH /update/mcp_tool_search_settings | HTTP 403, Only proxy admins can update MCP tool search settings. | The key is not a proxy admin key. Use a proxy admin key or ask a proxy admin to change the settings. |
PATCH /update/mcp_tool_search_settings | HTTP 500, Set `'STORE_MODEL_IN_DB='True'` in your env to enable this feature. | The gateway does not store settings in the database. Set store_model_in_db: true in general_settings, or edit spanway_settings.mcp_tool_search in the config file and restart. |
PATCH /update/mcp_tool_search_settings | HTTP 422, a field such as top_k out of range | top_k must be 1 to 100 and similarity_threshold 0.0 to 1.0. Send a value in range. |
GET /get/mcp_tool_search_settings | HTTP 500, Database not connected. Please connect a database. | The gateway runs without a database. Set DATABASE_URL, or read the values from the config file. |
Logs
| Level | Line | Meaning |
|---|---|---|
| WARNING | mcp_tool_search settings are invalid, the virtual tools return the error: ... | spanway_settings.mcp_tool_search or a stored key or team override failed validation. Searches for the affected keys return the error text until it is fixed. A stored override is only out of range when the row was written outside the API, so fix that row. |
| INFO | mcp_tool_call asked for <name>, which the key cannot see | The model named a tool outside the key's catalogue. Normal while the model learns the names, action needed only if it repeats for every request. |
| WARNING | MCP auto-execute hit MAX_MCP_TOOL_CALL_ROUNDS=5; forcing a text-only follow-up. | The model was still calling tools after five rounds. The gateway made the last follow-up without tools. Normal for hard questions, check the tool results if it happens on every request. |
| DEBUG | mcp_tool_search on chat returned <n> of <m> tools | How many of the key's tools one search returned. Useful when tuning top_k and the threshold. |
The global defaults are edited on the MCP Servers page of the Admin UI, in the Semantic Filter and Tool Search settings panels. The per key and per team switch and overrides sit in MCP Settings of the create key and team forms, and under MCP Servers / Access Groups in a key's Edit Settings.
Every change is written to the audit log. A change on the Tool Search tab writes a config row named mcp_tool_search with the old and new values, and a change of a key's or team's switch or overrides shows the old and new object_permission values in that key's or team's updated row.
Errors
| Status | Where | Meaning | Fix |
|---|---|---|---|
| 400 naming the embedding model's context window | Filtered request | Embedding the tools or the message exceeded the embedding model's context window. The request is rejected rather than silently passing every tool through. | Use an embedding model with a larger window, or shorten tool descriptions with tool_name_to_description. |
| "No embedding models available" | Semantic Filter and Tool Search tabs | No model in model_list serves embeddings. | Add one on Models + Endpoints. |
No x-spanway-semantic-filter header | Filtered request | The request expanded no MCP server, or the filter is off. | Check enabled and that tools names a registered server. |
| Search returns nothing | mcp_tool_search | No tool the key may reach matches above similarity_threshold. | Lower the threshold, or grant the server, see access control. |
| Catalogue still returned to a key with tool search on | tools/list | The key asked for one server with x-mcp-servers, mcp_server_name or a toolset, which bypasses the virtual tools. | Connect to the aggregate /mcp endpoint. |
Related
- MCP access control for the
mcp_tool_search_enabledgrant. - MCP tools over REST for the list and call routes used above.
- Tool registry and tool policies for trust policies on the tools that are kept.
- The MCP Servers page for the Semantic Filter and Tool Search tabs.
- MCP overview.
- Toolsets for curated tool groups.
- Virtual keys for the key forms.