API Reference¶
Core Session Management¶
This module contains the session manager for the MCP ephemeral K8s library. It is used to create and manage MCP servers in a Kubernetes cluster.
KubernetesSessionManager
¶
Bases: BaseModel
Kubernetes session manager for MCP.
This manager creates and manages Kubernetes jobs for MCP sessions. It implements the async context manager protocol for easy resource management.
Source code in src/mcp_ephemeral_k8s/session_manager.py
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 | |
__aenter__()
async
¶
Enter the async context manager.
Source code in src/mcp_ephemeral_k8s/session_manager.py
112 113 114 115 | |
__aexit__(exc_type, exc_val, exc_tb)
async
¶
Exit the async context manager.
Source code in src/mcp_ephemeral_k8s/session_manager.py
117 118 119 120 | |
create_mcp_server(config, wait_for_ready=True, expose_port=False)
async
¶
Start a new MCP server using the provided configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
EphemeralMcpServerConfig
|
The configuration for the MCP servers |
required |
wait_for_ready
|
bool
|
Whether to wait for the job to be ready before returning a response to the client |
True
|
expose_port
|
bool
|
Whether to expose the port through a Kubernetes service |
False
|
Returns:
| Type | Description |
|---|---|
EphemeralMcpServer
|
The MCP server instance |
Source code in src/mcp_ephemeral_k8s/session_manager.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
delete_mcp_server(job_name, wait_for_deletion=True)
async
¶
Delete the MCP server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_name
|
str
|
Name of the job/pod |
required |
wait_for_deletion
|
bool
|
Whether to wait for the job to be deleted |
True
|
Returns:
| Type | Description |
|---|---|
EphemeralMcpServer
|
The MCP server instance |
Source code in src/mcp_ephemeral_k8s/session_manager.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
expose_mcp_server_port(mcp_server)
¶
Expose the MCP server port to the outside world through a Kubernetes service.
Source code in src/mcp_ephemeral_k8s/session_manager.py
283 284 285 | |
load_session_manager()
¶
Load Kubernetes configuration from default location or from service account if running in cluster.
Source code in src/mcp_ephemeral_k8s/session_manager.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
mount_mcp_server(job_name)
async
¶
Mount an MCP server over SSE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_name
|
str
|
The name of the pod that is running the MCP server. |
required |
Source code in src/mcp_ephemeral_k8s/session_manager.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
remove_mcp_server_port(mcp_server)
¶
Remove the MCP server.
Source code in src/mcp_ephemeral_k8s/session_manager.py
287 288 289 | |
Command Line Interface¶
McpEphemeralK8s
¶
Bases: BaseSettings
The MCP ephemeral K8s CLI.
Source code in src/mcp_ephemeral_k8s/cli.py
45 46 47 48 49 50 51 52 53 | |
Application Layer¶
MCP server application, meant to be used as an MCP server that can spawn other MCP servers.
create_mcp_server(ctx, runtime_exec, runtime_mcp, runtime_args='', env=None, wait_for_ready=False)
async
¶
Create a new ephemeral MCP server in Kubernetes.
Spawns a new MCP server instance as a Kubernetes Job with the specified configuration. The server runs in an isolated environment and can be customized with different runtime executors, packages, arguments, and environment variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
runtime_exec
|
str
|
The executor to use for running the MCP server (e.g., 'uvx' for Python packages, 'npx' for Node.js packages, 'docker' for container images). |
required |
runtime_mcp
|
str
|
The MCP package or image to run (e.g., 'mcp-server-sqlite' for a Python-based SQLite MCP server, or '@modelcontextprotocol/server-filesystem' for a Node.js filesystem server). |
required |
runtime_args
|
str
|
Optional arguments to pass to the MCP server at runtime. These are appended to the command line when starting the server. |
''
|
env
|
dict[str, str] | None
|
Optional dictionary of environment variables to set in the server's container. Useful for configuration, authentication tokens, or feature flags. |
None
|
wait_for_ready
|
bool
|
If True, this call will block until the Kubernetes Job is in a ready state. If False, returns immediately after submitting the Job. |
False
|
Returns:
| Type | Description |
|---|---|
EphemeralMcpServer
|
An EphemeralMcpServer object containing the created server's configuration, |
EphemeralMcpServer
|
pod name, status, and connection details. |
Example usage
To create a filesystem MCP server: create_mcp_server( runtime_exec='npx', runtime_mcp='@modelcontextprotocol/server-filesystem', runtime_args='/tmp', wait_for_ready=True )
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
delete_mcp_server(ctx, job_name, wait_for_deletion=False)
async
¶
Delete an ephemeral MCP server and clean up its resources.
Terminates a running MCP server instance and removes its associated Kubernetes resources including the Job, Pod, and any exposed Services. This is important for cleaning up resources when an MCP server is no longer needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_name
|
str
|
The name of the pod/server to delete. This is returned when creating a server and can be retrieved using list_mcp_servers(). |
required |
wait_for_deletion
|
bool
|
If True, this call will block until the Kubernetes resources are fully deleted and removed from the cluster. If False, initiates deletion and returns immediately. |
False
|
Returns:
| Type | Description |
|---|---|
EphemeralMcpServer
|
An EphemeralMcpServer object containing the deleted server's final state and |
EphemeralMcpServer
|
metadata before removal. |
Example usage
After retrieving the list of servers, delete a specific one by its pod name: delete_mcp_server(job_name='mcp-server-xyz123', wait_for_deletion=True)
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
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 | |
get_mcp_server_status(ctx, job_name)
async
¶
Get the detailed Kubernetes status of an MCP server.
Retrieves the underlying Kubernetes Job status for a specific MCP server instance. This provides low-level details about the Job's execution state, including conditions, start/completion times, and any failure information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_name
|
str
|
The name of the pod/server to check. This identifier is returned when creating a server and can be retrieved using list_mcp_servers(). |
required |
Returns:
| Type | Description |
|---|---|
V1Job | None
|
A Kubernetes V1Job object containing the Job's complete status information, |
V1Job | None
|
or None if the Job is not found. The Job status includes fields such as: |
V1Job | None
|
|
V1Job | None
|
|
V1Job | None
|
|
V1Job | None
|
|
V1Job | None
|
|
Example usage
Use this to debug issues with a server or to check if a Job has completed successfully: get_mcp_server_status(job_name='mcp-server-xyz123')
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
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 | |
get_version()
¶
Get the version of the MCP ephemeral server.
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
44 45 46 47 | |
health_check(request)
async
¶
Health check endpoint for the MCP server.
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
238 239 240 241 | |
lifespan(server)
async
¶
Lifecycle hooks for the MCP ephemeral server.
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
18 19 20 21 22 23 24 25 26 | |
list_mcp_servers(ctx)
async
¶
List all currently running ephemeral MCP servers.
Retrieves information about all MCP server instances that are currently managed by this controller. Each server entry includes its pod name, configuration details, current status, creation timestamp, and connection information if available.
Returns:
| Type | Description |
|---|---|
list[EphemeralMcpServer]
|
A list of EphemeralMcpServer objects representing all active MCP servers. |
list[EphemeralMcpServer]
|
Each object contains the server's configuration, runtime status, and metadata. |
Example usage
Call this tool to see what MCP servers are currently running before creating new ones or to check the status of existing servers.
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
list_presets()
¶
List all available preset MCP server configurations.
Returns a list of pre-configured MCP server templates that can be used as examples or starting points for creating new ephemeral MCP servers. Each preset includes the runtime executor (e.g., 'uvx', 'npx'), the MCP package to install, optional runtime arguments, and environment variables.
Returns:
| Type | Description |
|---|---|
list[EphemeralMcpServerConfig]
|
A list of EphemeralMcpServerConfig objects containing preset configurations. |
list[EphemeralMcpServerConfig]
|
Each config specifies how to run a specific MCP server package. |
Example usage
Use this to discover available MCP server configurations before creating one.
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
main(transport='sse', show_banner=True, allow_origins=None, **transport_kwargs)
¶
Run the FastMCP server.
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
mount_mcp_server(ctx, job_name, name=None)
async
¶
Mount a remote MCP server over SSE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_name
|
str
|
The name of the pod that is running the remote MCP server. |
required |
name
|
str | None
|
The name of the proxy server. |
None
|
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
201 202 203 204 205 206 207 208 209 210 211 212 | |
remove_mcp_server_mount(name=None)
async
¶
Remove the mount of an MCP server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
The name of the server to remove. If None, all mounted servers with a prefix will be removed. |
None
|
Source code in src/mcp_ephemeral_k8s/app/mcp_server.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
API Models & Configuration¶
This module contains the models for the MCP ephemeral K8s library.
EphemeralMcpServer
¶
Bases: BaseModel
The MCP server that is running in a Kubernetes pod.
Source code in src/mcp_ephemeral_k8s/api/ephemeral_mcp_server.py
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 | |
EphemeralMcpServerConfig
¶
Bases: BaseModel
Configuration for Kubernetes resources.
Source code in src/mcp_ephemeral_k8s/api/ephemeral_mcp_server.py
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 | |
image_name
property
¶
The name of the image to use for the MCP server.
job_name
cached
property
¶
The name of the job to use for the MCP server.
from_docker_image(image, entrypoint=None, **kwargs)
classmethod
¶
Create an EphemeralMcpServerConfig from a Docker image. The image must be a MCP server image, otherwise an error is raised.
Source code in src/mcp_ephemeral_k8s/api/ephemeral_mcp_server.py
143 144 145 146 147 148 149 150 151 152 153 | |
validate_runtime_exec()
¶
Validate the runtime configuration. Both runtime_exec and runtime_mcp must be specified, or neither.
Source code in src/mcp_ephemeral_k8s/api/ephemeral_mcp_server.py
98 99 100 101 102 103 104 105 106 107 108 109 | |
KubernetesProbeConfig
¶
Bases: BaseModel
Configuration for Kubernetes readiness probe.
The readiness probe is used to determine when a container is ready to accept traffic. These defaults are tuned for MCP server startup, which may take time to install dependencies.
With defaults: waits 10s initially, then checks every 1s for up to 300 failures (5 minutes total).
Source code in src/mcp_ephemeral_k8s/api/ephemeral_mcp_server.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
KubernetesRuntime
¶
Bases: StrEnum
The runtime that is being used for Kubeconfig
Source code in src/mcp_ephemeral_k8s/api/ephemeral_mcp_server.py
16 17 18 19 20 | |
Exceptions¶
This module contains the exceptions for the MCP ephemeral K8s library.
InvalidKubeConfigError
¶
Bases: Exception
Exception raised when the kube config is invalid.
Source code in src/mcp_ephemeral_k8s/api/exceptions.py
6 7 8 9 10 11 | |
MCPInvalidRuntimeError
¶
Bases: ValueError
An error that occurs when the runtime is invalid.
Source code in src/mcp_ephemeral_k8s/api/exceptions.py
38 39 40 41 42 | |
MCPJobError
¶
Bases: Exception
Exception raised when the MCP job is in an error state.
Source code in src/mcp_ephemeral_k8s/api/exceptions.py
61 62 63 64 65 66 | |
MCPJobNotFoundError
¶
Bases: Exception
Exception raised when the MCP job is not found.
Source code in src/mcp_ephemeral_k8s/api/exceptions.py
30 31 32 33 34 35 | |
MCPJobTimeoutError
¶
Bases: Exception
Exception raised when the MCP job times out.
Source code in src/mcp_ephemeral_k8s/api/exceptions.py
53 54 55 56 57 58 | |
MCPNamespaceNotFoundError
¶
Bases: ValueError
An error that occurs when the namespace is not found.
Source code in src/mcp_ephemeral_k8s/api/exceptions.py
45 46 47 48 49 50 | |
MCPPortForwardError
¶
Bases: Exception
Exception raised when the MCP port forward fails.
Source code in src/mcp_ephemeral_k8s/api/exceptions.py
14 15 16 17 18 19 | |
MCPServerCreationError
¶
Bases: Exception
Exception raised when the MCP server creation fails.
Source code in src/mcp_ephemeral_k8s/api/exceptions.py
22 23 24 25 26 27 | |
Kubernetes Operations¶
Job Management¶
check_pod_status(core_v1, job_name, namespace)
¶
Check the status of pods associated with a job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_v1
|
CoreV1Api
|
The Kubernetes core API client |
required |
job_name
|
str
|
Name of the job/pod |
required |
namespace
|
str
|
Kubernetes namespace |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if a pod is running and ready (probes successful), False if waiting for pods |
Raises:
| Type | Description |
|---|---|
MCPJobError
|
If a pod is in Failed or Unknown state |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
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 | |
create_mcp_server_job(config, namespace, service_account_name=None)
¶
Create a job that will run until explicitly terminated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
EphemeralMcpServerConfig
|
The configuration for the MCP server |
required |
namespace
|
str
|
Kubernetes namespace |
required |
service_account_name
|
str | None
|
Optional ServiceAccount name to use for the pod |
None
|
Returns:
| Type | Description |
|---|---|
V1Job
|
The MCP server instance |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 | |
create_proxy_server(url, **kwargs)
¶
Create a proxy server from a remote MCP server over SSE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The SSE endpoint URL of the remote MCP server |
required |
**kwargs
|
Any
|
Additional keyword arguments for SSETransport configuration - sse_read_timeout: SSE read timeout (default: 300s) - headers: Optional HTTP headers dict - auth: Optional authentication - httpx_client_factory: Optional custom HTTPX client factory |
{}
|
Returns:
| Type | Description |
|---|---|
FastMCP
|
FastMCP proxy server instance |
Example
server = create_proxy_server( ... url="http://pod.default.svc.cluster.local:8080/sse", ... sse_read_timeout=600.0 ... )
Source code in src/mcp_ephemeral_k8s/k8s/job.py
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 | |
delete_mcp_server_job(core_v1, batch_v1, job_name, namespace)
¶
Delete a Kubernetes job and its associated pods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_v1
|
CoreV1Api
|
The Kubernetes core API client |
required |
batch_v1
|
BatchV1Api
|
The Kubernetes batch API client |
required |
job_name
|
str
|
The name of the pod to delete |
required |
namespace
|
str
|
The namespace of the pod |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the job was deleted successfully, False otherwise |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
expose_mcp_server_port(core_v1, job_name, namespace, port)
¶
Expose the MCP server port to the outside world.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_v1
|
CoreV1Api
|
The Kubernetes core API client |
required |
job_name
|
str
|
Name of the pod (job name) |
required |
namespace
|
str
|
Kubernetes namespace |
required |
port
|
int
|
Port to expose |
required |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
get_mcp_server_job_status(batch_v1, job_name, namespace)
¶
Get the status of a Kubernetes job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_v1
|
BatchV1Api
|
The Kubernetes batch API client |
required |
job_name
|
str
|
The name of the pod to get the status of |
required |
namespace
|
str
|
The namespace of the pod |
required |
Returns:
| Type | Description |
|---|---|
None | V1Job
|
The status of the job |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
116 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 | |
remove_mcp_server_port(core_v1, job_name, namespace)
¶
Remove the MCP server port from the outside world.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_v1
|
CoreV1Api
|
The Kubernetes core API client |
required |
job_name
|
str
|
Name of the pod |
required |
namespace
|
str
|
Kubernetes namespace |
required |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
335 336 337 338 339 340 341 342 343 344 345 | |
wait_for_job_deletion(batch_v1, job_name, namespace, sleep_time=1, max_wait_time=60)
async
¶
Wait for a job to be deleted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_v1
|
BatchV1Api
|
The Kubernetes batch API client |
required |
job_name
|
str
|
Name of the pod |
required |
namespace
|
str
|
Kubernetes namespace |
required |
sleep_time
|
float
|
Time to sleep between checks |
1
|
max_wait_time
|
float
|
Maximum time to wait before timing out |
60
|
Raises:
| Type | Description |
|---|---|
MCPJobTimeoutError
|
If the job is not deleted within max_wait_time |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
wait_for_job_ready(batch_v1, core_v1, job_name, namespace, sleep_time=1, max_wait_time=60)
async
¶
Wait for a job's pod to be in the running state and ready (probes successful).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_v1
|
BatchV1Api
|
The Kubernetes batch API client |
required |
core_v1
|
CoreV1Api
|
The Kubernetes core API client |
required |
job_name
|
str
|
Name of the pod |
required |
namespace
|
str
|
Kubernetes namespace |
required |
sleep_time
|
float
|
Time to sleep between checks |
1
|
max_wait_time
|
float
|
Maximum time to wait before timing out |
60
|
Raises:
| Type | Description |
|---|---|
MCPJobTimeoutError
|
If the job does not become ready within max_wait_time |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
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 | |
RBAC Management¶
Helper functions for managing RBAC resources for spawned MCP server pods.
RBACPreset
¶
Bases: StrEnum
Preset RBAC configurations for service accounts.
Source code in src/mcp_ephemeral_k8s/k8s/rbac.py
14 15 16 17 18 | |
ServiceAccountConfig
¶
Bases: BaseModel
Configuration for ServiceAccount RBAC permissions.
Source code in src/mcp_ephemeral_k8s/k8s/rbac.py
29 30 31 32 33 34 35 36 37 38 39 | |
UnknownRBACPresetError
¶
Bases: ValueError
Exception raised when an unknown RBAC preset is encountered.
Source code in src/mcp_ephemeral_k8s/k8s/rbac.py
21 22 23 24 25 26 | |
create_service_account_for_job(core_v1, rbac_v1, job_name, namespace, cluster_wide=True, sa_config=None)
¶
Create a ServiceAccount and RBAC resources for a spawned MCP server job.
This creates: - A ServiceAccount - A Role/ClusterRole with permissions based on the ServiceAccountConfig - A RoleBinding/ClusterRoleBinding
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_v1
|
CoreV1Api
|
The Kubernetes core API client |
required |
rbac_v1
|
RbacAuthorizationV1Api
|
The Kubernetes RBAC API client |
required |
job_name
|
str
|
The name of the job (used for naming resources) |
required |
namespace
|
str
|
Kubernetes namespace |
required |
cluster_wide
|
bool
|
Whether to create ClusterRole/ClusterRoleBinding (default: True) Note: This parameter is deprecated when sa_config is provided |
True
|
sa_config
|
ServiceAccountConfig | None
|
ServiceAccount configuration with RBAC preset (default: minimal preset) |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The name of the created ServiceAccount |
Source code in src/mcp_ephemeral_k8s/k8s/rbac.py
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 | |
delete_service_account_for_job(core_v1, rbac_v1, job_name, namespace, cluster_wide=True)
¶
Delete the ServiceAccount and RBAC resources for a spawned MCP server job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_v1
|
CoreV1Api
|
The Kubernetes core API client |
required |
rbac_v1
|
RbacAuthorizationV1Api
|
The Kubernetes RBAC API client |
required |
job_name
|
str
|
The name of the job |
required |
namespace
|
str
|
Kubernetes namespace |
required |
cluster_wide
|
bool
|
Whether ClusterRole/ClusterRoleBinding were created (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if all resources were deleted successfully, False otherwise |
Source code in src/mcp_ephemeral_k8s/k8s/rbac.py
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 | |
Utilities¶
This module contains a utility function to generate unique identifiers for MCP ephemeral K8s resources based on RFC 1123 Label Names.
generate_unique_id(prefix=None, max_length=63)
¶
Generate a unique identifier that follows the Kubernetes naming rules (RFC 1123 Label Names).
RFC 1123 Label Names must: - Contain only lowercase alphanumeric characters or '-' - Start with an alphanumeric character - End with an alphanumeric character - Be at most 63 characters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str | None
|
Optional prefix for the ID. Will be converted to lowercase and non-compliant characters will be replaced with dashes. |
None
|
max_length
|
int
|
Maximum length of the generated ID, defaults to 63 (K8s limit). |
63
|
Returns:
| Type | Description |
|---|---|
str
|
A unique RFC 1123 compliant identifier string. |
Source code in src/mcp_ephemeral_k8s/k8s/uid.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 | |