Skip to content

Commit 927fd32

Browse files
committed
feat: enable bind to unix domain socket
Related: openai/openai-agents-python#437 Related: modelcontextprotocol/python-sdk#430 Related: johnandersen777/scitt-api-emulator@73bbb92 Signed-off-by: John Andersen <johnandersenpdx@gmail.com>
1 parent 79670a4 commit 927fd32

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Arguments
111111
| `command_or_url` | Yes | The command to spawn the MCP stdio server | uvx mcp-server-fetch |
112112
| `--sse-port` | No, random available | The SSE server port to listen on | 8080 |
113113
| `--sse-host` | No, `127.0.0.1` by default | The host IP address that the SSE server will listen on | 0.0.0.0 |
114+
| `--sse-uds` | No | The UNIX Domain Socket that the SSE server will listen on | /tmp/mcp.sock |
114115
| `--env` | No | Additional environment variables to pass to the MCP stdio server | FOO=BAR |
115116
| `--pass-environment` | No | Pass through all environment variables when spawning the server | --no-pass-environment |
116117
| `--allow-origin` | No | Pass through all environment variables when spawning the server | --allow-cors "\*" |
@@ -129,6 +130,9 @@ mcp-proxy --sse-port=8080 uvx mcp-server-fetch
129130
# Start the MCP server behind the proxy with a custom host and port
130131
mcp-proxy --sse-host=0.0.0.0 --sse-port=8080 uvx mcp-server-fetch
131132

133+
# Start the MCP server behind the proxy bound to a UNIX Domain Socket
134+
mcp-proxy --sse-uds /tmp/mcp.sock uvx mcp-server-fetch
135+
132136
# Start the MCP server behind the proxy with a custom user agent
133137
# Note that the `--` separator is used to separate the `mcp-proxy` arguments from the `mcp-server-fetch` arguments
134138
mcp-proxy --sse-port=8080 -- uvx mcp-server-fetch --user-agent=YourUserAgent

src/mcp_proxy/__main__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def main() -> None:
3838
" mcp-proxy --sse-port 8080 -- your-command --arg1 value1 --arg2 value2\n"
3939
" mcp-proxy your-command --sse-port 8080 -e KEY VALUE -e ANOTHER_KEY ANOTHER_VALUE\n"
4040
" mcp-proxy your-command --sse-port 8080 --allow-origin='*'\n"
41+
" mcp-proxy your-command --sse-uds /tmp/mcp.sock\n"
4142
),
4243
formatter_class=argparse.RawTextHelpFormatter,
4344
)
@@ -97,6 +98,11 @@ def main() -> None:
9798
default="127.0.0.1",
9899
help="Host to expose an SSE server on. Default is 127.0.0.1",
99100
)
101+
sse_server_group.add_argument(
102+
"--sse-uds",
103+
default=None,
104+
help="UNIX Domain Socket to expose an SSE server on. Default is None",
105+
)
100106
sse_server_group.add_argument(
101107
"--allow-origin",
102108
nargs="+",
@@ -142,6 +148,7 @@ def main() -> None:
142148
sse_settings = SseServerSettings(
143149
bind_host=args.sse_host,
144150
port=args.sse_port,
151+
uds=args.sse_uds,
145152
allow_origins=args.allow_origin if len(args.allow_origin) > 0 else None,
146153
)
147154
asyncio.run(run_sse_server(stdio_params, sse_settings))

src/mcp_proxy/sse_server.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class SseServerSettings:
2323

2424
bind_host: str
2525
port: int
26+
uds: str | None = None
2627
allow_origins: list[str] | None = None
2728
log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO"
2829

@@ -90,12 +91,19 @@ async def run_sse_server(
9091
debug=(sse_settings.log_level == "DEBUG"),
9192
)
9293

94+
config_kwargs = {}
95+
96+
if sse_settings.uds:
97+
config_kwargs["uds"] = sse_settings.uds
98+
else:
99+
config_kwargs["host"] = sse_settings.bind_host
100+
config_kwargs["port"] = sse_settings.port
101+
93102
# Configure HTTP server
94103
config = uvicorn.Config(
95104
starlette_app,
96-
host=sse_settings.bind_host,
97-
port=sse_settings.port,
98105
log_level=sse_settings.log_level.lower(),
106+
**config_kwargs,
99107
)
100108
http_server = uvicorn.Server(config)
101109
await http_server.serve()

0 commit comments

Comments
 (0)