unblu_mcp
¶
unblu-mcp package.
A model context protocol server for interacting with Unblu deployments.
Modules:
-
cli–
Classes:
-
ConnectionConfig–Configuration returned by a connection provider.
-
ConnectionProvider–Abstract base class for connection providers.
-
DefaultConnectionProvider–Default provider using environment variables.
-
K8sConnectionProvider–Connection provider for Kubernetes deployments using port-forwarding.
-
K8sEnvironmentConfig–Configuration for a Kubernetes environment.
-
OperationInfo–Brief information about an API operation.
-
OperationSchema–Full schema for an API operation.
-
ServiceInfo–Information about an API service category.
-
UnbluAPIRegistry–Registry for Unblu API operations parsed from OpenAPI spec.
Functions:
-
create_server–Create the Unblu MCP server with progressive disclosure tools.
-
detect_environment_from_context–Detect the environment from the current kubectl context.
-
get_parser–Return the CLI argument parser.
-
get_server–Get or create the global server instance.
-
main–Run the main program.
ConnectionConfig
dataclass
¶
ConnectionConfig(
base_url: str,
headers: dict[str, str] = dict(),
auth: Auth | None = None,
timeout: float = 30.0,
)
Configuration returned by a connection provider.
Attributes:
-
auth(Auth | None) –Optional httpx auth handler for basic auth, etc.
-
base_url(str) –The base URL for API requests (e.g., http://localhost:8084/app/rest/v4).
-
headers(dict[str, str]) –Additional headers to include in all requests (e.g., trusted headers).
-
timeout(float) –Request timeout in seconds.
auth
class-attribute
instance-attribute
¶
auth: Auth | None = None
Optional httpx auth handler for basic auth, etc.
base_url
instance-attribute
¶
base_url: str
The base URL for API requests (e.g., http://localhost:8084/app/rest/v4).
headers
class-attribute
instance-attribute
¶
Additional headers to include in all requests (e.g., trusted headers).
ConnectionProvider
¶
Bases: ABC
Abstract base class for connection providers.
Connection providers handle the complexity of connecting to Unblu deployments in various environments. They can:
- Start/stop port-forwards or tunnels
- Manage authentication (API keys, basic auth, trusted headers)
- Handle environment switching
- Refresh credentials when needed
Example implementation for Kubernetes:
class K8sConnectionProvider(ConnectionProvider):
def __init__(self, environment: str = "dev"):
self.environment = environment
self._port_forward_process = None
async def setup(self) -> None:
# Start kubectl port-forward
...
async def teardown(self) -> None:
# Stop port-forward
...
def get_config(self) -> ConnectionConfig:
return ConnectionConfig(
base_url=f"http://localhost:{self.port}/app/rest/v4",
headers={
"x-unblu-trusted-user-id": "superadmin",
"x-unblu-trusted-user-role": "SUPER_ADMIN",
},
)
Methods:
-
get_config–Return the current connection configuration.
-
health_check–Check if the connection is healthy.
-
setup–Initialize the connection (start port-forward, refresh auth, etc.).
-
teardown–Clean up resources (stop port-forward, close connections, etc.).
get_config
abstractmethod
¶
get_config() -> ConnectionConfig
Return the current connection configuration.
This is called for each API request, allowing dynamic configuration (e.g., refreshing tokens, switching environments).
Returns:
-
ConnectionConfig–ConnectionConfig with base_url, headers, auth, and timeout.
Source code in src/unblu_mcp/_internal/providers.py
79 80 81 82 83 84 85 86 87 88 | |
health_check
async
¶
health_check() -> bool
Check if the connection is healthy.
Override this to implement custom health checks (e.g., ping the API).
Returns:
-
bool–True if the connection is healthy, False otherwise.
Source code in src/unblu_mcp/_internal/providers.py
90 91 92 93 94 95 96 97 98 | |
setup
abstractmethod
async
¶
setup() -> None
Initialize the connection (start port-forward, refresh auth, etc.).
Called once when the MCP server starts, before any API requests. Should be idempotent - safe to call multiple times.
Source code in src/unblu_mcp/_internal/providers.py
63 64 65 66 67 68 69 | |
teardown
abstractmethod
async
¶
teardown() -> None
Clean up resources (stop port-forward, close connections, etc.).
Called when the MCP server shuts down. Should be safe to call even if setup() was never called.
Source code in src/unblu_mcp/_internal/providers.py
71 72 73 74 75 76 77 | |
DefaultConnectionProvider
¶
DefaultConnectionProvider(
base_url: str | None = None,
api_key: str | None = None,
username: str | None = None,
password: str | None = None,
trusted_headers: dict[str, str] | None = None,
)
Bases: ConnectionProvider
Default provider using environment variables.
This is the standard provider for direct connections to Unblu Cloud or self-hosted deployments with direct network access.
Environment variables
UNBLU_BASE_URL: API base URL (default: https://unblu.cloud/app/rest/v4) UNBLU_API_KEY: Bearer token for API key auth UNBLU_USERNAME: Username for basic auth UNBLU_PASSWORD: Password for basic auth UNBLU_TRUSTED_HEADERS: Trusted headers (format: "key:value,key:value")
Methods:
-
get_config–Build config from environment variables and constructor args.
-
health_check–Check if the connection is healthy.
-
setup–No setup needed for direct connections.
-
teardown–No teardown needed for direct connections.
Source code in src/unblu_mcp/_internal/providers.py
115 116 117 118 119 120 121 122 123 124 125 126 127 | |
get_config
¶
get_config() -> ConnectionConfig
Build config from environment variables and constructor args.
Source code in src/unblu_mcp/_internal/providers.py
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 | |
health_check
async
¶
health_check() -> bool
Check if the connection is healthy.
Override this to implement custom health checks (e.g., ping the API).
Returns:
-
bool–True if the connection is healthy, False otherwise.
Source code in src/unblu_mcp/_internal/providers.py
90 91 92 93 94 95 96 97 98 | |
setup
async
¶
setup() -> None
No setup needed for direct connections.
Source code in src/unblu_mcp/_internal/providers.py
129 130 | |
teardown
async
¶
teardown() -> None
No teardown needed for direct connections.
Source code in src/unblu_mcp/_internal/providers.py
132 133 | |
K8sConnectionProvider
¶
K8sConnectionProvider(
environment: str | K8sEnvironmentConfig = "dev",
trusted_user_id: str = "superadmin",
trusted_user_role: str = "SUPER_ADMIN",
environments: dict[str, K8sEnvironmentConfig]
| None = None,
)
Bases: ConnectionProvider
Connection provider for Kubernetes deployments using port-forwarding.
This provider: - Automatically starts kubectl port-forward on setup - Cleans up the port-forward process on teardown - Configures trusted headers authentication - Supports multiple environments with different ports
Parameters:
-
environment(str | K8sEnvironmentConfig, default:'dev') –Environment name (dev, staging, prod) or custom K8sEnvironmentConfig.
-
trusted_user_id(str, default:'superadmin') –User ID for trusted headers auth (default: superadmin).
-
trusted_user_role(str, default:'SUPER_ADMIN') –User role for trusted headers auth (default: SUPER_ADMIN).
-
environments(dict[str, K8sEnvironmentConfig] | None, default:None) –Custom environment configurations (overrides defaults).
Example
provider = K8sConnectionProvider(environment="dev") await provider.setup() # Starts port-forward config = provider.get_config() # Returns connection config await provider.teardown() # Stops port-forward
Methods:
-
get_config–Return connection config with trusted headers.
-
health_check–Check if the port-forward is healthy.
-
setup–Start kubectl port-forward if not already running.
-
teardown–Stop the port-forward process only if we own it.
Attributes:
-
environment(str) –Return the current environment name.
-
local_port(int) –Return the local port for this environment.
Source code in src/unblu_mcp/_internal/providers_k8s.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
get_config
¶
get_config() -> ConnectionConfig
Return connection config with trusted headers.
Source code in src/unblu_mcp/_internal/providers_k8s.py
276 277 278 279 280 281 282 283 284 | |
health_check
async
¶
health_check() -> bool
Check if the port-forward is healthy.
Source code in src/unblu_mcp/_internal/providers_k8s.py
286 287 288 | |
setup
async
¶
setup() -> None
Start kubectl port-forward if not already running.
Uses a lock file to coordinate between multiple MCP server instances. Only one instance will start the port-forward; others will reuse it.
Source code in src/unblu_mcp/_internal/providers_k8s.py
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 | |
teardown
async
¶
teardown() -> None
Stop the port-forward process only if we own it.
Only the instance that acquired the lock and started the port-forward will terminate it. Other instances just release their reference.
Source code in src/unblu_mcp/_internal/providers_k8s.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
K8sEnvironmentConfig
dataclass
¶
K8sEnvironmentConfig(
name: str,
local_port: int,
namespace: str,
service: str = "haproxy",
service_port: int = 8080,
api_path: str = "/app/rest/v4",
)
Configuration for a Kubernetes environment.
Attributes:
-
api_path(str) –API path prefix.
-
local_port(int) –Local port for port-forwarding.
-
name(str) –Environment name (e.g., dev, staging, prod).
-
namespace(str) –Kubernetes namespace.
-
service(str) –Kubernetes service name.
-
service_port(int) –Service port to forward.
OperationInfo
¶
Bases: BaseModel
Brief information about an API operation.
OperationSchema
¶
Bases: BaseModel
Full schema for an API operation.
ServiceInfo
¶
Bases: BaseModel
Information about an API service category.
UnbluAPIRegistry
¶
Registry for Unblu API operations parsed from OpenAPI spec.
Methods:
-
get_operation_schema–Get full schema for an operation.
-
list_operations–List operations for a specific service (case-insensitive).
-
list_services–List all available API services.
-
search_operations–Search operations by name, path, or description.
Source code in src/unblu_mcp/_internal/server.py
61 62 63 64 65 66 67 | |
get_operation_schema
¶
get_operation_schema(
operation_id: str,
) -> OperationSchema | None
Get full schema for an operation.
Source code in src/unblu_mcp/_internal/server.py
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 | |
list_operations
¶
list_operations(service: str) -> list[OperationInfo]
List operations for a specific service (case-insensitive).
Source code in src/unblu_mcp/_internal/server.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
list_services
¶
list_services() -> list[ServiceInfo]
List all available API services.
Source code in src/unblu_mcp/_internal/server.py
112 113 114 | |
search_operations
¶
search_operations(
query: str, limit: int = 20
) -> list[OperationInfo]
Search operations by name, path, or description.
Source code in src/unblu_mcp/_internal/server.py
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 | |
create_server
¶
create_server(
spec_path: str | Path | None = None,
base_url: str | None = None,
api_key: str | None = None,
username: str | None = None,
password: str | None = None,
provider: ConnectionProvider | None = None,
policy_file: str | Path | None = None,
) -> FastMCP
Create the Unblu MCP server with progressive disclosure tools.
Parameters:
-
spec_path(str | Path | None, default:None) –Path to swagger.json. Defaults to swagger.json in package root.
-
base_url(str | None, default:None) –Unblu API base URL. Defaults to UNBLU_BASE_URL env var.
-
api_key(str | None, default:None) –API key for authentication. Defaults to UNBLU_API_KEY env var.
-
username(str | None, default:None) –Username for basic auth. Defaults to UNBLU_USERNAME env var.
-
password(str | None, default:None) –Password for basic auth. Defaults to UNBLU_PASSWORD env var.
-
provider(ConnectionProvider | None, default:None) –Optional connection provider for complex connectivity (e.g., K8s port-forward). If provided, overrides base_url/api_key/username/password.
-
policy_file(str | Path | None, default:None) –Optional path to Eunomia policy JSON file for authorization. Requires the 'safety' extra: pip install unblu-mcp[safety]
Returns:
-
FastMCP–Configured FastMCP server instance.
Source code in src/unblu_mcp/_internal/server.py
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 338 339 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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | |
detect_environment_from_context
¶
detect_environment_from_context() -> str | None
Detect the environment from the current kubectl context.
Matches environment names from the loaded configuration against patterns in the current kubectl context name.
Returns:
-
str | None–Environment name or None if not detected.
Source code in src/unblu_mcp/_internal/providers_k8s.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
get_parser
¶
get_parser() -> ArgumentParser
Return the CLI argument parser.
Returns:
-
ArgumentParser–An argparse parser.
Source code in src/unblu_mcp/_internal/cli.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
get_server
¶
get_server() -> FastMCP
Get or create the global server instance.
Source code in src/unblu_mcp/_internal/server.py
651 652 653 654 655 | |
main
¶
Run the main program.
This function is executed when you type unblu-mcp or python -m unblu_mcp.
Parameters:
Returns:
-
int–An exit code.
Source code in src/unblu_mcp/_internal/cli.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |