File tree 7 files changed +188
-122
lines changed
7 files changed +188
-122
lines changed Original file line number Diff line number Diff line change @@ -123,6 +123,26 @@ If you want to use the tools inside of [claude desktop](https://claude.ai/downlo
123
123
124
124
To add new MCP servers, edit the config.json file.
125
125
126
+ ### API Key Authentication
127
+
128
+ MCP-Bridge supports API key authentication to secure your server. To enable this feature, add an ` api_key ` field to your config.json file:
129
+
130
+ ``` json
131
+ {
132
+ "api_key" : " your-secure-api-key-here"
133
+ }
134
+ ```
135
+
136
+ When making requests to the MCP-Bridge server, include the API key in the Authorization header as a Bearer token:
137
+
138
+ ```
139
+ Authorization: Bearer your-secure-api-key-here
140
+ ```
141
+
142
+ If the ` api_key ` field is empty or not present in the configuration, authentication will be skipped, allowing backward compatibility.
143
+
144
+ ### Full Configuration Example
145
+
126
146
an example config.json file with most of the options explicitly set:
127
147
128
148
``` json
@@ -162,7 +182,8 @@ an example config.json file with most of the options explicitly set:
162
182
},
163
183
"logging" : {
164
184
"log_level" : " DEBUG"
165
- }
185
+ },
186
+ "api_key" : " your-secure-api-key-here"
166
187
}
167
188
```
168
189
@@ -172,6 +193,7 @@ an example config.json file with most of the options explicitly set:
172
193
| mcp_servers | The MCP servers configuration |
173
194
| network | uvicorn network configuration |
174
195
| logging | The logging configuration |
196
+ | api_key | API key for server authentication |
175
197
176
198
## Support
177
199
Original file line number Diff line number Diff line change
1
+ from fastapi import Depends , HTTPException , Security , status
2
+ from fastapi .security import HTTPBearer , HTTPAuthorizationCredentials
3
+ from mcp_bridge .config import config
4
+
5
+ security = HTTPBearer (auto_error = False )
6
+
7
+ async def get_api_key (credentials : HTTPAuthorizationCredentials = Security (security )):
8
+ """
9
+ Validate the API key provided in the Authorization header.
10
+
11
+ If no API key is configured in the server settings, authentication is skipped.
12
+ If an API key is configured, the request must include a matching API key.
13
+
14
+ The API key should be provided in the Authorization header as:
15
+ Authorization: Bearer your-api-key-here
16
+ """
17
+ # If no API key is configured, skip authentication
18
+ if not config .api_key :
19
+ return True
20
+
21
+ # If API key is configured but not provided in the request
22
+ if not credentials :
23
+ raise HTTPException (
24
+ status_code = status .HTTP_401_UNAUTHORIZED ,
25
+ detail = "API key is required in Authorization header (Bearer token)" ,
26
+ )
27
+
28
+ # If API key is provided but doesn't match
29
+ if credentials .credentials != config .api_key :
30
+ raise HTTPException (
31
+ status_code = status .HTTP_401_UNAUTHORIZED ,
32
+ detail = "Invalid API key" ,
33
+ )
34
+
35
+ return True
Original file line number Diff line number Diff line change @@ -73,6 +73,11 @@ class Settings(BaseSettings):
73
73
default_factory = lambda : Network .model_construct (),
74
74
description = "network config" ,
75
75
)
76
+
77
+ api_key : str = Field (
78
+ default = "" ,
79
+ description = "API key for authenticating requests to the MCP Bridge server"
80
+ )
76
81
77
82
model_config = SettingsConfigDict (
78
83
env_prefix = "MCP_BRIDGE__" ,
Original file line number Diff line number Diff line change 1
- from fastapi import APIRouter
1
+ from fastapi import APIRouter , Depends
2
2
3
3
from lmos_openai_types import CreateChatCompletionRequest , CreateCompletionRequest
4
4
10
10
)
11
11
12
12
from mcp_bridge .openapi_tags import Tag
13
+ from mcp_bridge .auth import get_api_key
13
14
14
- router = APIRouter (prefix = "/v1" , tags = [Tag .openai ])
15
+ router = APIRouter (prefix = "/v1" , tags = [Tag .openai ], dependencies = [ Depends ( get_api_key )] )
15
16
16
17
17
18
@router .post ("/completions" )
Original file line number Diff line number Diff line change 1
- from fastapi import APIRouter
1
+ from fastapi import APIRouter , Depends
2
2
from mcp_bridge .openapi_tags import Tag
3
+ from mcp_bridge .auth import get_api_key
3
4
4
5
from .tools import router as tools_router
5
6
from .prompts import router as prompts_router
6
7
from .resources import router as resources_router
7
8
from .server import router as server_router
8
9
9
- router = APIRouter (prefix = "/mcp" , tags = [Tag .mcp_management ])
10
+ router = APIRouter (prefix = "/mcp" , tags = [Tag .mcp_management ], dependencies = [ Depends ( get_api_key )] )
10
11
11
12
router .include_router (tools_router )
12
13
router .include_router (prompts_router )
Original file line number Diff line number Diff line change 1
- from fastapi import APIRouter
1
+ from fastapi import APIRouter , Depends
2
2
from .sse import router as sse_router
3
3
from mcp_bridge .openapi_tags import Tag
4
+ from mcp_bridge .auth import get_api_key
4
5
5
6
__all__ = ["router" ]
6
7
7
- router = APIRouter (prefix = "/mcp-server" , tags = [Tag .mcp_server ])
8
+ router = APIRouter (prefix = "/mcp-server" , tags = [Tag .mcp_server ], dependencies = [ Depends ( get_api_key )] )
8
9
router .include_router (sse_router )
You can’t perform that action at this time.
0 commit comments