Modules
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
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 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 |
|
__enter__()
¶
Enter the context manager.
Source code in src/mcp_ephemeral_k8s/session_manager.py
99 100 101 102 |
|
__exit__(exc_type, exc_val, exc_tb)
¶
Exit the context manager.
Source code in src/mcp_ephemeral_k8s/session_manager.py
104 105 106 107 |
|
create_mcp_server(config, wait_for_ready=True, expose_port=False)
¶
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 |
True
|
Returns:
Type | Description |
---|---|
EphemeralMcpServer
|
The MCP server instance |
Source code in src/mcp_ephemeral_k8s/session_manager.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
delete_mcp_server(pod_name, wait_for_deletion=True)
¶
Delete the MCP server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pod_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
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
expose_mcp_server_port(mcp_server)
¶
Expose the MCP server port to the outside world.
Source code in src/mcp_ephemeral_k8s/session_manager.py
226 227 228 |
|
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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
remove_mcp_server_port(mcp_server)
¶
Remove the MCP server port from the outside world.
Source code in src/mcp_ephemeral_k8s/session_manager.py
230 231 232 |
|
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, env=None, wait_for_ready=False)
¶
Create a new MCP server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
runtime_exec
|
str
|
The runtime to use for the MCP server (e.g. "uvx", "npx", "go run"). |
required |
runtime_mcp
|
str
|
The runtime to use for the MCP server (e.g. "mcp-server-fetch"). |
required |
env
|
dict[str, str] | None
|
The environment variables to set for the MCP server. |
None
|
wait_for_ready
|
bool
|
Whether to wait for the MCP server to be ready before returning. |
False
|
Source code in src/mcp_ephemeral_k8s/app/mcp.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
delete_mcp_server(ctx, pod_name, wait_for_deletion=False)
¶
Delete an MCP server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pod_name
|
str
|
The name of the MCP server to delete. |
required |
wait_for_deletion
|
bool
|
Whether to wait for the MCP server to be deleted before returning. |
False
|
Source code in src/mcp_ephemeral_k8s/app/mcp.py
67 68 69 70 71 72 73 74 75 76 |
|
get_mcp_server_status(ctx, pod_name)
¶
Get the status of an MCP server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pod_name
|
str
|
The name of the MCP server to get the status of. |
required |
Source code in src/mcp_ephemeral_k8s/app/mcp.py
79 80 81 82 83 84 85 86 87 |
|
get_version()
¶
Get the version of the MCP ephemeral server.
Source code in src/mcp_ephemeral_k8s/app/mcp.py
26 27 28 29 |
|
lifespan(server)
async
¶
Lifecycle hooks for the MCP ephemeral server.
Source code in src/mcp_ephemeral_k8s/app/mcp.py
13 14 15 16 17 18 19 |
|
list_mcp_servers(ctx)
¶
List all running MCP servers.
Source code in src/mcp_ephemeral_k8s/app/mcp.py
39 40 41 42 43 |
|
list_presets()
¶
List all preset configurations.
Source code in src/mcp_ephemeral_k8s/app/mcp.py
33 34 35 36 |
|
FastAPI application for the MCP ephemeral server.
lifespan(app)
async
¶
Lifecycle hooks for the MCP ephemeral server.
Source code in src/mcp_ephemeral_k8s/app/fastapi.py
14 15 16 17 18 19 20 21 22 23 |
|
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
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 |
|
EphemeralMcpServerConfig
¶
Bases: BaseModel
Configuration for Kubernetes resources.
Source code in src/mcp_ephemeral_k8s/api/ephemeral_mcp_server.py
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 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 |
|
image_name
property
¶
The name of the image to use for the MCP server.
job_name
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
116 117 118 119 120 121 122 123 124 125 126 |
|
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
73 74 75 76 77 78 79 80 81 82 83 84 |
|
KubernetesProbeConfig
¶
Bases: BaseModel
The configuration for the Kubernetes probe.
Source code in src/mcp_ephemeral_k8s/api/ephemeral_mcp_server.py
21 22 23 24 25 26 27 28 |
|
KubernetesRuntime
¶
Bases: str
, Enum
The runtime that is being used for Kubeconfig
Source code in src/mcp_ephemeral_k8s/api/ephemeral_mcp_server.py
14 15 16 17 18 |
|
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 |
|
check_pod_status(core_v1, pod_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 |
pod_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
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 |
|
create_mcp_server_job(config, namespace)
¶
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 |
Returns:
Type | Description |
---|---|
V1Job
|
The MCP server instance |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
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 59 60 61 |
|
delete_mcp_server_job(core_v1, batch_v1, pod_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 |
pod_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
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 |
|
expose_mcp_server_port(core_v1, pod_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 |
pod_name
|
str
|
Name of the pod |
required |
namespace
|
str
|
Kubernetes namespace |
required |
port
|
int
|
Port to expose |
required |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
get_mcp_server_job_status(batch_v1, pod_name, namespace)
¶
Get the status of a Kubernetes job.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_v1
|
BatchV1Api
|
The Kubernetes batch API client |
required |
pod_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
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 |
|
remove_mcp_server_port(core_v1, pod_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 |
pod_name
|
str
|
Name of the pod |
required |
namespace
|
str
|
Kubernetes namespace |
required |
Source code in src/mcp_ephemeral_k8s/k8s/job.py
292 293 294 295 296 297 298 299 300 301 302 |
|
wait_for_job_deletion(batch_v1, pod_name, namespace, sleep_time=1, max_wait_time=60)
¶
Wait for a job to be deleted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_v1
|
BatchV1Api
|
The Kubernetes batch API client |
required |
pod_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
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
wait_for_job_ready(batch_v1, core_v1, pod_name, namespace, sleep_time=1, max_wait_time=60)
¶
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 |
pod_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
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 |
|
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 |
|