Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/mcpo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def main(
log_level: Annotated[
Optional[str], typer.Option("--log-level", help="Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)")
] = None,
client_header_forwarding: Annotated[
Optional[str], typer.Option("--client-header-forwarding", help="Config for header forwarding")
] = None
):
server_command = None
if not config_path:
Expand Down Expand Up @@ -114,7 +117,7 @@ def main(
# Set environment variables
for key, value in env_dict.items():
os.environ[key] = value
except Exception as e:
except Exception:
pass

# Whatever the prefix is, make sure it starts and ends with a /
Expand Down Expand Up @@ -152,6 +155,7 @@ def main(
root_path=root_path,
headers=headers,
hot_reload=hot_reload,
client_header_forwarding=client_header_forwarding
)
)

Expand Down
11 changes: 11 additions & 0 deletions src/mcpo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ async def run(

# MCP Config
config_path = kwargs.get("config_path")
client_header_forwarding = kwargs.get("client_header_forwarding", None)

# mcpo server
name = kwargs.get("name") or "MCP OpenAPI Proxy"
Expand Down Expand Up @@ -844,6 +845,15 @@ def filter(self, record):
logger.warning("Invalid JSON format for headers. Headers will be ignored.")
headers = None

client_header_forwarding = kwargs.get("client_header_forwarding")
if client_header_forwarding and isinstance(client_header_forwarding, str):
try:
client_header_forwarding = json.loads(client_header_forwarding)
validate_client_header_forwarding_config("default", client_header_forwarding)
except json.JSONDecodeError:
logger.warning("Invalid JSON format for client header forwarding config. Client header forwarding config will be ignored.")
client_header_forwarding = None

if server_type == "sse":
logger.info(
f"Configuring for a single SSE MCP Server with URL {server_command[0]}"
Expand All @@ -860,6 +870,7 @@ def filter(self, record):
main_app.state.args = server_command[0] # Expects URL as the first element
main_app.state.api_dependency = api_dependency
main_app.state.headers = headers
main_app.state.client_header_forwarding = client_header_forwarding
elif server_command: # This handles stdio
logger.info(
f"Configuring for a single Stdio MCP Server with command: {' '.join(server_command)}"
Expand Down
3 changes: 1 addition & 2 deletions src/mcpo/utils/headers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import re
from typing import Dict, List, Optional, Any
from typing import Dict, List, Any
from fastapi import Request

logger = logging.getLogger(__name__)
Expand Down
8 changes: 4 additions & 4 deletions src/mcpo/utils/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ def get_tool_handler(
client_header_forwarding_config=None,
):
async def call_tool_with_reconnect(
request: Request, arguments: Dict[str, Any]
request: Request, arguments: Dict[str, Any], meta: Dict[str, Any]
) -> CallToolResult:
session_manager = getattr(request.app.state, "session_manager", None)

async def _invoke(session):
return await session.call_tool(endpoint_name, arguments=arguments)
return await session.call_tool(endpoint_name, arguments=arguments, meta=meta)

if session_manager:
try:
Expand Down Expand Up @@ -349,7 +349,7 @@ async def tool(

logger.info(f"Calling endpoint: {endpoint_name}, with args: {args}")
try:
result = await call_tool_with_reconnect(request, args)
result = await call_tool_with_reconnect(request, args, meta)

if result.isError:
error_message = "Unknown tool execution error"
Expand Down Expand Up @@ -409,7 +409,7 @@ async def tool(request: Request):

logger.info(f"Calling endpoint: {endpoint_name}, with no args")
try:
result = await call_tool_with_reconnect(request, {})
result = await call_tool_with_reconnect(request, {}, meta)

if result.isError:
error_message = "Unknown tool execution error"
Expand Down
89 changes: 69 additions & 20 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.