-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathlayerlens_server.py
More file actions
350 lines (286 loc) · 12.1 KB
/
Copy pathlayerlens_server.py
File metadata and controls
350 lines (286 loc) · 12.1 KB
1
2
3
4
5
6
7
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
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
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
"""LayerLens MCP Server -- powered by the LayerLens Python SDK.
An MCP (Model Context Protocol) server that exposes LayerLens trace
inspection, evaluation, and judge management capabilities as tools that
any MCP-compatible AI assistant can invoke.
Prerequisites
-------------
pip install layerlens --index-url https://sdk.layerlens.ai/package mcp
Environment
-----------
LAYERLENS_STRATIX_API_KEY API key used by the SDK (picked up
automatically by ``Stratix()``).
Usage
-----
python layerlens_server.py
"""
from __future__ import annotations
import os
import sys
import json
import asyncio
import threading
from typing import Any, Optional
from mcp.types import Tool, TextContent
from mcp.server import Server
from mcp.server.stdio import stdio_server
from layerlens import Stratix, StratixError, NotFoundError
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from _helpers import create_judge as _create_judge_helper
# ---------------------------------------------------------------------------
# Lazy-initialised SDK client
# ---------------------------------------------------------------------------
_client_lock = threading.Lock()
_client: Optional[Stratix] = None
def _get_client() -> Stratix:
"""Return the module-level Stratix client, creating it on first use."""
global _client
if _client is None:
with _client_lock:
if _client is None: # double-check after acquiring lock
_client = Stratix()
return _client
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _obj_to_text(obj: Any) -> str:
"""Convert a Pydantic model, dict, or list to a readable string."""
if obj is None:
return "(no data)"
if hasattr(obj, "model_dump"):
return json.dumps(obj.model_dump(), indent=2, default=str)
if isinstance(obj, (dict, list)):
return json.dumps(obj, indent=2, default=str)
return str(obj)
def _error_text(exc: Exception) -> str:
"""Format an exception into a user-friendly error message."""
if isinstance(exc, NotFoundError):
return f"Not found: {exc}"
if isinstance(exc, StratixError):
return f"LayerLens API error: {exc}"
return f"Error: {exc}"
# ---------------------------------------------------------------------------
# MCP server factory
# ---------------------------------------------------------------------------
def create_server() -> Server:
"""Create and return the MCP server with all tool handlers wired up."""
server = Server("layerlens")
# ----- tool catalogue ------------------------------------------------
@server.list_tools()
async def list_tools() -> list[Tool]:
return [
Tool(
name="list_traces",
description=(
"List traces stored in LayerLens. Returns the most recent traces with optional pagination."
),
inputSchema={
"type": "object",
"properties": {
"limit": {
"type": "integer",
"description": "Maximum number of traces to return (default 20, max 500).",
},
},
},
),
Tool(
name="get_trace",
description="Retrieve the full details of a single trace by its ID.",
inputSchema={
"type": "object",
"properties": {
"trace_id": {
"type": "string",
"description": "The trace ID.",
},
},
"required": ["trace_id"],
},
),
Tool(
name="run_evaluation",
description=(
"Run a judge evaluation against a trace. Returns the "
"created trace-evaluation object (initially in pending state)."
),
inputSchema={
"type": "object",
"properties": {
"trace_id": {
"type": "string",
"description": "The trace ID to evaluate.",
},
"judge_id": {
"type": "string",
"description": "The judge ID to use for evaluation.",
},
},
"required": ["trace_id", "judge_id"],
},
),
Tool(
name="get_evaluation",
description=(
"Get the status and results of a trace evaluation. "
"If the evaluation has completed, the results are included."
),
inputSchema={
"type": "object",
"properties": {
"evaluation_id": {
"type": "string",
"description": "The trace-evaluation ID.",
},
},
"required": ["evaluation_id"],
},
),
Tool(
name="create_judge",
description="Create a new evaluation judge with a name and goal.",
inputSchema={
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Display name for the judge.",
},
"goal": {
"type": "string",
"description": "The evaluation goal / criteria the judge should assess.",
},
},
"required": ["name", "goal"],
},
),
Tool(
name="list_judges",
description="List all judges configured in the current project.",
inputSchema={
"type": "object",
"properties": {},
},
),
]
# ----- tool dispatcher -----------------------------------------------
@server.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
try:
client = _get_client()
if name == "list_traces":
return await _handle_list_traces(client, arguments)
if name == "get_trace":
return await _handle_get_trace(client, arguments)
if name == "run_evaluation":
return await _handle_run_evaluation(client, arguments)
if name == "get_evaluation":
return await _handle_get_evaluation(client, arguments)
if name == "create_judge":
return await _handle_create_judge(client, arguments)
if name == "list_judges":
return await _handle_list_judges(client, arguments)
return [TextContent(type="text", text=f"Unknown tool: {name}")]
except Exception as exc:
return [TextContent(type="text", text=_error_text(exc))]
return server
# ---------------------------------------------------------------------------
# Individual tool handlers
#
# Each handler wraps synchronous SDK calls in asyncio.to_thread() so they
# do not block the async MCP event loop.
# ---------------------------------------------------------------------------
async def _handle_list_traces(client: Stratix, arguments: dict) -> list[TextContent]:
limit = arguments.get("limit", 20)
resp = await asyncio.to_thread(client.traces.get_many, page_size=limit, sort_by="created_at", sort_order="desc")
if resp is None:
return [TextContent(type="text", text="No traces found.")]
lines: list[str] = [f"Traces (showing {resp.count} of {resp.total_count}):"]
for t in resp.traces:
eval_info = ""
if t.evaluations_count:
eval_info = f" | {t.evaluations_count} evaluation(s)"
lines.append(f" - {t.id} created={t.created_at} file={t.filename}{eval_info}")
return [TextContent(type="text", text="\n".join(lines))]
async def _handle_get_trace(client: Stratix, arguments: dict) -> list[TextContent]:
trace_id: str = arguments["trace_id"]
trace = await asyncio.to_thread(client.traces.get, trace_id)
if trace is None:
return [TextContent(type="text", text=f"Trace {trace_id} not found.")]
return [TextContent(type="text", text=_obj_to_text(trace))]
async def _handle_run_evaluation(client: Stratix, arguments: dict) -> list[TextContent]:
trace_id: str = arguments["trace_id"]
judge_id: str = arguments["judge_id"]
evaluation = await asyncio.to_thread(client.trace_evaluations.create, trace_id=trace_id, judge_id=judge_id)
if evaluation is None:
return [TextContent(type="text", text="Failed to create evaluation.")]
return [
TextContent(
type="text",
text=(
f"Evaluation created.\n"
f" ID: {evaluation.id}\n"
f" Status: {evaluation.status}\n"
f" Trace: {trace_id}\n"
f" Judge: {judge_id}"
),
)
]
async def _handle_get_evaluation(client: Stratix, arguments: dict) -> list[TextContent]:
eid: str = arguments["evaluation_id"]
evaluation = await asyncio.to_thread(client.trace_evaluations.get, eid)
if evaluation is None:
return [TextContent(type="text", text=f"Evaluation {eid} not found.")]
parts: list[str] = [
f"Evaluation {eid}:",
f" Status: {evaluation.status}",
]
# If the evaluation finished, fetch and append results.
if (
hasattr(evaluation.status, "value")
and evaluation.status.value == "success"
or str(evaluation.status) == "success"
):
results_resp = await asyncio.to_thread(client.trace_evaluations.get_results, id=eid)
if results_resp and results_resp.score is not None:
r = results_resp
parts.append("")
parts.append(f" Result:")
parts.append(f" Score: {r.score}")
parts.append(f" Passed: {r.passed}")
parts.append(f" Reasoning: {r.reasoning}")
parts.append(f" Latency: {r.latency_ms} ms")
parts.append(f" Cost: {r.total_cost}")
return [TextContent(type="text", text="\n".join(parts))]
async def _handle_create_judge(client: Stratix, arguments: dict) -> list[TextContent]:
name: str = arguments["name"]
goal: str = arguments["goal"]
judge = await asyncio.to_thread(_create_judge_helper, client, name=name, evaluation_goal=goal)
if judge is None:
return [TextContent(type="text", text="Failed to create judge.")]
return [
TextContent(
type="text",
text=(f"Judge created.\n ID: {judge.id}\n Name: {judge.name}"),
)
]
async def _handle_list_judges(client: Stratix, arguments: dict) -> list[TextContent]:
resp = await asyncio.to_thread(client.judges.get_many)
if resp is None or not resp.judges:
return [TextContent(type="text", text="No judges found.")]
lines: list[str] = [f"Judges ({len(resp.judges)}):"]
for j in resp.judges:
lines.append(f" - {j.id} name={j.name!r}")
return [TextContent(type="text", text="\n".join(lines))]
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
async def _run() -> None:
server = create_server()
options = server.create_initialization_options()
async with stdio_server() as (read_stream, write_stream):
await server.run(read_stream, write_stream, options)
def main() -> None:
"""Run the MCP server over stdio."""
asyncio.run(_run())
if __name__ == "__main__":
main()