levier-runtime 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- levier_runtime/__init__.py +57 -0
- levier_runtime/channel/__init__.py +5 -0
- levier_runtime/channel/client.py +317 -0
- levier_runtime/channel/envelope.py +57 -0
- levier_runtime/cli.py +116 -0
- levier_runtime/config.py +289 -0
- levier_runtime/context.py +409 -0
- levier_runtime/events.py +100 -0
- levier_runtime/ids.py +36 -0
- levier_runtime/llm/__init__.py +44 -0
- levier_runtime/llm/anthropic.py +410 -0
- levier_runtime/llm/base.py +134 -0
- levier_runtime/llm/http.py +133 -0
- levier_runtime/llm/openai.py +379 -0
- levier_runtime/llm/router.py +56 -0
- levier_runtime/manager.py +272 -0
- levier_runtime/mcp/__init__.py +8 -0
- levier_runtime/mcp/client.py +289 -0
- levier_runtime/profile.py +91 -0
- levier_runtime/react/__init__.py +11 -0
- levier_runtime/react/agent.py +527 -0
- levier_runtime/react/checkpoint.py +193 -0
- levier_runtime/runtime.py +139 -0
- levier_runtime/toolbox.py +261 -0
- levier_runtime/tools/__init__.py +23 -0
- levier_runtime/tools/base.py +131 -0
- levier_runtime/types.py +143 -0
- levier_runtime-0.1.0.dist-info/METADATA +101 -0
- levier_runtime-0.1.0.dist-info/RECORD +37 -0
- levier_runtime-0.1.0.dist-info/WHEEL +5 -0
- levier_runtime-0.1.0.dist-info/top_level.txt +2 -0
- middleware/__init__.py +1 -0
- middleware/runtime/__init__.py +1 -0
- middleware/runtime/v1/__init__.py +1 -0
- middleware/runtime/v1/runtime_pb2.py +98 -0
- middleware/runtime/v1/runtime_pb2.pyi +332 -0
- middleware/runtime/v1/runtime_pb2_grpc.py +84 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"""TaskAssign/Cancel/Reconcile lifecycle for one runtime instance.
|
|
2
|
+
|
|
3
|
+
This module is SDK-internal. Runtime authors only see `RunContext`.
|
|
4
|
+
|
|
5
|
+
Copyright 2026 The Levier Authors.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import asyncio
|
|
11
|
+
import logging
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from typing import Any, Protocol
|
|
14
|
+
|
|
15
|
+
from middleware.runtime.v1 import runtime_pb2
|
|
16
|
+
|
|
17
|
+
from .channel.client import ChannelClient
|
|
18
|
+
from .context import RunContext
|
|
19
|
+
from .events import EventWriter
|
|
20
|
+
from .types import (
|
|
21
|
+
McpBinding,
|
|
22
|
+
Message,
|
|
23
|
+
ModelConfig,
|
|
24
|
+
Timeouts,
|
|
25
|
+
ToolSpec,
|
|
26
|
+
params_from_proto,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
TERMINAL_STATUSES = {"completed", "failed", "cancelled"}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Runner(Protocol):
|
|
33
|
+
"""A runtime's `run(ctx)` function."""
|
|
34
|
+
|
|
35
|
+
async def __call__(self, ctx: RunContext) -> str:
|
|
36
|
+
"""Return a terminal status."""
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class NoopRunner:
|
|
40
|
+
"""Fallback runner used only for internal tests."""
|
|
41
|
+
|
|
42
|
+
async def __call__(self, ctx: RunContext) -> str:
|
|
43
|
+
return "completed"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass(frozen=True)
|
|
47
|
+
class ManagerConfig:
|
|
48
|
+
"""Runtime identity and capacity."""
|
|
49
|
+
|
|
50
|
+
runtime_name: str
|
|
51
|
+
max_concurrent_runs: int = 4
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class RunManager:
|
|
55
|
+
"""Tracks active runs and handles TaskAssign/Cancel/Reconcile."""
|
|
56
|
+
|
|
57
|
+
def __init__(
|
|
58
|
+
self,
|
|
59
|
+
sender: ChannelClient,
|
|
60
|
+
config: ManagerConfig,
|
|
61
|
+
runner: Runner,
|
|
62
|
+
*,
|
|
63
|
+
logger: logging.Logger | None = None,
|
|
64
|
+
) -> None:
|
|
65
|
+
self.sender = sender
|
|
66
|
+
self.config = config
|
|
67
|
+
self.runner = runner
|
|
68
|
+
self.logger = logger or logging.getLogger("levier_runtime.manager")
|
|
69
|
+
self.active: dict[str, RunContext] = {}
|
|
70
|
+
self._closing = False
|
|
71
|
+
|
|
72
|
+
async def handle(self, envelope: runtime_pb2.Envelope) -> None:
|
|
73
|
+
"""Dispatch one inbound RuntimeChannel envelope."""
|
|
74
|
+
payload = envelope.WhichOneof("payload")
|
|
75
|
+
if payload == "task_assign":
|
|
76
|
+
await self._handle_task_assign(envelope)
|
|
77
|
+
elif payload == "cancel":
|
|
78
|
+
await self._handle_cancel(envelope)
|
|
79
|
+
elif payload == "reconcile":
|
|
80
|
+
await self._handle_reconcile(envelope)
|
|
81
|
+
|
|
82
|
+
async def _handle_task_assign(self, envelope: runtime_pb2.Envelope) -> None:
|
|
83
|
+
assign = envelope.task_assign
|
|
84
|
+
reason = self._validation_error(envelope, assign)
|
|
85
|
+
if reason:
|
|
86
|
+
await self._send_ack(envelope, accepted=False, reason=reason)
|
|
87
|
+
return
|
|
88
|
+
|
|
89
|
+
existing = self.active.get(assign.run_id)
|
|
90
|
+
if existing is not None:
|
|
91
|
+
if existing._lease_epoch == envelope.lease_epoch:
|
|
92
|
+
await self._send_ack(envelope, accepted=True)
|
|
93
|
+
return
|
|
94
|
+
await self._send_ack(
|
|
95
|
+
envelope,
|
|
96
|
+
accepted=False,
|
|
97
|
+
reason="run already active with different lease_epoch",
|
|
98
|
+
)
|
|
99
|
+
return
|
|
100
|
+
|
|
101
|
+
if len(self.active) >= self.config.max_concurrent_runs:
|
|
102
|
+
await self._send_ack(envelope, accepted=False, reason="capacity")
|
|
103
|
+
return
|
|
104
|
+
|
|
105
|
+
await self._send_ack(envelope, accepted=True)
|
|
106
|
+
ctx = self._new_context(assign, envelope)
|
|
107
|
+
self.active[ctx.run_id] = ctx
|
|
108
|
+
await ctx.emit("RUN_STATUS", {"status": "running"})
|
|
109
|
+
ctx.task = asyncio.create_task(self._execute(ctx), name=f"run-{ctx.run_id}")
|
|
110
|
+
|
|
111
|
+
def _new_context(
|
|
112
|
+
self,
|
|
113
|
+
assign: runtime_pb2.TaskAssign,
|
|
114
|
+
envelope: runtime_pb2.Envelope,
|
|
115
|
+
) -> RunContext:
|
|
116
|
+
timeouts = assign.timeouts
|
|
117
|
+
events = EventWriter(
|
|
118
|
+
self.sender,
|
|
119
|
+
assign.run_id,
|
|
120
|
+
envelope.attempt,
|
|
121
|
+
envelope.lease_epoch,
|
|
122
|
+
)
|
|
123
|
+
ctx = RunContext(
|
|
124
|
+
runtime_name=self.config.runtime_name,
|
|
125
|
+
run_id=assign.run_id,
|
|
126
|
+
app_id=assign.app_id,
|
|
127
|
+
input_messages=[Message.from_proto(value) for value in assign.input],
|
|
128
|
+
params=params_from_proto(assign.params),
|
|
129
|
+
model=ModelConfig.from_proto(assign.model),
|
|
130
|
+
tools=[ToolSpec.from_proto(value) for value in assign.tools],
|
|
131
|
+
mcp_bindings=[McpBinding.from_proto(value) for value in assign.mcp_bindings],
|
|
132
|
+
timeouts=Timeouts(
|
|
133
|
+
run_ms=timeouts.run_timeout_ms if timeouts is not None else 0,
|
|
134
|
+
tool_ms=timeouts.tool_timeout_ms if timeouts is not None else 0,
|
|
135
|
+
),
|
|
136
|
+
session_id=assign.session_id or None,
|
|
137
|
+
resume_from=assign.resume_from.state or None,
|
|
138
|
+
trace_id=assign.trace_context,
|
|
139
|
+
channel=self.sender,
|
|
140
|
+
events=events,
|
|
141
|
+
attempt=envelope.attempt,
|
|
142
|
+
lease_epoch=envelope.lease_epoch,
|
|
143
|
+
)
|
|
144
|
+
ctx.task = None
|
|
145
|
+
return ctx
|
|
146
|
+
|
|
147
|
+
def _validation_error(
|
|
148
|
+
self,
|
|
149
|
+
envelope: runtime_pb2.Envelope,
|
|
150
|
+
assign: runtime_pb2.TaskAssign,
|
|
151
|
+
) -> str:
|
|
152
|
+
if envelope.run_id != assign.run_id:
|
|
153
|
+
return "run_id mismatch"
|
|
154
|
+
if not assign.run_id:
|
|
155
|
+
return "run_id is required"
|
|
156
|
+
if not assign.app_id:
|
|
157
|
+
return "app_id is required"
|
|
158
|
+
if envelope.attempt < 1:
|
|
159
|
+
return "attempt must be >= 1"
|
|
160
|
+
if envelope.lease_epoch < 1:
|
|
161
|
+
return "lease_epoch must be >= 1"
|
|
162
|
+
if self._closing:
|
|
163
|
+
return "runtime draining"
|
|
164
|
+
return ""
|
|
165
|
+
|
|
166
|
+
async def _send_ack(
|
|
167
|
+
self,
|
|
168
|
+
request: runtime_pb2.Envelope,
|
|
169
|
+
*,
|
|
170
|
+
accepted: bool,
|
|
171
|
+
reason: str = "",
|
|
172
|
+
) -> None:
|
|
173
|
+
ack = runtime_pb2.TaskAck(accepted=accepted, reason=reason)
|
|
174
|
+
await self.sender.send_response(request, ack, "task_ack")
|
|
175
|
+
|
|
176
|
+
async def _execute(self, ctx: RunContext) -> None:
|
|
177
|
+
status = "failed"
|
|
178
|
+
reason = ""
|
|
179
|
+
error_code = ""
|
|
180
|
+
try:
|
|
181
|
+
status = await self.runner(ctx)
|
|
182
|
+
if status not in TERMINAL_STATUSES:
|
|
183
|
+
reason = f"runtime returned invalid status {status!r}"
|
|
184
|
+
error_code = "INVALID_RUNTIME_STATUS"
|
|
185
|
+
status = "failed"
|
|
186
|
+
if ctx.cancelled:
|
|
187
|
+
status = "cancelled"
|
|
188
|
+
except asyncio.CancelledError:
|
|
189
|
+
status = "cancelled"
|
|
190
|
+
except Exception as exc: # runtime failures become a failed run
|
|
191
|
+
status = "failed"
|
|
192
|
+
reason = str(exc)
|
|
193
|
+
error_code = "RUNTIME_ERROR"
|
|
194
|
+
self.logger.error("runtime run failed", extra={
|
|
195
|
+
"run_id": ctx.run_id,
|
|
196
|
+
"error": str(exc),
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
if error_code and not ctx.cancelled:
|
|
200
|
+
await self._emit_error(ctx, error_code, reason)
|
|
201
|
+
if ctx.cancelled:
|
|
202
|
+
status = "cancelled"
|
|
203
|
+
ctx.outcome = status
|
|
204
|
+
data: dict[str, Any] = {"status": status}
|
|
205
|
+
if reason:
|
|
206
|
+
data["reason"] = reason
|
|
207
|
+
try:
|
|
208
|
+
await ctx.emit("RUN_STATUS", data)
|
|
209
|
+
except Exception as exc:
|
|
210
|
+
self.logger.error("runtime terminal event failed", extra={
|
|
211
|
+
"run_id": ctx.run_id,
|
|
212
|
+
"error": str(exc),
|
|
213
|
+
})
|
|
214
|
+
finally:
|
|
215
|
+
try:
|
|
216
|
+
await asyncio.shield(ctx.close_toolbox())
|
|
217
|
+
except asyncio.CancelledError:
|
|
218
|
+
pass
|
|
219
|
+
except Exception as exc:
|
|
220
|
+
self.logger.warning("close run toolbox failed", extra={
|
|
221
|
+
"run_id": ctx.run_id,
|
|
222
|
+
"error": str(exc),
|
|
223
|
+
})
|
|
224
|
+
self.active.pop(ctx.run_id, None)
|
|
225
|
+
|
|
226
|
+
async def _emit_error(self, ctx: RunContext, code: str, message: str) -> None:
|
|
227
|
+
try:
|
|
228
|
+
await ctx.error(code, message)
|
|
229
|
+
except Exception as exc:
|
|
230
|
+
self.logger.error("runtime error event failed", extra={
|
|
231
|
+
"run_id": ctx.run_id,
|
|
232
|
+
"error": str(exc),
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
async def _handle_cancel(self, envelope: runtime_pb2.Envelope) -> None:
|
|
236
|
+
self._cancel_run(envelope.run_id)
|
|
237
|
+
|
|
238
|
+
async def _handle_reconcile(self, envelope: runtime_pb2.Envelope) -> None:
|
|
239
|
+
for run_id in envelope.reconcile.cancel_run_ids:
|
|
240
|
+
self._cancel_run(run_id)
|
|
241
|
+
|
|
242
|
+
def _cancel_run(self, run_id: str) -> None:
|
|
243
|
+
ctx = self.active.get(run_id)
|
|
244
|
+
if ctx is None:
|
|
245
|
+
return
|
|
246
|
+
ctx.cancel_event.set()
|
|
247
|
+
if ctx.task is not None and not ctx.task.done():
|
|
248
|
+
ctx.task.cancel()
|
|
249
|
+
|
|
250
|
+
def running_runs(self) -> list[tuple[str, int]]:
|
|
251
|
+
"""Snapshot for Hello.running_runs."""
|
|
252
|
+
return [
|
|
253
|
+
(ctx.run_id, ctx._lease_epoch)
|
|
254
|
+
for ctx in self.active.values()
|
|
255
|
+
]
|
|
256
|
+
|
|
257
|
+
def running_count(self) -> int:
|
|
258
|
+
"""Current active run count for Heartbeat."""
|
|
259
|
+
return len(self.active)
|
|
260
|
+
|
|
261
|
+
async def close(self) -> None:
|
|
262
|
+
"""Cancel active runs and wait for their tasks."""
|
|
263
|
+
self._closing = True
|
|
264
|
+
tasks: list[asyncio.Task[Any]] = []
|
|
265
|
+
for ctx in list(self.active.values()):
|
|
266
|
+
ctx.cancel_event.set()
|
|
267
|
+
if ctx.task is not None and not ctx.task.done():
|
|
268
|
+
ctx.task.cancel()
|
|
269
|
+
tasks.append(ctx.task)
|
|
270
|
+
if tasks:
|
|
271
|
+
await asyncio.gather(*tasks, return_exceptions=True)
|
|
272
|
+
self.active.clear()
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"""Levier ? Runtime SDK client for the independent MCP runtime.
|
|
2
|
+
|
|
3
|
+
The client speaks newline-delimited JSON-RPC over a Unix domain socket. It
|
|
4
|
+
never imports the MCP SDK and never holds an MCP connection itself.
|
|
5
|
+
|
|
6
|
+
Copyright 2026 The Levier Authors.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import asyncio
|
|
12
|
+
import json
|
|
13
|
+
from dataclasses import dataclass, field
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from ..types import McpBinding, ToolResult, ToolSpec
|
|
17
|
+
|
|
18
|
+
_DEFAULT_TIMEOUT_SECONDS = 30.0
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class McpRuntimeError(RuntimeError):
|
|
22
|
+
"""A normalized MCP runtime failure."""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
code: str,
|
|
27
|
+
message: str,
|
|
28
|
+
*,
|
|
29
|
+
retryable: bool = False,
|
|
30
|
+
) -> None:
|
|
31
|
+
super().__init__(message)
|
|
32
|
+
self.code = code
|
|
33
|
+
self.retryable = retryable
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class McpSessionInfo:
|
|
38
|
+
"""One open MCP session and its namespaced tools."""
|
|
39
|
+
|
|
40
|
+
session_id: str
|
|
41
|
+
tools: list[ToolSpec] = field(default_factory=list)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class McpRuntimeClient:
|
|
45
|
+
"""JSON-RPC client for `mcp_runtime`."""
|
|
46
|
+
|
|
47
|
+
def __init__(
|
|
48
|
+
self,
|
|
49
|
+
socket_path: str,
|
|
50
|
+
*,
|
|
51
|
+
default_timeout: float = _DEFAULT_TIMEOUT_SECONDS,
|
|
52
|
+
) -> None:
|
|
53
|
+
if not socket_path:
|
|
54
|
+
raise ValueError("socket_path is required")
|
|
55
|
+
self.socket_path = socket_path
|
|
56
|
+
self.default_timeout = default_timeout
|
|
57
|
+
self._reader: asyncio.StreamReader | None = None
|
|
58
|
+
self._writer: asyncio.StreamWriter | None = None
|
|
59
|
+
self._reader_task: asyncio.Task[None] | None = None
|
|
60
|
+
self._connect_lock = asyncio.Lock()
|
|
61
|
+
self._pending: dict[int, asyncio.Future[dict[str, Any]]] = {}
|
|
62
|
+
self._next_id = 1
|
|
63
|
+
self._closing = False
|
|
64
|
+
|
|
65
|
+
async def health(self) -> dict[str, Any]:
|
|
66
|
+
"""Return runtime health."""
|
|
67
|
+
result = await self._request("mcp.health", {})
|
|
68
|
+
return result if isinstance(result, dict) else {}
|
|
69
|
+
|
|
70
|
+
async def open(self, binding: McpBinding) -> McpSessionInfo:
|
|
71
|
+
"""Open one binding and return its namespaced tools."""
|
|
72
|
+
result = await self._request("mcp.open", _binding_dict(binding), timeout=60.0)
|
|
73
|
+
if not isinstance(result, dict):
|
|
74
|
+
raise McpRuntimeError("BAD_RESPONSE", "mcp.open returned an invalid result")
|
|
75
|
+
tools = [
|
|
76
|
+
ToolSpec(
|
|
77
|
+
name=str(tool.get("name", "")),
|
|
78
|
+
description=str(tool.get("description", "")),
|
|
79
|
+
input_schema_json=str(tool.get("input_schema_json", "")),
|
|
80
|
+
)
|
|
81
|
+
for tool in result.get("tools", [])
|
|
82
|
+
if isinstance(tool, dict)
|
|
83
|
+
]
|
|
84
|
+
return McpSessionInfo(
|
|
85
|
+
session_id=str(result.get("session_id", "")),
|
|
86
|
+
tools=tools,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
async def call(
|
|
90
|
+
self,
|
|
91
|
+
session_id: str,
|
|
92
|
+
tool_name: str,
|
|
93
|
+
arguments_json: str,
|
|
94
|
+
timeout_ms: int,
|
|
95
|
+
) -> ToolResult:
|
|
96
|
+
"""Call one namespaced MCP tool."""
|
|
97
|
+
timeout = max(1, timeout_ms) / 1000
|
|
98
|
+
result = await self._request(
|
|
99
|
+
"mcp.call",
|
|
100
|
+
{
|
|
101
|
+
"session_id": session_id,
|
|
102
|
+
"tool_name": tool_name,
|
|
103
|
+
"arguments_json": arguments_json,
|
|
104
|
+
"timeout_ms": timeout_ms,
|
|
105
|
+
},
|
|
106
|
+
timeout=timeout + 5,
|
|
107
|
+
)
|
|
108
|
+
if not isinstance(result, dict):
|
|
109
|
+
raise McpRuntimeError("BAD_RESPONSE", "mcp.call returned an invalid result")
|
|
110
|
+
if bool(result.get("ok")):
|
|
111
|
+
return ToolResult(ok=True, result_json=str(result.get("result_json", "")))
|
|
112
|
+
error = result.get("error") or {}
|
|
113
|
+
return ToolResult(
|
|
114
|
+
ok=False,
|
|
115
|
+
error_code=str(error.get("code", "TOOL_EXEC_ERROR")),
|
|
116
|
+
error_message=str(error.get("message", "")),
|
|
117
|
+
retryable=bool(error.get("retryable", False)),
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
async def close_session(self, session_id: str) -> None:
|
|
121
|
+
"""Close one MCP session."""
|
|
122
|
+
if not session_id:
|
|
123
|
+
return
|
|
124
|
+
await self._request("mcp.close", {"session_id": session_id})
|
|
125
|
+
|
|
126
|
+
async def close(self) -> None:
|
|
127
|
+
"""Close the client and its connection."""
|
|
128
|
+
self._closing = True
|
|
129
|
+
self._fail_pending(McpRuntimeError("MCP_RUNTIME_UNAVAILABLE", "client closed"))
|
|
130
|
+
if self._reader_task is not None:
|
|
131
|
+
self._reader_task.cancel()
|
|
132
|
+
try:
|
|
133
|
+
await self._reader_task
|
|
134
|
+
except asyncio.CancelledError:
|
|
135
|
+
pass
|
|
136
|
+
self._reader_task = None
|
|
137
|
+
if self._writer is not None:
|
|
138
|
+
self._writer.close()
|
|
139
|
+
try:
|
|
140
|
+
await self._writer.wait_closed()
|
|
141
|
+
except ConnectionError:
|
|
142
|
+
pass
|
|
143
|
+
self._writer = None
|
|
144
|
+
self._reader = None
|
|
145
|
+
|
|
146
|
+
async def _request(
|
|
147
|
+
self,
|
|
148
|
+
method: str,
|
|
149
|
+
params: dict[str, Any],
|
|
150
|
+
*,
|
|
151
|
+
timeout: float | None = None,
|
|
152
|
+
) -> Any:
|
|
153
|
+
await self._ensure_connected()
|
|
154
|
+
assert self._writer is not None
|
|
155
|
+
request_id = self._next_id
|
|
156
|
+
self._next_id += 1
|
|
157
|
+
loop = asyncio.get_running_loop()
|
|
158
|
+
future: asyncio.Future[dict[str, Any]] = loop.create_future()
|
|
159
|
+
self._pending[request_id] = future
|
|
160
|
+
payload = {
|
|
161
|
+
"jsonrpc": "2.0",
|
|
162
|
+
"id": request_id,
|
|
163
|
+
"method": method,
|
|
164
|
+
"params": params,
|
|
165
|
+
}
|
|
166
|
+
try:
|
|
167
|
+
self._writer.write((json.dumps(payload, ensure_ascii=False) + "\n").encode("utf-8"))
|
|
168
|
+
await self._writer.drain()
|
|
169
|
+
response = await asyncio.wait_for(
|
|
170
|
+
future,
|
|
171
|
+
timeout if timeout is not None else self.default_timeout,
|
|
172
|
+
)
|
|
173
|
+
except asyncio.TimeoutError as exc:
|
|
174
|
+
raise McpRuntimeError(
|
|
175
|
+
"MCP_RUNTIME_TIMEOUT",
|
|
176
|
+
f"MCP runtime did not answer {method!r} in time",
|
|
177
|
+
retryable=True,
|
|
178
|
+
) from exc
|
|
179
|
+
finally:
|
|
180
|
+
self._pending.pop(request_id, None)
|
|
181
|
+
|
|
182
|
+
error = response.get("error")
|
|
183
|
+
if isinstance(error, dict):
|
|
184
|
+
data = error.get("data") or {}
|
|
185
|
+
raise McpRuntimeError(
|
|
186
|
+
str(error.get("code", "MCP_RUNTIME_ERROR")),
|
|
187
|
+
str(error.get("message", "MCP runtime error")),
|
|
188
|
+
retryable=bool(data.get("retryable", False)),
|
|
189
|
+
)
|
|
190
|
+
return response.get("result")
|
|
191
|
+
|
|
192
|
+
async def _ensure_connected(self) -> None:
|
|
193
|
+
if self._writer is not None and not self._closing:
|
|
194
|
+
return
|
|
195
|
+
async with self._connect_lock:
|
|
196
|
+
if self._writer is not None and not self._closing:
|
|
197
|
+
return
|
|
198
|
+
if self._closing:
|
|
199
|
+
raise McpRuntimeError("MCP_RUNTIME_UNAVAILABLE", "client is closed")
|
|
200
|
+
try:
|
|
201
|
+
reader, writer = await _open_connection(self.socket_path)
|
|
202
|
+
except OSError as exc:
|
|
203
|
+
raise McpRuntimeError(
|
|
204
|
+
"MCP_RUNTIME_UNAVAILABLE",
|
|
205
|
+
f"connect MCP runtime at {self.socket_path}: {exc}",
|
|
206
|
+
retryable=True,
|
|
207
|
+
) from exc
|
|
208
|
+
self._reader = reader
|
|
209
|
+
self._writer = writer
|
|
210
|
+
self._reader_task = asyncio.create_task(
|
|
211
|
+
self._read_loop(reader, writer),
|
|
212
|
+
name="mcp-runtime-reader",
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
async def _read_loop(
|
|
216
|
+
self,
|
|
217
|
+
reader: asyncio.StreamReader,
|
|
218
|
+
writer: asyncio.StreamWriter,
|
|
219
|
+
) -> None:
|
|
220
|
+
try:
|
|
221
|
+
while True:
|
|
222
|
+
line = await reader.readline()
|
|
223
|
+
if not line:
|
|
224
|
+
break
|
|
225
|
+
try:
|
|
226
|
+
response = json.loads(line.decode("utf-8"))
|
|
227
|
+
except (UnicodeDecodeError, json.JSONDecodeError):
|
|
228
|
+
continue
|
|
229
|
+
if not isinstance(response, dict):
|
|
230
|
+
continue
|
|
231
|
+
request_id = response.get("id")
|
|
232
|
+
if not isinstance(request_id, int):
|
|
233
|
+
continue
|
|
234
|
+
future = self._pending.get(request_id)
|
|
235
|
+
if future is not None and not future.done():
|
|
236
|
+
future.set_result(response)
|
|
237
|
+
finally:
|
|
238
|
+
if self._writer is writer:
|
|
239
|
+
self._writer = None
|
|
240
|
+
self._reader = None
|
|
241
|
+
self._fail_pending(
|
|
242
|
+
McpRuntimeError(
|
|
243
|
+
"MCP_RUNTIME_UNAVAILABLE",
|
|
244
|
+
"MCP runtime connection closed",
|
|
245
|
+
retryable=True,
|
|
246
|
+
)
|
|
247
|
+
)
|
|
248
|
+
writer.close()
|
|
249
|
+
try:
|
|
250
|
+
await writer.wait_closed()
|
|
251
|
+
except ConnectionError:
|
|
252
|
+
pass
|
|
253
|
+
|
|
254
|
+
def _fail_pending(self, exc: BaseException) -> None:
|
|
255
|
+
pending = list(self._pending.values())
|
|
256
|
+
self._pending.clear()
|
|
257
|
+
for future in pending:
|
|
258
|
+
if not future.done():
|
|
259
|
+
future.set_exception(exc)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
async def _open_connection(endpoint: str):
|
|
263
|
+
"""Open a Unix socket or a TCP endpoint such as tcp://127.0.0.1:9091."""
|
|
264
|
+
value = endpoint.strip()
|
|
265
|
+
if value.startswith("tcp://"):
|
|
266
|
+
value = value[len("tcp://") :]
|
|
267
|
+
host, _, port = value.rpartition(":")
|
|
268
|
+
if not host or not port:
|
|
269
|
+
raise OSError(f"invalid MCP runtime TCP endpoint: {endpoint!r}")
|
|
270
|
+
return await asyncio.open_connection(host, int(port))
|
|
271
|
+
if "://" not in value and "/" not in value and "\\" not in value and ":" in value:
|
|
272
|
+
host, _, port = value.rpartition(":")
|
|
273
|
+
if host and port.isdigit():
|
|
274
|
+
return await asyncio.open_connection(host, int(port))
|
|
275
|
+
return await asyncio.open_unix_connection(value)
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _binding_dict(binding: McpBinding) -> dict[str, Any]:
|
|
279
|
+
return {
|
|
280
|
+
"server_name": binding.server_name,
|
|
281
|
+
"transport": binding.transport,
|
|
282
|
+
"command": binding.command,
|
|
283
|
+
"args": list(binding.args),
|
|
284
|
+
"env": dict(binding.env),
|
|
285
|
+
"url": binding.url,
|
|
286
|
+
"headers": dict(binding.headers),
|
|
287
|
+
"allowed_tools": list(binding.allowed_tools),
|
|
288
|
+
"namespace": binding.namespace,
|
|
289
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Read the user-level Levier CLI profile.
|
|
2
|
+
|
|
3
|
+
The CLI owns the profile file; runtimes only use it as a fallback when no
|
|
4
|
+
explicit ``--config`` or environment variable is provided. Runtime authors do
|
|
5
|
+
not need to parse this file themselves.
|
|
6
|
+
|
|
7
|
+
Copyright 2026 The Levier Authors.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
import sys
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def profile_path() -> Path:
|
|
18
|
+
"""Return the same profile path used by the Go CLI."""
|
|
19
|
+
value = os.environ.get("LEVIER_HOME")
|
|
20
|
+
if value:
|
|
21
|
+
return Path(value).expanduser() / "config.yaml"
|
|
22
|
+
if os.name == "nt":
|
|
23
|
+
base = os.environ.get("APPDATA")
|
|
24
|
+
if base:
|
|
25
|
+
return Path(base) / "levier" / "config.yaml"
|
|
26
|
+
elif sys.platform == "darwin":
|
|
27
|
+
return Path.home() / "Library" / "Application Support" / "levier" / "config.yaml"
|
|
28
|
+
else:
|
|
29
|
+
base = os.environ.get("XDG_CONFIG_HOME")
|
|
30
|
+
if base:
|
|
31
|
+
return Path(base) / "levier" / "config.yaml"
|
|
32
|
+
return Path.home() / ".config" / "levier" / "config.yaml"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def profile_values() -> dict[str, str]:
|
|
36
|
+
"""Read the flat profile without adding another YAML dependency."""
|
|
37
|
+
try:
|
|
38
|
+
lines = profile_path().read_text(encoding="utf-8").splitlines()
|
|
39
|
+
except OSError:
|
|
40
|
+
return {}
|
|
41
|
+
values: dict[str, str] = {}
|
|
42
|
+
for raw in lines:
|
|
43
|
+
line = raw.strip()
|
|
44
|
+
if not line or line.startswith("#") or ":" not in line:
|
|
45
|
+
continue
|
|
46
|
+
key, value = line.split(":", 1)
|
|
47
|
+
key = key.strip()
|
|
48
|
+
value = value.strip()
|
|
49
|
+
if not key:
|
|
50
|
+
continue
|
|
51
|
+
if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}:
|
|
52
|
+
value = value[1:-1]
|
|
53
|
+
values[key] = value
|
|
54
|
+
return values
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def default_runtime_config() -> str:
|
|
58
|
+
"""Return the configured Runtime YAML path, or an empty string."""
|
|
59
|
+
return profile_values().get("runtime_config", "")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def default_mcp_socket() -> str:
|
|
63
|
+
"""Return the configured MCP Runtime socket, or an empty string."""
|
|
64
|
+
return profile_values().get("mcp_socket", "")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def default_runtime_name() -> str:
|
|
68
|
+
"""Return the process name used when no runtime YAML is supplied."""
|
|
69
|
+
return profile_values().get("runtime_name", "")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def default_runtime_token() -> str:
|
|
73
|
+
"""Return the local runtime credential, if one is in the user profile."""
|
|
74
|
+
return profile_values().get("runtime_token", "")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def default_middleware_address() -> str:
|
|
78
|
+
"""Return the gRPC address derived by ``levier up``/``levier setup``."""
|
|
79
|
+
value = profile_values().get("middleware_address", "")
|
|
80
|
+
if value:
|
|
81
|
+
return value
|
|
82
|
+
server = profile_values().get("server", "")
|
|
83
|
+
if not server:
|
|
84
|
+
return ""
|
|
85
|
+
from urllib.parse import urlparse
|
|
86
|
+
|
|
87
|
+
parsed = urlparse(server if "://" in server else f"http://{server}")
|
|
88
|
+
host = parsed.hostname or ""
|
|
89
|
+
if not host:
|
|
90
|
+
return ""
|
|
91
|
+
return f"{host}:9090"
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Optional ReAct helper for Levier runtimes.
|
|
2
|
+
|
|
3
|
+
This package is optional. The Runtime SDK core does not import it.
|
|
4
|
+
|
|
5
|
+
Copyright 2026 The Levier Authors.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .agent import ReactAgent
|
|
9
|
+
from .checkpoint import CheckpointError, CheckpointState, capture
|
|
10
|
+
|
|
11
|
+
__all__ = ["CheckpointError", "CheckpointState", "ReactAgent", "capture"]
|