main
OpenAI-compatible HTTP interface for MADA Orchestrator.
This module exposes the configured MADA agent team through a small FastAPI app
that implements the OpenAI-compatible /v1/models and /v1/chat/completions
endpoints expected by tools such as Open WebUI.
MADAOpenAIAPIService
Manage the shared orchestrator instance used by the HTTP API.
The API server keeps one lazily-initialized orchestrator for the process and reuses it across requests. Model listing and health checks can succeed before the orchestrator has been started; chat requests trigger startup on demand.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
Full application configuration used to construct the orchestrator and its agents. |
|
model_name |
Model identifier exposed through |
|
api_key |
Optional API key required for incoming HTTP requests. |
|
bearer_token |
Optional bearer token forwarded to streamable HTTP MCP
servers as |
|
orchestrator |
Optional[MADAOrchestrator]
|
Lazily-created orchestrator instance, or |
_startup_lock |
Async lock used to ensure only one request performs the lazy startup sequence. |
Source code in src/mada/interfaces/openai_api/main.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
__init__(config, model_name=DEFAULT_MODEL_NAME, api_key=None, bearer_token=None)
Initialize the OpenAI API service wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AppConfig
|
Parsed application configuration. |
required |
model_name
|
str
|
Externally visible model identifier to publish via the OpenAI-compatible model listing endpoints. |
DEFAULT_MODEL_NAME
|
api_key
|
Optional[str]
|
Optional API key that callers must provide through
|
None
|
bearer_token
|
Optional[str]
|
Optional token forwarded to configured streamable HTTP MCP servers during orchestrator startup. |
None
|
Source code in src/mada/interfaces/openai_api/main.py
collect_response(messages)
async
Collect the full assistant response for non-streaming requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[dict[str, Any]]
|
OpenAI-style chat messages from the HTTP request body. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The full assistant response text assembled from streamed chunks. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the orchestrator has not been initialized yet. |
Source code in src/mada/interfaces/openai_api/main.py
ensure_started()
async
Initialize the orchestrator once, on demand.
Concurrent requests are synchronized so that only one request performs startup and the remaining requests observe the initialized orchestrator.
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
OrchestratorStartupError
|
If startup fails. |
Source code in src/mada/interfaces/openai_api/main.py
shutdown()
async
Release all orchestrator resources.
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in src/mada/interfaces/openai_api/main.py
startup()
async
Initialize the orchestrator and all configured agents.
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
OrchestratorStartupError
|
If the orchestrator or one of its MCP connections fails to initialize. |
KeyboardInterrupt
|
Propagated unchanged. |
SystemExit
|
Propagated unchanged. |
Source code in src/mada/interfaces/openai_api/main.py
stream_response(messages)
async
Yield OpenAI-compatible SSE events for a streaming response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[dict[str, Any]]
|
OpenAI-style chat messages from the HTTP request body. |
required |
Yields:
| Type | Description |
|---|---|
AsyncGenerator[str, None]
|
Server-sent event payload strings formatted for the OpenAI streaming |
AsyncGenerator[str, None]
|
chat completions protocol. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the orchestrator has not been initialized yet. |
Source code in src/mada/interfaces/openai_api/main.py
validate_api_key(authorization, x_api_key)
Validate the caller-provided API key when one is configured.
Open WebUI can send either a Bearer token or x-api-key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
authorization
|
Optional[str]
|
Optional |
required |
x_api_key
|
Optional[str]
|
Optional |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
HTTPException
|
If an API key is configured and the request does not provide a matching key. |
Source code in src/mada/interfaces/openai_api/main.py
OrchestratorStartupError
Bases: RuntimeError
Raised when the orchestrator cannot be initialized for API requests.
This is used to convert lower-level startup failures, most commonly MCP
connection problems, into a stable error type that the FastAPI layer can map
to a 503 Service Unavailable response.
Source code in src/mada/interfaces/openai_api/main.py
create_openai_api_app(config, model_name=DEFAULT_MODEL_NAME, api_key=None, bearer_token=None)
Build and return a FastAPI app backed by the configured MADA orchestrator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AppConfig
|
Parsed MADA application configuration. |
required |
model_name
|
str
|
Model identifier to expose through the OpenAI-compatible model listing endpoints. |
DEFAULT_MODEL_NAME
|
api_key
|
Optional[str]
|
Optional API key required for incoming requests. |
None
|
bearer_token
|
Optional[str]
|
Optional bearer token forwarded to streamable HTTP MCP servers during orchestrator startup. |
None
|
Returns:
| Type | Description |
|---|---|
FastAPI
|
A configured FastAPI application exposing health, model listing, and |
FastAPI
|
chat completion routes. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If FastAPI or Uvicorn dependencies are unavailable. |
Source code in src/mada/interfaces/openai_api/main.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
main(host, port, model_name, api_key, bearer_token, config_file)
Run MADA Orchestrator as an OpenAI-compatible API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Host interface to bind the HTTP server to. |
required |
port
|
int
|
TCP port to bind the HTTP server to. |
required |
model_name
|
str
|
Model identifier to expose through the OpenAI-compatible model listing endpoints. |
required |
api_key
|
Optional[str]
|
Optional API key required for incoming requests. |
required |
bearer_token
|
Optional[str]
|
Optional bearer token forwarded to streamable HTTP MCP servers during orchestrator startup. |
required |
config_file
|
str
|
Path to the MADA Orchestrator configuration file. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in src/mada/interfaces/openai_api/main.py
openai_api_entrypoint(host, port, model_name, api_key, bearer_token, config_file)
Load config and start the OpenAI-compatible API server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Host interface to bind the HTTP server to. |
required |
port
|
int
|
TCP port to bind the HTTP server to. |
required |
model_name
|
str
|
Model identifier to expose through model listing routes. |
required |
api_key
|
Optional[str]
|
Optional API key required for incoming requests. |
required |
bearer_token
|
Optional[str]
|
Optional bearer token forwarded to streamable HTTP MCP servers during orchestrator startup. |
required |
config_file
|
str
|
Path to the JSON configuration file to load. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in src/mada/interfaces/openai_api/main.py
run_openai_api(config, host, port, model_name=DEFAULT_MODEL_NAME, api_key=None, bearer_token=None)
Launch the OpenAI-compatible FastAPI server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AppConfig
|
Parsed MADA application configuration. |
required |
host
|
str
|
Host interface to bind the HTTP server to. |
required |
port
|
int
|
TCP port to bind the HTTP server to. |
required |
model_name
|
str
|
Model identifier to expose through model listing routes. |
DEFAULT_MODEL_NAME
|
api_key
|
Optional[str]
|
Optional API key required for incoming requests. |
None
|
bearer_token
|
Optional[str]
|
Optional bearer token forwarded to streamable HTTP MCP servers during orchestrator startup. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|