feat: add support for SSE level authentication (#2)

Support a new env var called API_ACCESS_TOKEN
Fix typo in README
This commit is contained in:
Sergey Parfenyuk
2024-12-28 22:08:29 +01:00
committed by GitHub
3 changed files with 18 additions and 5 deletions

View File

@@ -56,11 +56,19 @@ Configure Claude Desktop to recognize the MCP server.
"mcp-proxy": {
"command": "mcp-proxy",
"env": {
"SSE_URL": "http://example.io/sss"
"SSE_URL": "http://example.io/sse"
}
}
}
}
}
```
## Advanced Configuration
### Environment Variables
| Name | Description |
| ---- | ----------- |
| SSE_URL | The MCP server SSE endpoint to connect to e.g. http://example.io/sse |
| API_ACCESS_TOKEN | Added in the `Authorization` header of the HTTP request as a `Bearer` access token |

View File

@@ -109,10 +109,14 @@ async def create_proxy_server(remote_app: ClientSession):
return app
async def run_sse_client(url: str):
async def run_sse_client(url: str, api_access_token: str | None = None):
from mcp.client.sse import sse_client
async with sse_client(url=url) as (read_stream, write_stream):
headers = {}
if api_access_token is not None:
headers["Authorization"] = f"Bearer {api_access_token}"
async with sse_client(url=url, headers=headers) as (read_stream, write_stream):
async with ClientSession(read_stream, write_stream) as session:
app = await create_proxy_server(session)
async with server.stdio_server() as (read_stream, write_stream):

View File

@@ -7,13 +7,14 @@ from . import run_sse_client
logging.basicConfig(level=logging.DEBUG)
SSE_URL: t.Final[str] = os.getenv("SSE_URL", "")
API_ACCESS_TOKEN: t.Final[str] = os.getenv("API_ACCESS_TOKEN", None)
if not SSE_URL:
raise ValueError("SSE_URL environment variable is not set")
def main():
asyncio.run(run_sse_client(SSE_URL))
asyncio.run(run_sse_client(SSE_URL, api_access_token=API_ACCESS_TOKEN))
if __name__ == "__main__":