|
2 | 2 |
|
3 | 3 | import json |
4 | 4 | import logging |
| 5 | +from urllib.parse import urlparse |
| 6 | + |
5 | 7 | from fastapi import Request |
6 | 8 |
|
| 9 | +from configuration import AppConfig |
| 10 | + |
| 11 | + |
7 | 12 | logger = logging.getLogger("app.endpoints.dependencies") |
8 | 13 |
|
9 | 14 |
|
@@ -46,3 +51,40 @@ def extract_mcp_headers(request: Request) -> dict[str, dict[str, str]]: |
46 | 51 | ) |
47 | 52 | mcp_headers = {} |
48 | 53 | return mcp_headers |
| 54 | + |
| 55 | + |
| 56 | +def handle_mcp_headers_with_toolgroups( |
| 57 | + mcp_headers: dict[str, dict[str, str]], config: AppConfig |
| 58 | +) -> dict[str, dict[str, str]]: |
| 59 | + """Process MCP headers by converting toolgroup names to URLs. |
| 60 | +
|
| 61 | + This function takes MCP headers where keys can be either valid URLs or |
| 62 | + toolgroup names. For valid URLs (HTTP/HTTPS), it keeps them as-is. For |
| 63 | + toolgroup names, it looks up the corresponding MCP server URL in the |
| 64 | + configuration and replaces the key with the URL. Unknown toolgroup names |
| 65 | + are filtered out. |
| 66 | +
|
| 67 | + Args: |
| 68 | + mcp_headers: Dictionary with keys as URLs or toolgroup names |
| 69 | + config: Application configuration containing MCP server definitions |
| 70 | +
|
| 71 | + Returns: |
| 72 | + Dictionary with URLs as keys and their corresponding headers as values |
| 73 | + """ |
| 74 | + converted_mcp_headers = {} |
| 75 | + |
| 76 | + for key, item in mcp_headers.items(): |
| 77 | + key_url_parsed = urlparse(key) |
| 78 | + if key_url_parsed.scheme in ("http", "https") and key_url_parsed.netloc: |
| 79 | + # a valid url is supplied, deliver it as is |
| 80 | + converted_mcp_headers[key] = item |
| 81 | + else: |
| 82 | + # assume the key is a toolgroup name |
| 83 | + # look for toolgroups name in mcp_servers configuration |
| 84 | + # if the mcp server is not found, the mcp header gets ignored |
| 85 | + for mcp_server in config.mcp_servers: |
| 86 | + if mcp_server.name == key and mcp_server.url: |
| 87 | + converted_mcp_headers[mcp_server.url] = item |
| 88 | + break |
| 89 | + |
| 90 | + return converted_mcp_headers |
0 commit comments