The platform for continuously improving AI agents.
"""Project-scoped MCP resources backed by existing Overmind rows."""
from __future__ import annotations
import hashlib
import json
import math
from collections import Counter
from collections.abc import Iterable
from urllib.parse import parse_qs, quote, unquote, urlparse
from asgiref.sync import sync_to_async
from django.conf import settings
from django.db.models import Q
from mcp import types
from mcp.server.lowlevel.helper_types import ReadResourceContents
from mcp.shared.exceptions import McpError
from pydantic import AnyUrl, ValidationError
from modal_shared.decisions import DECISION_OBJECTIVES
from modal_shared.training_telemetry import STARTUP_LABELS
from overbae.api.eval_serializers import run_progress
from overbae.core.errors import DatasetError, InputValidationError
from overbae.innermind import comparison
from overbae.innermind import context as eval_context
from overbae.innermind.definitions import definition_source
from overbae.models import (
Capability,
Cell,
ContextSnapshot,
Dataset,
DatasetPipeline,
DatasetPipelineBinding,
DatasetPipelinePackage,
DatasetPipelineRun,
DatasetTransfer,
DecisionJob,
DeployedModel,
EvalMatrix,
EvalRun,
EvalSet,
FinetuningJob,
InferenceRequest,
ModelActivation,
ResearchExperiment,
SignalEvent,
SignalSubscription,
Span,
TrainingCheckpoint,
TrainingPreparation,
)
from overbae.services import (
activity,
context_graph,
dedicated,
dedicated_progress,
inference_requests,
model_workflows,
operational_progress,
research_triggers,
training_monitoring,
)
from overbae.services.codebase.flow import build_capability_flow
from overbae.services.connectors.progress import sync_progress
from overbae.services.datasets import paths, pipeline_bindings, pipeline_packages, workbench
from overbae.services.datasets.admission import status as landing_admission_status
from overbae.services.datasets.imports import status as import_status
from overbae.services.deployment import deployment_progress
from overbae.services.entity_resolution import (
resolve_capability,
resolve_connector,
resolve_dataset,
resolve_eval_run,
resolve_session,
)
from overbae.services.inference_live import worker_status
from overbae.services.inference_metrics import model_activity, model_metrics, monitoring_options
from overbae.services.mcp import CONTRACT_VERSION
from overbae.services.mcp.context import get_context, project_context
from overbae.services.mcp.contracts.activity import InspectActivityInput, InspectActivityOutput
from overbae.services.mcp.contracts.datasets import (
next_actions,
serialize_dataset_detail,
)
from overbae.services.mcp.contracts.instrumentation import MAX_INSTRUMENTATION_SPANS
from overbae.services.mcp.errors import MCPError, error_payload, internal_error
from overbae.services.mcp.references import project_references, uuid_reference
from overbae.services.model_activation import activation_progress
from overbae.services.training_preparation import live_progress_for_job
from overbae.services.training_record import run_record
from sandbox.documents.languages import LANGUAGES
JSON_MIME = "application/json"
_MAX_EVENTS = 20
_MAX_LIST = 50
_ERROR_CAP = 1_000
def _normalize_key(key: object) -> str:
return "".join(character for character in str(key).casefold() if character.isalnum())
def _contains_key_part(key: object, parts: Iterable[str]) -> bool:
normalized = _normalize_key(key)
return any(part in normalized for part in parts)
_SENSITIVE_PARTS = frozenset(
{
"access_token",
"api_key",
"authorization",
"cookie",
"credential",
"password",
"private_key",
"refresh_token",
"secret",
"token",
}
)
_SENSITIVE_COMPACT_PARTS = frozenset(_normalize_key(part) for part in _SENSITIVE_PARTS)
_TOKEN_MEASUREMENTS = frozenset(
{
"tokens",
"supervised_tokens",
"trained_tokens",
"max_tokens",
"max_new_tokens",
"padded_tokens",
"padded_token_budget",
"tokens_per_second",
"tokens_processed",
"total_tokens",
"num_tokens",
"max_token_length",
}
)
_FINETUNE_PROGRESS_BLOCKED_PARTS = frozenset(
{"artifact", "artifacts", "checkpoint", "checkpoints", "download", "presigned", "uri", "url"}
)
_FINETUNE_PROGRESS_BLOCKED_COMPACT_PARTS = frozenset(
_normalize_key(part) for part in _FINETUNE_PROGRESS_BLOCKED_PARTS
)
_FINETUNE_PROGRESS_REDACTED_PARTS = (
_SENSITIVE_COMPACT_PARTS | _FINETUNE_PROGRESS_BLOCKED_COMPACT_PARTS
)
_CONNECTOR_MAPPING_SOURCES = frozenset({"observation_name", "metadata", "tag", "trace_name"})
def _connector_setup_resource(uri: str) -> dict:
return {
"uri": uri,
"kind": "connector_setup",
"command": "overmind connector add langfuse --json",
"types": [
{
"connector_type": "langfuse",
"auth": "pair",
"command": "overmind connector add langfuse --json",
"env": ["LANGFUSE_PUBLIC_KEY", "LANGFUSE_SECRET_KEY"],
},
{
"connector_type": "langsmith",
"auth": "bearer",
"command": "overmind connector add langsmith --json",
"env": ["LANGSMITH_API_KEY"],
},
{
"connector_type": "braintrust",
"auth": "bearer",
"command": "overmind connector add braintrust --json",
"env": ["BRAINTRUST_API_KEY"],
},
{
"connector_type": "galileo",
"auth": "bearer",
"command": "overmind connector add galileo --json",
"env": ["GALILEO_API_KEY"],
},
],
"auth": (
"Overmind key from --api-key, .overmind/credentials.toml, or OVERMIND_API_KEY. "
"project-id must be this MCP project."
),
"provider_env": (
"Provider keys come from those env names or a TTY prompt. Generic fallback: "
"OVERMIND_CONNECTOR_API_KEY and OVERMIND_CONNECTOR_API_SECRET."
),
"boundary": (
"The human runs the CLI in a terminal. Never paste provider keys in chat. "
"Do not have the agent export keys or run the command in a non-TTY sandbox."
),
"next_mcp_calls": [
"inspect_connectors(connector=id, include_source_projects=true)",
"configure_connector (stage mapping.names, present alternatives, wait, confirm_mapping=true)",
"sync_connector",
],
}
def pipeline_package_guide() -> dict:
"""How to author a retained package: the same text for coding agents and the assistant."""
return {
"max_bytes": pipeline_packages.MAX_BYTES,
"max_files": pipeline_packages.MAX_FILES,
"manifest": {
"version": 1,
"runtime": "sha256:<approved immutable image ID>",
"parameters": {},
"steps": [
{
"id": "transform",
"input": "source",
"name": "Transform",
"entrypoint": "transform.py",
"input_schema": {"text": "string", "source_row": "integer"},
"output_schema": {"text": "string", "source_row": "integer"},
"checks": {"preserve_rows": True},
}
],
},
"column_types": sorted(pipeline_packages.TYPES),
"analytics": "Published cells carry fingerprint-bound server analytics from a full-row scan. inspect_dataset and REST expose the same counts, baselines, warnings and not_checked risks. Read the exact selected consumer/partition cell before training handoff. Historical or stale reports are unmeasured: use explore_dataset to refresh without changing rows. Class imbalance and rare-class thresholds are advisory; soft targets are not converted to labels and a majority baseline is not model performance.",
"consumer": {
"values": ["decision_train", "decision_eval", "chat_train", "model_eval"],
"scope": "Declare on the final step. Validates the complete output, including nested decision targets, in both preview and publish. Technical compatibility does not establish target meaning.",
},
"batch_rows": "Optional per-step integer 1–100000. The script runs once per consecutive row batch, each in a fresh process, one after another within the step's single sandbox run; outputs concatenate in source order and all steps publish atomically. Declare only for independent row transformations, never global sorting, grouping, splitting or deduplication. Memory admission estimates one batch from the frame's average row width, plus the whole input and output held between batches; at most 10,000 batches; each batch gets the package time limit; progress reports completed batches. Global row checks run after concatenation.",
"corrections": "Edit the same recipe family and run the corrected package from its original source cell. Successful publication replaces the displayed process; no repair-history view or disconnected prepared-v2 sources. Pinned training and evaluation inputs remain reproducible internally. Recipe ownership is project-scoped, not tied to the dataset or capability; reuse only when source and task semantics are compatible.",
"source_boundary": "Upload original source bytes once with pinned upstream revision metadata. Register retained transformation code before full-data processing. Locally prepared derivatives must use lineage-bound external import, never masquerade as an independent source upload.",
"script_arguments": ["input.jsonl", "output.jsonl", "parameters.json"],
"io": "Optional per-step jsonl (default) or parquet. With parquet the script reads input.parquet and writes output.parquet instead (for pandas, pyarrow or DuckDB scripts); nested values are JSON text in the input; output columns may be any Arrow type except binary, which is stored as UTF-8 text. Output values must be finite.",
"inspectability": "Keep each step's meaningful logic in its entrypoint; reserve helpers for reusable functions. Cell transformation metadata binds exact run/revision/package/entrypoint and distinguishes platform execution from external imports. Inspect every retained file via the package resource (file, offset, limit in characters; next_offset until exhausted). Source reads verify the package and file checksums without executing code.",
"starter_script": "import json, sys\n\nwith open(sys.argv[1], encoding='utf-8') as source, open(sys.argv[2], 'w', encoding='utf-8') as output:\n for line in source:\n row = json.loads(line)\n # Author the requested transformation here; retain source_row.\n output.write(json.dumps(row, ensure_ascii=False) + '\\n')\n",
"checks": "preserve_rows and lineage apply in preview and publish. min_rows/max_rows are full-population assertions: preview records them as deferred, publication enforces them. Do not hardcode the first source's size in a reusable recipe unless that size is a real requirement.",
"lineage": "Retain source_row or declare all contributing _overmind_parent_rows. Preserve duplicate observations and unknown meaning. Use input=source/earlier step or inputs=[distinct earlier IDs] to concatenate disjoint branches in declared order. Overlapping source_row identities fail rather than being deduplicated. Omitted input means the previous step.",
"flow": "Declare stable step IDs. Script conditions cite expression and entrypoint line; scripts implement selection. Every step executes, including empty outputs. Flow exposes nodes, edges, terminal_steps and unconsumed_steps. Converge training branches into the last step's trainable output, retaining unresolved review metadata. A retained script with inputs consumes combined branches. Cycles, overlapping identities and job-level conditional skipping are unsupported.",
"runtime": "Inspect inspect_dataset_workbench.runner for approved images and worker freshness. Dependencies are installed in the pinned operator-approved runtime; network and package installation are unavailable during execution. No credentials or host mounts enter script containers.",
"next": "Retain with save_dataset_pipeline(dataset=..., package=...), validate, preview and inspect; publish explicitly. Reuse revisions on compatible sources, or save a derived_from variant. Save bindings paused and enable explicitly.",
}
def _dataset_upload_resource(uri: str) -> dict:
from overbae.services.datasets import documents, files, spreadsheets
return {
"uri": uri,
"kind": "dataset_upload",
"extensions": list(files.ALLOWED_SUFFIXES),
"max_bytes": files.MAX_UPLOAD_BYTES,
"document_max_bytes": documents.MAX_BYTES,
"pdf_max_pages": documents.MAX_PAGES,
"ocr": {
"language_mode": "default_english",
"language_override_tool": "update_dataset",
"language_field": "ocr_languages",
"supported_languages": sorted(LANGUAGES.values()),
"override_guidance": "Set ocr_languages on a draft before uploading; [] uses English. Actual languages and model identities are retained in extraction provenance.",
"checkpoint_retention_days": 30,
},
"max_files": 100,
"json_array_max_bytes": files.JSON_ARRAY_MAX_BYTES,
"limits": (
f"CSV, TSV, JSON, JSONL, NDJSON, and Parquet uploads are capped at "
f"{files.MAX_UPLOAD_BYTES // 1024**3} GiB. "
f"Monolithic JSON arrays are capped at {files.JSON_ARRAY_MAX_BYTES // 1024**2} MiB "
"because the parser reads them whole. "
f"XLSX workbooks are capped at {spreadsheets.MAX_BYTES // 1024**2} MiB and "
f"{spreadsheets.MAX_EXPANDED_BYTES // 1024**2} MiB expanded."
),
"spreadsheets": "Each XLSX sheet with a header row and data lands as its own dataset. The first nonempty row is the header; formulas keep their source text and are not evaluated, dates become ISO text and durations seconds. Each row's _overmind_evidence names its sheet, row and the cells that held a formula, an error, a date or a duration. Ambiguous headers and values right of the header are refused.",
"column_sets": "A new dataset from several files lands each set of column names (compared ignoring case and order; value types do not split a set) as its own dataset; the first set stays the named dataset. Every landed dataset's source_spec.column_groups lists all of them. A split or an upload into an existing dataset needs files with one set of columns.",
"chunk_bytes": files.CHUNK_BYTES,
"command": "overmind dataset upload FILE --project-id PROJECT --json",
"pipeline_packages": {
"required_for_transformations": True,
"upload": "overmind dataset pipeline-upload DIRECTORY_OR_ZIP --project-id PROJECT --json",
"download": "overmind dataset pipeline-download PACKAGE --project-id PROJECT --output pipeline.zip --json",
**pipeline_package_guide(),
},
"ingestion_verification": "Before upload, inspect local record boundaries, fields, value types and expected row count. For JSON wrappers, explicitly select the intended top-level record array; clarify unresolved ambiguity instead of treating the whole wrapper as one row. Preserve original bytes. After landing, compare source count and representative nested values using inspect_dataset and query_dataset. Resolve mismatches before transformation or consumer handoff. State verification coverage: a sample is not a full-file audit, and successful transfer is not proof of correct ingestion.",
"readiness": "Run overmind connection check --project-id PROJECT --json from the actual coding environment. It verifies MCP, project access, transfer protocol and read/write access with the same resolved connection. A connected MCP client alone does not establish local transfer readiness.",
"recovery": "Re-run the identical CLI command to recover its stable transfer key and resume bytes or the existing publication. Use --request-key for an explicitly new upload or a caller-owned identity. Changed content or recipe under the same explicit key conflicts. get_job(kind=dataset_transfer) inspects the receipt without probing or dispatching; dataset_run reports subsequent extraction. Unknown connection errors do not prove sandbox denial. For confirmed network_permission_denied, request the host's supported scoped permission for the same command; do not change sandbox policy, ask for the file again or open a browser.",
"json_selection": "For a JSON object containing a row array under a nonstandard field, pass --json-rows-field FIELD explicitly. The selected top-level field is recorded in extraction metadata; original file bytes and wrapper metadata remain downloadable. No target meaning is inferred.",
"written_intent": "Use start_dataset with a brief and the user's explicit intent and capability. Its upload.argv carries exact project/dataset identities; replace FILE with the inspected local path. For a new upload, --brief records the original request.",
"existing_dataset": "Use overmind dataset upload FILE --project-id PROJECT --dataset DATASET --json to add files to an existing workshop. Each upload appends a recorded import cell after the current chain, preserving earlier versions and source evidence. Inspect the dataset and poll get_job(kind=dataset_run) for completion. REST source accepts uploads; landing does not start preparation.",
"documents": "PDF, DOCX, Markdown, UTF-8 text and PNG/JPEG/WebP images are extracted by the batch worker (100 MiB per document, 2000 pages per PDF). Upload reservations return the file-type byte limit. Original bytes and element/page evidence are retained. PDF extraction preserves native text and automatically runs local English Tesseract OCR on scanned pages and embedded images. Direct images are capped at 64 megapixels; animated images are rejected. OCR engine/version, page regions, upright image coordinates and recognition confidence are retained. Reading order and visual table structure are not reconstructed. A document with no text lands with rows=0 and extraction.status=no_text; only a source with no text at all fails. Upload inspection returns rows=null until extraction. get_job(kind=dataset_run) exposes file/stage and measured OCR-page progress under progress.landing.",
"pdf_text_recovery": {
"trigger": "Pages where the primary native parser returned no text are checked with PDFium before OCR, including selectable Type 3 font layers.",
"evidence": "Source extraction.native_text_recovery records engine/version, recovered pages, rows, characters, control_characters and reason. Each row's _overmind_evidence keeps its page regions and extraction method, pdfium-native-text or tesseract-ocr.",
"limits": "Encoded text is source evidence, not visual verification. Font-encoding artifacts, reading order and diagram relationships require native-agent inspection; the platform does not normalize their meaning. This recovery does not certify partial omissions on pages where the primary parser returned some text.",
},
"publication_status": "Without --wait, CLI state/state_scope=at_publication describe the saved publication snapshot, not current extraction. Read get_job(kind=dataset_run) for current progress. --wait returns state_scope=observed_after_landing.",
"auth": "Account or project API key from --api-key, .overmind/credentials.toml, OVERMIND_API_KEY, or the saved local transfer connection at $XDG_CONFIG_HOME/overmind/connection.toml (default ~/.config/overmind/connection.toml). The saved connection contains an api-key bound to its base-url and must have permissions 0600. MCP authentication alone does not configure CLI authentication. Missing credentials are a local connection error, not a reason to open the Console.",
"config": (
"project-id from overmind.toml or --project-id; base URL from OVERMIND_API_URL, "
"--api-url, or overmind.toml. With no other key, the saved local transfer connection supplies both key and base URL; a different explicitly selected API is rejected."
),
"flow": (
"POST /api/dataset-transfers/ with project, request_key, ordered files [{filename,size,sha256}] (or one filename/size/sha256), and destination recipe; "
"PUT /api/dataset-transfers/{id}/chunk/?offset=&file_index= streams bounded per-file bytes; "
"POST /api/dataset-transfers/{id}/complete/ verifies the hash and publishes once. "
"Read the receipt or repeat reservation with the identical key to resume. Publication is separate from landing."
),
"multiple_files": (
"Use overmind dataset upload FILE1 FILE2 ... --project-id PROJECT --json. Up to 100 "
"ordered files and 2 GiB combined bytes share one durable transfer receipt; per-file "
"type limits still apply. A new dataset lands one dataset per set of column names "
"(case and order ignored) and per workbook sheet, each with one source cell, listed in "
"source_spec.column_groups. With --dataset, the batch needs one set of columns and one "
"attachment cell combines it with the preceding version. Original "
"bytes and file-row evidence remain retained. Repeat the identical command after "
"interruption; changed file order, content or recipe conflicts under the same request key. "
"No dataset is published until every file is verified. --json-rows-field requires a single file. "
"Managed file IDs cannot be republished through unscoped upload endpoints."
),
"batch_limits": {"max_files": 100, "max_total_bytes": 2 * 1024**3},
"next_mcp_calls": [
"get_job(kind=dataset_run, id=dataset_id)",
],
}
def _dataset_export_resource(uri: str) -> dict:
return {
"uri": uri,
"kind": "dataset_export",
"command": "overmind dataset export DATASET --json",
"original_source_command": "overmind dataset export DATASET --source SHA256 --output FILE --json",
"original_source": "Use a sources[].sha256 from inspect_dataset to retrieve retained original bytes. The CLI verifies SHA-256, refuses redirects and existing output files, and removes a corrupt download. Do not combine --source with --cell or --format csv.",
"formats": ["jsonl", "csv"],
"dataset": "Use the dataset id supplied by MCP; the CLI does not resolve dataset names.",
"auth": "X-Api-Key from --api-key, .overmind/credentials.toml, OVERMIND_API_KEY, or the saved local transfer connection. Missing credentials are a local connection error; do not fall back to the Console.",
"config": "Base URL from OVERMIND_API_URL, --api-url, or overmind.toml; --path selects overmind.toml. With no other key, use api-key and base-url together from $XDG_CONFIG_HOME/overmind/connection.toml (default ~/.config/overmind/connection.toml, permissions 0600). A different explicitly selected API is rejected.",
"output": (
"Without --output, the CLI uses and sanitizes the server Content-Disposition filename. "
"It refuses to overwrite an existing local path."
),
"flow": ("select traces -> land a dataset from traces -> run dataset export locally"),
"trace_export": "Keep trace export on this workflow; there is no export_trace MCP tool.",
}
def _checkpoint_download_resource(uri: str) -> dict:
return {
"uri": uri,
"kind": "checkpoint_download",
"command": "overmind model download-checkpoint DEPLOYMENT --json",
"deployment": (
"Resolve and read the deployment through the existing MCP resource/tool flow, then use "
"the deployment id supplied by MCP. The CLI does not resolve deployment names."
),
"auth": "X-Api-Key from --api-key, OVERMIND_API_KEY or repository credentials; otherwise use the endpoint-bound account connection in $XDG_CONFIG_HOME/overmind/connection.toml (default ~/.config/overmind/connection.toml).",
"config": "Base URL from --api-url, OVERMIND_API_URL, overmind.toml or the saved account connection; --path selects overmind.toml. An endpoint override cannot move saved credentials to another API.",
"mcp_boundary": (
"MCP carries deployment metadata and guidance, not checkpoint bytes. The presigned S3 "
"download URL is used only by the local CLI and must never be exposed to model context."
),
"availability": (
"Only archived checkpoints for deployed models linked to fine-tuning jobs are "
"downloadable. Deployments without a fine-tuning job and archives that are not ready "
"are unavailable."
),
"output": "Parse the JSON result and report the local path and bytes_written.",
"overwrite": "The CLI refuses to overwrite an existing local file.",
"redirects": "Metadata and artifact redirects are refused; the API key is sent only to the metadata endpoint.",
}
def resource_uri(kind: str, value: str) -> str:
if kind == "project":
return "overmind://project/current"
if kind == "signal-events":
return "overmind://signal-events"
if kind in {"job", "jobs"}:
job_kind, job_id = value.split("/", 1)
return f"overmind://jobs/{quote(job_kind, safe='')}/{quote(job_id, safe='')}"
return f"overmind://{kind}/{quote(str(value), safe='')}"
def resource_link(kind: str, value: str, title: str) -> dict[str, str]:
return {"uri": resource_uri(kind, value), "title": title, "mimeType": JSON_MIME}
def safe_json(value, *, max_chars: int = 12_000):
if isinstance(value, dict):
result = {}
for key, item in list(value.items())[:100]:
key_text = str(key)
measurement = (
key_text in _TOKEN_MEASUREMENTS
and type(item) in {int, float}
and math.isfinite(item)
)
if not measurement and _contains_key_part(key_text, _SENSITIVE_COMPACT_PARTS):
continue
result[key_text] = safe_json(item, max_chars=max_chars)
return result
if isinstance(value, (list, tuple)):
return [safe_json(item, max_chars=max_chars) for item in list(value)[:100]]
if isinstance(value, str):
return value[:max_chars]
if value is None or isinstance(value, (bool, int, float)):
return value
return str(value)[:max_chars]
def safe_finetune_progress(value):
if isinstance(value, dict):
result = {}
for key, item in list(value.items())[:100]:
key_text = str(key)
numeric_metric = key_text in {
"checkpoint_step",
"restored_step",
"tokens_per_second",
"tokens_processed",
"total_tokens",
"num_tokens",
} and isinstance(item, (int, float))
if not numeric_metric and _contains_key_part(
key_text, _FINETUNE_PROGRESS_REDACTED_PARTS
):
continue
result[key_text] = safe_finetune_progress(item)
if isinstance(item, (list, tuple)) and len(item) > 100:
result[key_text + "_window"] = {
"total": len(item),
"returned": 100,
"order": "latest",
"truncated": True,
}
return result
if isinstance(value, (list, tuple)):
return [safe_finetune_progress(item) for item in list(value)[-100:]]
return safe_json(value)
def finetune_progress_payload(value):
progress = safe_finetune_progress(value)
if not isinstance(progress, dict):
return progress
preparation = progress.get("preparation")
if isinstance(preparation, dict) and preparation.get("state") in {
"queued",
"starting",
"running",
}:
return progress
diagnostics = progress.get("diagnostics")
stage = diagnostics.get("stage") if isinstance(diagnostics, dict) else None
if (stage or progress.get("stage")) in STARTUP_LABELS:
progress["stage_label"] = STARTUP_LABELS[stage or progress["stage"]]
if (stage or progress.get("stage")) == "initial_validation":
progress["stage_label"] = "Pre-training baseline evaluation"
progress["stage_description"] = (
"Measuring the starting model on the development set before training begins."
)
return progress
def _not_found(kind: str, value: str) -> MCPError:
return MCPError("resource_not_found", f"No {kind} resource {value!r} exists in this project.")
def _connector_capabilities(connector_type: str) -> dict:
from overbae.services.connectors import capabilities_for
capabilities = capabilities_for(connector_type)
return {
"exact_count": bool(getattr(capabilities, "exact_count", False)),
"capability_sources": [
source
for source in (getattr(capabilities, "capability_sources", None) or [])
if source in _CONNECTOR_MAPPING_SOURCES
],
"needs_source_project": bool(getattr(capabilities, "needs_source_project", True)),
"retention_note": str(getattr(capabilities, "retention_note", "") or "")[:1_000],
}
def connector_mapping_assignments(project, raw) -> tuple[dict, list[dict]]:
payload = raw if isinstance(raw, dict) else {}
source = payload.get("source")
mapping = {
"source": source if source in _CONNECTOR_MAPPING_SOURCES else None,
"key": str(payload.get("key"))[:255] if payload.get("key") is not None else None,
"names": [str(name)[:255] for name in (payload.get("names") or [])[:100]],
"assignments": {},
"fallback_capability_id": None,
}
assignments = payload.get("assignments") if isinstance(payload.get("assignments"), dict) else {}
ids = [
normalized for value in assignments.values() if (normalized := uuid_reference(str(value)))
]
capabilities = {
str(capability.id): capability
for capability in Capability.objects.filter(project=project, id__in=ids)
}
details = []
for key, value in list(assignments.items())[:100]:
source_value = str(key)[:255]
capability_id = str(value)[:255]
mapping["assignments"][source_value] = capability_id
capability = capabilities.get(capability_id)
details.append(
{
"source_value": source_value,
"capability_id": capability_id,
"capability_name": capability.name[:255] if capability else "Unknown capability",
}
)
fallback = payload.get("fallback_capability_id")
if fallback:
mapping["fallback_capability_id"] = str(fallback)[:255]
return mapping, details
def _connector_mapping(project, connector) -> tuple[dict, list[dict]]:
raw = connector.capability_mapping if isinstance(connector.capability_mapping, dict) else {}
return connector_mapping_assignments(project, raw)
def connector_resource_payload(project, connector, uri: str) -> dict:
"""Return connector metadata without credentials or provider response text."""
config = connector.active_config()
mapping, assignment_details = _connector_mapping(project, connector)
runs = list(connector.runs.order_by("-started_at")[:51])
progress = sync_progress(connector)
return {
"uri": uri,
"kind": "connector",
"id": str(connector.id),
"name": connector.name[:255],
"connector_type": connector.connector_type,
"status": "active" if connector.is_active else "inactive",
"verified": connector.verified_at is not None,
"verified_at": connector.verified_at,
"provider_capabilities": _connector_capabilities(connector.connector_type),
"active_config": (
{
"id": str(config.id),
"version": config.version,
"source_project_id": config.source_project_id,
"target_project_id": str(config.target_project_id)
if config.target_project_id
else None,
"lookback_days": config.lookback_days,
"backfill_from": config.backfill_from,
"backfill_to": config.backfill_to,
"effective_from": config.effective_from,
}
if config is not None
else None
),
"capability_mapping": mapping,
"capability_assignments": assignment_details,
"sync": {
"status": connector.sync_status,
"auto_sync_enabled": connector.auto_sync_enabled,
"poll_interval_seconds": connector.poll_interval_seconds,
"last_synced_at": connector.last_synced_at,
"next_poll_at": connector.next_poll_at,
"backfill_imported": connector.backfill_imported,
"backfill_total": connector.backfill_total,
"progress": progress.model_dump(mode="json") if progress else None,
"total_spans_imported": connector.total_spans_imported,
"total_traces_imported": connector.total_traces_imported,
"has_error": bool(connector.sync_error),
},
"sync_runs": [
{
"id": str(run.id),
"mode": run.mode,
"status": run.status,
"config_version": run.config_version,
"traces_seen": run.traces_seen,
"spans_created": run.spans_created,
"spans_skipped": run.spans_skipped,
"started_at": run.started_at,
"finished_at": run.finished_at,
"has_error": bool(run.error),
}
for run in runs[:50]
],
"sync_runs_truncated": len(runs) > 50,
"source_projects": [],
"source_projects_truncated": False,
"preview_count": None,
"provider_status": "not_requested",
}
def _span_payload(span: Span) -> dict:
return {
"span_id": span.span_id,
"trace_id": span.trace_id,
"parent_span_id": span.parent_span_id,
"name": span.name,
"span_type": span.span_type,
"operation": span.operation,
"status_code": span.status_code,
"status_message": (span.status_message or "")[:500],
"service_name": span.service_name,
"start_time_ns": span.start_time_ns,
"end_time_ns": span.end_time_ns,
"duration_ms": round(span.duration_ns / 1_000_000, 1),
"attributes": safe_json(span.attributes or {}, max_chars=8_000),
"events": safe_json((span.events or [])[:_MAX_EVENTS]),
"resource_attrs": safe_json(span.resource_attrs or {}, max_chars=8_000),
}
def _project_resource(project, uri: str) -> dict:
return {
"uri": uri,
"kind": "project",
"id": str(project.id),
"name": project.name,
"slug": project.slug,
"console_url": f"{settings.FRONTEND_URL.rstrip('/')}/?projectId={project.id}",
"integration_type": project.integration_type,
"is_active": project.is_active,
"created_at": project.created_at,
"updated_at": project.updated_at,
"repository_snapshot": (project.settings or {}).get("repository_snapshot"),
"last_synced_at": (project.settings or {}).get("last_synced_at"),
}
def _capability_resource(project, value: str, uri: str) -> dict:
capability, _ = resolve_capability(project, value)
if capability is None:
raise _not_found("capability", value)
capability = Capability.objects.select_related(
"active_model", "benchmark_model", "activation"
).get(pk=capability.pk)
flow = build_capability_flow(capability)
active_model = capability.active_model
benchmark = capability.benchmark_model
from overbae.services.datasets.rows import capability_dataset_rows
return {
"uri": uri,
"kind": "capability",
"id": str(capability.id),
"name": capability.name,
"slug": capability.slug,
"description": capability.description[:1_000],
"status": capability.status,
"observed": capability.observed,
"model": capability.model,
"dataset_size": capability_dataset_rows(capability),
"active_eval_set": str(capability.active_eval_set_id)
if capability.active_eval_set_id
else None,
"tool_spec": flow["tool_spec"],
"trajectory_map": flow["trajectory_map"],
"eval_preload": (capability.improvement_metadata or {}).get("eval_preload"),
"eval_matrix": [
{
"id": str(row.id),
"name": row.name,
"type": row.type,
"source": row.source[:2_000],
"definition_source": definition_source(row),
"span_type": row.span_type,
"span_name": row.span_name,
"enabled": row.enabled,
"scope": "capability" if row.capability_id else "project",
}
for row in EvalMatrix.objects.filter(project=project)
.select_related("capability")
.filter(Q(capability=capability) | Q(capability__isnull=True))
.order_by("name")[:_MAX_LIST]
],
"signal_subscriptions": [
{
"signal_type": sub.signal_type,
"enabled": sub.enabled,
"config": sub.config,
"scope": "capability" if sub.capability_id else "project",
}
for sub in SignalSubscription.objects.filter(project=project).filter(
Q(capability=capability) | Q(capability__isnull=True)
)[:_MAX_LIST]
],
"active_model": (
{
"id": str(active_model.id),
"model_id": active_model.model_id,
"status": active_model.status,
}
if active_model is not None
else None
),
"activation": activation_progress(getattr(capability, "activation", None)),
"previous_active_model": str(capability.previous_active_model_id)
if capability.previous_active_model_id
else None,
"first_application_request_at": capability.first_application_request_at,
"last_application_request_at": capability.last_application_request_at,
"benchmark_model": {
"id": str(benchmark.id) if benchmark else None,
"model_id": benchmark.model_id if benchmark else capability.model,
"source": "trained" if benchmark else "codebase",
"status": benchmark.status if benchmark else None,
},
"benchmark_candidates": list(
DeployedModel.objects.filter(
project=project, finetuning_job__capability=capability, status="ready"
)
.order_by("-created_at")
.values("id", "model_id", "base_model_id")[:100]
),
"updated_at": capability.updated_at,
}
def _trace_resource(project, value: str, uri: str) -> dict:
queryset = (
Span.objects.filter(project=project, trace_id=value)
.select_related("capability", "conversation")
.order_by("start_time_ns")
)
span_count = queryset.count()
spans = list(queryset[:MAX_INSTRUMENTATION_SPANS])
if not spans:
raise _not_found("trace", value)
root = next((span for span in spans if span.parent_span_id is None), spans[0])
return {
"uri": uri,
"kind": "trace",
"trace_id": value,
"span_count": span_count,
"truncated": span_count > MAX_INSTRUMENTATION_SPANS,
"root": {
"span_id": root.span_id,
"name": root.name,
"capability": root.capability.slug if root.capability_id else None,
"status_code": root.status_code,
"duration_ms": round(root.duration_ns / 1_000_000, 1),
},
"spans": [_span_payload(span) for span in spans],
}
def _session_resource(project, value: str, uri: str) -> dict:
session, _ = resolve_session(project, value)
if session is None:
raise _not_found("session", value)
spans = list(
Span.objects.filter(project=project, conversation_id=session.id)
.order_by("-start_time_ns")
.values_list("trace_id", flat=True)[: _MAX_LIST * 2]
)
trace_ids = list(dict.fromkeys(spans))[:_MAX_LIST]
return {
"uri": uri,
"kind": "session",
"id": str(session.id),
"external_id": session.external_id,
"name": session.name,
"capability": session.capability.slug if session.capability_id else None,
"trace_count": len(trace_ids),
"truncated": len(spans) > len(trace_ids),
"traces": [
resource_link("traces", trace_id, f"Trace {trace_id}") for trace_id in trace_ids
],
"created_at": session.created_at,
}
def _clip_text(value, cap: int) -> str:
return str(value or "")[:cap]
def _dataset_link(dataset) -> dict[str, str]:
try:
from overbae.services.mcp.contracts.datasets import dataset_resource_link
except ImportError:
return resource_link("datasets", str(dataset.id), (dataset.name or "Dataset")[:160])
link = dataset_resource_link(dataset)
if hasattr(link, "model_dump"):
return link.model_dump(mode="json", by_alias=True)
return dict(link)
def _dataset_human_action(dataset) -> dict:
if dataset.source_kind == Dataset.SourceKind.FILE and dataset.state == Dataset.State.LANDING:
return {
"command": "overmind dataset upload FILE --json",
"arguments": {"file": "<path>", "project_id": str(dataset.project_id)},
}
return {
"command": "overmind dataset export DATASET --json",
"arguments": {"dataset": str(dataset.id)},
}
def _dataset_detail_payload(dataset, *, uri: str) -> dict:
detail = serialize_dataset_detail(dataset)
payload = detail.model_dump(mode="json", by_alias=True)
payload["uri"] = uri
payload["workbench"] = workbench.describe(dataset)
payload.setdefault("kind", "dataset")
if not payload.get("human_action"):
payload["human_action"] = _dataset_human_action(dataset)
return payload
def dataset_run_job_payload(dataset, uri: str) -> dict:
chain = dataset.chain
versions = dataset.versions(chain=chain)
ran = [cell for cell in chain if cell.state == Cell.State.OK]
active = next((cell for cell in ran if cell.id == dataset.active_id), ran[-1] if ran else None)
actions = [action.model_dump(mode="json") for action in next_actions(dataset, chain, active)]
cells = {"n": len(chain), "states": dict(Counter(cell.state for cell in chain))}
dataset_link = _dataset_link(dataset)
job_link = resource_link(
"jobs", f"dataset_run/{dataset.id}", (dataset.name or "Dataset run")[:160]
)
error = _clip_text(dataset.error, _ERROR_CAP) or None
return {
"uri": uri,
"kind": "dataset_run",
"id": str(dataset.id),
"name": dataset.name,
"status": dataset.state,
"state": dataset.state,
"error": error,
"active": (
{
"id": str(active.id),
"version": versions.get(active.id, ""),
"title": active.title,
"rows": active.rows,
}
if active
else None
),
"cells": cells,
"next_action": actions[0] if actions else None,
"next_actions": actions,
"dataset": dataset_link,
"progress": {
"cells": cells["states"],
"rows": active.rows if active else 0,
"rows_scope": "active_version",
"landing": dataset.source_spec.get("landing_progress"),
"import": import_status(dataset),
"admission": landing_admission_status(dataset),
},
"resource_links": [job_link, dataset_link],
"created_at": dataset.created_at,
"updated_at": dataset.updated_at,
}
def _load_dataset_job(project, normalized_id):
if not normalized_id:
return None
return (
Dataset.objects.filter(project=project, id=normalized_id)
.select_related("capability", "active")
.prefetch_related("cells")
.first()
)
def _dataset_resource(project, value: str, uri: str) -> dict:
dataset, _ = resolve_dataset(project, value)
if dataset is None:
raise _not_found("dataset", value)
return _dataset_detail_payload(dataset, uri=uri)
def _eval_set_resource(project, value: str, uri: str) -> dict:
eval_set = (
EvalSet.objects.filter(project=project, id=uuid_reference(value))
.select_related("capability")
.first()
)
if eval_set is None:
raise _not_found("eval set", value)
params = parse_qs(urlparse(uri).query)
try:
offset = int(params.get("offset", [0])[0])
limit = int(params.get("limit", [20])[0])
if offset < 0 or not 1 <= limit <= 100:
raise ValueError
except ValueError:
raise MCPError("invalid_input", "Use a nonnegative offset and limit 1–100") from None
query = eval_set.rows.select_related("capability").order_by("name", "id")
total = query.count()
rows = list(query[offset : offset + limit])
return {
"uri": uri,
"kind": "eval_set",
"id": str(eval_set.id),
"name": eval_set.name,
"capability": str(eval_set.capability_id) if eval_set.capability_id else None,
"is_active": bool(
eval_set.capability_id and eval_set.capability.active_eval_set_id == eval_set.id
),
"rows": [
{
"id": str(row.id),
"name": row.name,
"type": row.type,
"source": row.source,
"definition_source": definition_source(row),
"span_type": row.span_type,
"span_name": row.span_name,
"enabled": row.enabled,
}
for row in rows
],
"criteria": eval_set.criteria,
"rows_truncated": offset + len(rows) < total,
"total": total,
"offset": offset,
"next_offset": offset + len(rows) if offset + len(rows) < total else None,
}
def _eval_run_resource(project, value: str, uri: str) -> dict:
run, _ = resolve_eval_run(project, value)
if run is None:
raise _not_found("eval run", value)
run = EvalRun.objects.select_related("dataset", "cell", "model_ref").get(pk=run.pk)
scores = comparison.scores_by_row(run)
rows = list(run.rows.order_by("row_index")[:5])
baseline = comparison.baseline_of(run.group)
return {
"uri": uri,
"kind": "eval_run",
"id": str(run.id),
"name": run.name,
"label": run.label,
"description": run.description[:1_000],
"status": run.status,
"mode": run.mode,
"model": run.resolved_model,
"group": str(run.group),
"is_baseline": run.is_baseline,
"dataset": str(run.dataset_id) if run.dataset_id else None,
"cell": str(run.cell_id) if run.cell_id else None,
"max_items": run.max_items,
"sampling": run.sampling,
"error": run.error[:1_000],
"summary": safe_json(run.summary or {}),
"context_checks": safe_json(eval_context.run_context_checks(run)),
"judge_model": run.judge_model,
"judge_context": safe_json(run.judge_context),
"matrix": [
{"name": row.get("name"), "type": row.get("type")} for row in run.matrix[:_MAX_LIST]
],
"progress": None if run.is_terminal else safe_json(run_progress(run)),
"row_count": run.rows.count(),
"rows": [
{
"row_index": row.row_index,
"state": row.state,
"output": safe_json(row.output),
"error": row.error[:_ERROR_CAP],
"score_error": row.score_error[:_ERROR_CAP],
"degraded": row.degraded,
"degraded_reason": row.degraded_reason,
"score": found.score
if (found := scores.get(f"{run.cell_id}:{row.row_index}"))
else None,
"metrics": safe_json(found.metrics) if found else {},
}
for row in rows
],
"baseline": (
resource_link("eval-runs", str(baseline.id), baseline.name)
if baseline is not None and baseline.pk != run.pk
else None
),
"created_at": run.created_at,
"completed_at": run.completed_at,
}
def _signal_events_resource(project, uri: str) -> dict:
events = list(
SignalEvent.objects.filter(project=project).select_related("capability")[:_MAX_LIST]
)
return {
"uri": uri,
"kind": "signal_events",
"unseen": SignalEvent.objects.filter(project=project, seen_at__isnull=True).count(),
"events": [
{
"id": str(event.id),
"signal_type": event.signal_type,
"conversation_id": str(event.conversation_id) if event.conversation_id else None,
"capability": event.capability.slug if event.capability_id else None,
"message": event.message[:1_000],
"details": safe_json(event.details),
"trace": resource_link("traces", event.trace_id, f"Trace {event.trace_id}")
if event.trace_id
else None,
"occurred_at": event.occurred_at,
"seen": event.seen_at is not None,
}
for event in events
],
}
def _finetune_resource(project, value: str, uri: str) -> dict:
normalized_id = uuid_reference(value)
job = (
FinetuningJob.objects.filter(project=project, id=normalized_id)
.select_related(
"capability",
"dataset",
"deployed_model",
"cell",
"validation_cell",
"eval_cell",
"eval_set",
"native_evaluation",
"decision_job",
)
.first()
if normalized_id
else None
)
if job is None:
raise _not_found("finetune", value)
events = list(job.events.order_by("-created_at")[:_MAX_LIST])
deployment = getattr(job, "deployed_model", None)
progress = live_progress_for_job(job)
result = job.result if isinstance(job.result, dict) else {}
metrics = progress.get("metrics") if isinstance(progress.get("metrics"), dict) else {}
loss = metrics.get("loss") or result.get("epoch_losses") or []
return {
"uri": uri,
"kind": "finetune",
"id": str(job.id),
"decision_job": str(job.decision_job.id)
if getattr(job, "decision_job", None) is not None
else None,
"group_id": str(job.group_id) if job.group_id else None,
"name": job.name,
"status": job.status,
"provider": job.provider,
"base_model": job.base_model,
"record": safe_json(run_record(job)),
"monitoring": safe_json(training_monitoring.summary(job)),
"training_objective": (job.hyperparameters or {}).get(
"objective", "assistant_cross_entropy"
),
"inference_contract": result.get("inference_contract")
or (
"decision"
if (job.hyperparameters or {}).get("objective") in DECISION_OBJECTIVES
else "chat"
),
"cost_usd": float(job.cost_usd) if job.cost_usd is not None else None,
"cost_synced_at": job.cost_synced_at,
"evaluation_plan": {
"eval_judge_model": job.eval_judge_model,
"eval_incumbent_before": job.eval_incumbent_before,
"eval_incumbent_after": job.eval_incumbent_after,
"eval_model_before": job.eval_model_before,
"eval_model_after": job.eval_model_after,
},
"capability": job.capability.slug if job.capability_id else None,
"dataset": str(job.dataset_id) if job.dataset_id else None,
"cell": str(job.cell_id) if job.cell_id else None,
"eval_cell": str(job.eval_cell_id) if job.eval_cell_id else None,
"progress": finetune_progress_payload(progress),
"loss": safe_json(loss[-100:] if isinstance(loss, list) else []),
"error": job.error_message[:1_000],
"deployed_model": str(deployment.id) if deployment is not None else None,
"events": [
{
"id": str(event.id),
"type": event.event_type,
"message": event.message[:500],
"created_at": event.created_at,
}
for event in events
],
"created_at": job.created_at,
"completed_at": job.completed_at,
}
def _dedicated_payload(deployment) -> dict | None:
row = dedicated.live_for(deployment)
if row is None:
return None
return {
"id": str(row.id),
"model_id": row.public_model_id,
"status": row.status,
"gpu_type": row.gpu_type,
"min_replicas": row.min_replicas,
"max_replicas": row.max_replicas,
"scale_to_zero": row.scale_to_zero,
"idle_shutdown_seconds": row.idle_shutdown_seconds,
"running_replicas": row.running_replicas,
"credits_per_gpu_hour": str(row.credits_per_gpu_hour),
"accrued_credits": str(row.accrued_credits),
"error": row.error_message[:1_000],
"startup": dedicated_progress.summary(row),
}
def _deployment_resource(project, value: str, uri: str) -> dict:
query = DeployedModel.objects.filter(project=project).select_related(
"finetuning_job__capability"
)
deployment = dedicated.deployed_model(query, value)
if deployment is None:
raise _not_found("deployment", value)
params = parse_qs(urlparse(uri).query, keep_blank_values=True)
try:
options = monitoring_options(
params.get("period", ["all"])[0], params.get("source", ["all"])[0]
)
except InputValidationError as exc:
raise MCPError("invalid_input", exc.detail) from exc
granularity = {"1h": "minute", "24h": "hour", "7d": "hour", "30d": "day", "all": "day"}[
options["period"]
]
return {
"uri": uri,
"kind": "deployment",
"id": str(deployment.id),
"model_id": deployment.model_id,
"inference_contract": deployment.inference_contract,
"serving_runtime": deployment.serving_runtime,
"status": deployment.status,
"progress": deployment_progress(deployment),
"worker": worker_status(deployment),
"base_model_id": deployment.base_model_id,
"quantization": deployment.quantization,
"gpu_type": deployment.gpu_type,
"is_lora": deployment.is_lora,
"sla_tier": deployment.sla_tier,
"inference_url": deployment.inference_url or None,
"monitoring": {**options, "granularity": granularity},
"metrics": model_metrics(deployment, **options),
"activity": model_activity(deployment, granularity=granularity, **options),
"finetuning_job": str(deployment.finetuning_job_id)
if deployment.finetuning_job_id
else None,
"error": deployment.error_message[:1_000],
"created_at": deployment.created_at,
"deployed_at": deployment.deployed_at,
"dedicated": _dedicated_payload(deployment),
}
def _job_resource(project, kind: str, value: str, uri: str) -> dict:
if kind == "inference_request":
request = InferenceRequest.objects.filter(project=project, pk=uuid_reference(value)).first()
if request is None:
raise _not_found("inference request", value)
result = inference_requests.describe(request)
params = parse_qs(urlparse(uri).query)
try:
offset, limit = int(params.get("offset", [0])[0]), int(params.get("limit", [32000])[0])
if offset < 0 or not 1 <= limit <= 32000:
raise ValueError
except ValueError:
raise MCPError(
"invalid_input", "Use a nonnegative content offset and limit 1–32000"
) from None
content = str(request.result.get("content") or "")
if request.result:
result["result"]["content"] = content[offset : offset + limit]
result["content_page"] = {
"offset": offset,
"total": len(content),
"next_offset": offset + limit if offset + limit < len(content) else None,
}
return {"uri": uri, "kind": kind, **result}
if kind == "model_activation":
activation = ModelActivation.objects.filter(
pk=uuid_reference(value), capability__project=project
).first()
if activation is None:
raise _not_found("model activation", value)
return {
"uri": uri,
"kind": kind,
"status": activation.stage,
**activation_progress(activation),
}
if kind in model_workflows.MODELS:
record = model_workflows.find(project, kind, uuid_reference(value))
if record is None:
raise _not_found(kind, value)
return {"uri": uri, "kind": kind, **safe_json(model_workflows.describe(kind, record))}
if kind == "training_preparation":
prep = TrainingPreparation.objects.filter(
pk=uuid_reference(value), cell__dataset__project=project
).first()
if prep is None:
raise _not_found("training preparation", value)
return {
"kind": kind,
"id": str(prep.id),
"status": prep.state,
"training_job": str(training_job.pk)
if (training_job := prep.current_training_job())
else None,
"report": prep.report,
"config": prep.config,
"error": prep.error,
"resource": resource_link("jobs", f"{kind}/{prep.id}", "Training preparation"),
}
if kind == "eval_run":
return _eval_run_resource(project, value, uri)
if kind in {"finetune", "finetune_job"}:
return _finetune_resource(project, value, uri)
if kind == "decision_job":
decision = DecisionJob.objects.filter(
finetuning_job__project=project, pk=uuid_reference(value)
).first()
if decision is None:
raise _not_found("decision job", value)
result = _finetune_resource(
project,
str(decision.finetuning_job_id),
f"overmind://finetunes/{decision.finetuning_job_id}",
)
return {
**result,
"uri": uri,
"kind": "decision_job",
"id": str(decision.id),
"finetuning_job": str(decision.finetuning_job_id),
}
if kind in {"deployment", "model_deployment"}:
return _deployment_resource(project, value, uri)
normalized_id = uuid_reference(value)
if kind == "dataset_transfer":
from overbae.services.datasets import transfers
transfer = DatasetTransfer.objects.filter(project=project, pk=normalized_id).first()
if transfer is None:
raise _not_found("dataset transfer", value)
return {"uri": uri, "kind": kind, **transfers.describe(transfer)}
if kind == "dataset_pipeline":
run = (
DatasetPipelineRun.objects.filter(dataset__project=project, pk=normalized_id)
.select_related("dataset", "pipeline__package")
.first()
if normalized_id
else None
)
if run is None:
raise _not_found("dataset pipeline", value)
return {"uri": uri, "kind": kind, "status": run.state, **workbench.run_record(run)}
if kind == "dataset_run":
job = _load_dataset_job(project, normalized_id)
if job is None:
raise _not_found("dataset run", value)
return dataset_run_job_payload(job, uri)
raise _not_found("job", f"{kind}/{value}")
def _connector_resource(project, value: str, uri: str) -> dict:
connector, _ = resolve_connector(project, value)
if connector is None:
raise _not_found("connector", value)
return connector_resource_payload(project, connector, uri)
def interface_resource():
# Catalog registrars load resources; inspect the catalog only after initialization.
from overbae.services.mcp.catalog import CATALOG
context = get_context()
manifest = [tool.model_dump(mode="json") for tool in CATALOG.visible_to(context)]
parsed = urlparse(context.inference_base_url)
origin = (
f"{parsed.scheme}://{parsed.netloc}"
if parsed.scheme in {"http", "https"} and parsed.netloc
else None
)
return {
"contract_version": CONTRACT_VERSION,
"transfer_protocol_version": 1,
"catalog_sha256": hashlib.sha256(json.dumps(manifest, sort_keys=True).encode()).hexdigest(),
"tool_count": len(manifest),
"credit_reporting": {
"tool": "get_credit_balance",
"units": "100 credits = 1 USD; decimal values are exact JSON strings.",
"scope": "Account and project credentials expose the connection owner's account balance, lifetime grants/spending and account-wide usage by service. No project_id is required; since filters usage, not lifetime totals. get_credit_usage pages one project's charges by service, cause, experiment and run.",
"payer": "A project's owner pays for all its work at the provider's price, with no markup; each charge names its cause and who started it. Storage and compute on Overmind's own servers are included.",
"task_budget": "For work in a project, read get_credit_usage without since before the work and again before further paid work, and compare the growth of spent_credits with the agreed budget: the project's owner pays, so the connection's own account usage can miss it, and a running container's charge grows on its existing row, which since (a filter on when a row was created) does not count. Compare get_credit_balance spent_credits the same way only for a budget of the connection owner's account. A running container's charge grows each minute, and a model call the provider prices later posts then. No reservation or hard spending cap is created. With billing_enabled=false, metering is uncapped.",
"gate": "Paid work a person or agent starts needs the owner's balance above zero; the balance can go negative while work runs. With billing enabled, at zero background_paused is true: scoring, signals and scheduled work wait until a top-up. Without billing nothing pauses.",
},
"training_measurements": {
"planning": "save_training_plan retains immutable source/task, prepared versions, partition and attributed audit/baseline/sanity evidence. Full training requires no pilot; bind create_training_experiment with stage full directly. Stage pilot and measured pilot selection are optional. Primary metric is a JSON Pointer into check metrics; optional pilot selection requires finite task metrics and a verified checkpoint from matching data, and limits full recipes to declared budget-field changes. Hardware profiles are not quality pilots. New upstream identities require new matching evidence; old snapshots stay reproducible.",
"monitoring": "A frozen monitoring policy controls bounded development checks, generation scoring, checkpoint selection and optional early stopping. inspect_training_progress pages durable checks and exact evidence without invoking workers. cancel_finetune preserves completed evidence and records remote acknowledgement separately. Development metrics are not final benchmarks. Native target distributions and mean-only denominators remain distinct.",
"billing": "Training, preparation and publication containers are charged by their reported container seconds at Modal's rates for GPU, CPU and memory, also when the job fails or is cancelled. Read them with get_credit_usage(cause_kind='finetune_job').",
"forecast": "Matching duration evidence comes from worker usage receipts, excluding collector delay; estimates and recorded charges are not all-in invoices or spend caps.",
"recovery": "The training collector renews a short lease while alive. Reconciliation closes missed terminal operational events from local receipts without another provider call; worker heartbeat timestamps are preserved.",
},
"dataset_transformations": {
"query_capacity": {
"global_concurrency": settings.DATASET_QUERY_MAX_IN_FLIGHT,
"project_concurrency": settings.DATASET_QUERY_PROJECT_CONCURRENCY,
"timeout_seconds": settings.DATASET_QUERY_TIMEOUT_SECONDS,
"busy_code": "query_capacity_busy",
"unavailable_code": "query_capacity_unavailable",
"retry": "Use the returned cell, identical SQL and retry_after_seconds. Bulk analysis belongs in a retained pipeline. No private SQL engine is created before admission.",
},
"source_admission": "Source imports wait durably before bounded broker publication. Dataset-run progress.admission and dataset operation.landing_admission expose queue, execution and terminal states with timestamps. One import per project runs at a time; waiting does not consume an execution attempt or expire unless explicitly configured. A started import whose worker or document parser stopped is retried automatically under a new owner, up to 3 attempts in total, and keeps its source. Other failures, and imports at the attempt limit, stop; resume_dataset_import retries a stopped import with attempts left.",
"progress": "Preview and publication expose aggregate seconds plus per-step runtime stage_seconds for container setup, input transfer, script execution, artifact transfer, output reading and cleanup. A stopped script never reports completed runtime; cleanup_confirmed is separate from publication success.",
"parameters": "Package manifest parameters declare types, for example {seed: integer}; run parameters supply values, for example {seed: 42}.",
"queries": "Read-only queries have a deployment-configured deadline (default 10 seconds); query_timeout leaves the cell unchanged. Oversized results are rejected during materialization, not clipped.",
"inspection": "Cell transformation metadata distinguishes recorded script/operation execution, external imports and unrecorded history. It links the exact run, revision, package checksum and entrypoint. Read all retained package files via dataset-pipeline-packages resources using file/offset/limit character pagination; source reads never execute code.",
"prompt": "author-dataset-transformation",
"lookup": "inspect_dataset_workbench(pipeline=exact revision ID) returns the recipe and paginated family history; runs and bindings are scoped to that revision",
"retained_code": "Every new transformation revision requires an uploaded Python package. Author meaningful staged entrypoints, pipeline-upload, save_dataset_pipeline(dataset=DATASET, package=UUID), validate, preview, publish and verify the exact run/cells. Historical package-free revisions remain readable but cannot execute or bind. External imports are attributed results, not retained transformations.",
"branches": "Acyclic forks and fan-in: input selects one parent; inputs concatenates distinct parents in declared order. Nonempty branch schemas must match; overlapping source_row identities or incompatible schemas fail without coercion. Retained scripts implement routing and consume concatenated branch inputs.",
"conditions": "Agent-declared expression and retained entrypoint line; never evaluated as code or inferred by the platform",
"empty_branches": "All steps execute; zero rows are valid unless row checks reject them",
"output": "Last declared step; all published step cells remain selectable",
"unsupported": [
"overlapping row identities across inputs",
"cycles",
"job-level conditional skipping",
"automatic exclusivity or coverage verification",
],
"adaptation": "Reuse the revision for parameters only; new family via derived_from=revision ID for different logic/source contracts; revise a family via pipeline=family ID and expected_revision",
},
"connection": {
"mcp_url": origin + "/api/mcp/" if origin else None,
"console_url": settings.FRONTEND_URL,
"identity_basis": "authenticated request endpoint; compare with your intended environment",
},
"guidance": {
"entry": "list_projects; data-only work does not require code scanning or a capability",
"source": "Pass exact cell UUIDs and fingerprints. Workshop pipelines publish new versions, including from frozen historical parents.",
"workshop": "The native agent authors and explains. Discover reusable project revisions with inspect_dataset_workbench. Register script packages via CLI and save_dataset_pipeline, validate, preview, then run on pinned source cells. Each step publishes a cell atomically. Adapt with derived_from; original revisions remain unchanged. Explicitly enabled bindings rebuild full source snapshots using the pinned revision. Scripts run only in approved isolated containers, never API/Celery Python. Imports remain externally attributed.",
"lifecycle": "create saves a draft; prepare verifies inputs; launch is explicit paid authorization",
"resume": "Use get_job and next_actions. Saved request keys and provider receipts survive reconnects; unresolved submissions are not replayed.",
"recipes": "Use get_model_catalog for supported model-specific context, batch and training-method constraints. Hyperparameters remain a model-dependent extension; stable partition, sampling, inference and workload settings are typed.",
"cost": "No implicit spend limit. User constraints bind a reviewed quote; missing cost components stay explicit.",
"pause": "Pausing a comparison prevents future submissions. It neither cancels in-flight provider work nor configures an external notification monitor.",
},
"training_runtime": "Pinned per experiment and job; local catalog identity does not certify deployed GPU code",
"installed_guidance": "Connected tool schemas are authoritative when installed skills differ",
"discovery": "Compare this endpoint and catalog hash with the connection you intend to use. Refresh tools/list when the cached inventory differs; do not silently switch environments.",
}
def _pipeline_content_page(params):
try:
offset, limit = int(params.get("offset", [0])[0]), int(params.get("limit", [8000])[0])
if offset < 0 or not 1 <= limit <= 16000:
raise ValueError
return offset, limit
except ValueError:
raise MCPError("invalid_input", "Use a nonnegative offset and limit 1–16000.") from None
def _read_resource_sync(project, raw_uri: str) -> dict:
parsed = urlparse(raw_uri)
if parsed.scheme != "overmind":
raise _not_found("resource", raw_uri)
segments = [unquote(part) for part in parsed.path.split("/") if part]
host = parsed.netloc
if host == "experiments" and len(segments) == 2 and segments[1] == "automation":
experiment = ResearchExperiment.objects.filter(
pk=uuid_reference(segments[0]), project=project, deleted_at__isnull=True
).first()
if experiment is None:
raise _not_found("experiment", segments[0])
params = parse_qs(parsed.query)
try:
return {
"uri": raw_uri,
**research_triggers.inspect(
experiment,
cursor=uuid_reference(params["cursor"][0]) if params.get("cursor") else None,
limit=int(params.get("limit", [100])[0]),
evidence_fire=uuid_reference(params["fire"][0]) if params.get("fire") else None,
evidence_cursor=int(params.get("evidence_cursor", [0])[0]),
),
}
except (ValueError, InputValidationError) as error:
raise MCPError("invalid_input", str(error)) from error
if host == "context" and len(segments) == 2 and segments[0] == "snapshots":
snapshot = ContextSnapshot.objects.filter(
project=project, pk=uuid_reference(segments[1])
).first()
if snapshot is None:
raise _not_found("context snapshot", segments[1])
return {
**context_graph.snapshot_receipt(snapshot),
"observation_window": snapshot.manifest["observation_window"],
"node_count": len(snapshot.manifest["nodes"]),
"edge_count": len(snapshot.manifest["edges"]),
"manifest": snapshot.manifest,
}
if host == "deployments" and len(segments) == 2 and segments[0] == "dedicated":
return _deployment_resource(project, "/".join(segments), raw_uri)
if host == "checkpoints" and len(segments) == 1:
checkpoint = TrainingCheckpoint.objects.filter(
job__project=project, pk=uuid_reference(segments[0])
).first()
if checkpoint is None:
raise _not_found("checkpoint", segments[0])
return {
"uri": raw_uri,
"id": str(checkpoint.pk),
"training_job_id": str(checkpoint.job_id),
"identity": checkpoint.identity,
"state": checkpoint.state,
"step": checkpoint.step,
"attempt": checkpoint.attempt,
"selected": checkpoint.selected,
"created_at": checkpoint.created_at,
"training_resource_uri": f"overmind://finetunes/{checkpoint.job_id}?project_id={project.pk}",
}
if host == "eval-rows" and len(segments) == 1:
row = EvalMatrix.objects.filter(project=project, pk=uuid_reference(segments[0])).first()
if row is None:
raise _not_found("eval row", segments[0])
params = parse_qs(parsed.query)
try:
offset = int(params.get("offset", [0])[0])
limit = int(params.get("limit", [20])[0])
source_offset = int(params.get("source_offset", [0])[0])
if offset < 0 or source_offset < 0 or not 1 <= limit <= 100:
raise ValueError
except ValueError:
raise MCPError("invalid_input", "Use nonnegative offsets and limit 1–100.") from None
definition = {**row.snapshot(), "enabled": row.enabled, "updated_at": row.updated_at}
if params.get("revision"):
revision = row.revisions.filter(pk=uuid_reference(params["revision"][0])).first()
if revision is None:
raise _not_found("eval revision", params["revision"][0])
definition = dict(revision.definition)
source = definition["source"]
definition["source"] = source[source_offset : source_offset + 12000]
revisions = row.revisions.order_by("-created_at", "-pk")
total = revisions.count()
return {
"uri": raw_uri,
"id": str(row.pk),
"project_id": str(project.pk),
**definition,
"source_page": {
"offset": source_offset,
"total": len(source),
"next_offset": source_offset + 12000
if source_offset + 12000 < len(source)
else None,
},
"revisions": [
{
"id": str(revision.pk),
"created_at": revision.created_at,
"resource_uri": f"overmind://eval-rows/{row.pk}?project_id={project.pk}&revision={revision.pk}",
}
for revision in revisions[offset : offset + limit]
],
"revision_total": total,
"next_offset": offset + limit if offset + limit < total else None,
}
if host == "activity" and not segments:
params = parse_qs(parsed.query, keep_blank_values=True)
try:
values = InspectActivityInput.model_validate(
{key: value[0] for key, value in params.items() if key != "project_id"}
)
if any(len(value) != 1 for value in params.values()):
raise ValueError
except (ValidationError, ValueError):
raise MCPError(
"invalid_input", "Use offset >= 0, limit 1–100 and path=agent|data."
) from None
context = get_context()
query = activity.visible(
context.user,
[project.pk],
project.pk,
include_account=context.token.scope.get("scope") != "project",
)
return InspectActivityOutput.model_validate(
{
**activity.feed(query, offset=values.offset, limit=values.limit),
"milestones": activity.milestones(query, values.path),
}
).model_dump(mode="json")
if host == "interface" and segments == ["current"]:
return interface_resource()
if host == "project" and segments == ["current"]:
return _project_resource(project, raw_uri)
if host == "dataset-upload" and not segments:
return _dataset_upload_resource(raw_uri)
if host == "dataset-export" and not segments:
return _dataset_export_resource(raw_uri)
if host == "checkpoint-download" and not segments:
return _checkpoint_download_resource(raw_uri)
if host == "connector-setup" and not segments:
return _connector_setup_resource(raw_uri)
if host == "dataset-pipelines" and len(segments) == 1:
recipe = (
DatasetPipeline.objects.select_related("package")
.filter(project=project, pk=uuid_reference(segments[0]))
.first()
)
if recipe is None:
raise _not_found("pipeline revision", segments[0])
result = {"uri": raw_uri, **workbench.pipeline_record(recipe)}
if recipe.package_id:
result["package_detail"] = pipeline_packages.record(recipe.package)
result["package_resource"] = resource_link(
"dataset-pipeline-packages", recipe.package_id, "Retained source package"
)
return result
if host == "dataset-pipeline-bindings" and len(segments) == 1:
binding = DatasetPipelineBinding.objects.filter(
project=project, pk=uuid_reference(segments[0])
).first()
if binding is None:
raise _not_found("pipeline binding", segments[0])
return {"uri": raw_uri, **pipeline_bindings.binding_record(binding)}
if host == "dataset-pipeline-packages" and len(segments) == 1:
package = DatasetPipelinePackage.objects.filter(
project=project, pk=uuid_reference(segments[0])
).first()
if package is None:
raise _not_found("pipeline package", segments[0])
result = {"uri": raw_uri, **pipeline_packages.record(package)}
params = parse_qs(parsed.query)
filename = params.get("file", [None])[0]
if filename:
offset, limit = _pipeline_content_page(params)
if filename not in {item["path"] for item in package.inventory}:
raise _not_found("package file", filename)
try:
result["file"] = pipeline_packages.source_file(
package, filename, offset=offset, limit=limit
)
except DatasetError as exc:
raise MCPError(exc.code, exc.detail) from exc
return result
if host == "pipeline-diagnostics" and len(segments) == 1:
run = DatasetPipelineRun.objects.filter(
dataset__project=project, pk=uuid_reference(segments[0])
).first()
if run is None:
raise _not_found("pipeline run", segments[0])
params = parse_qs(parsed.query)
offset, limit = _pipeline_content_page(params)
try:
step = int(params.get("step", [0])[0])
channel = params.get("channel", ["stderr"])[0]
if not 0 <= step < 20 or channel not in {"stderr", "stdout"}:
raise ValueError
except ValueError:
raise MCPError("invalid_input", "Use step 0–19 and stdout or stderr.") from None
path = paths.media_root() / "pipeline-runs" / str(run.pk) / f"{step}-{channel}.log"
content = path.read_text(errors="replace") if path.exists() else ""
return {
"run": str(run.pk),
"step": step,
"channel": channel,
"source": "untrusted_script_output",
"available": path.exists(),
"retained_limit_bytes": 65536,
"content": content[offset : offset + limit],
"total": len(content),
"next_offset": offset + limit if offset + limit < len(content) else None,
}
if host == "operations" and len(segments) == 1:
params = parse_qs(parsed.query)
try:
return operational_progress.inspect(
project.pk,
uuid_reference(segments[0]),
after=int(params.get("after", [0])[0]),
limit=int(params.get("limit", [50])[0]),
)
except operational_progress.OperationNotFoundError:
raise _not_found("operation", segments[0]) from None
except ValueError:
raise MCPError("invalid_input", "Invalid operational event cursor or limit") from None
if host == "signal-events" and not segments:
return _signal_events_resource(project, raw_uri)
if (
host
in {
"capabilities",
"traces",
"sessions",
"datasets",
"eval-runs",
"eval-sets",
"finetunes",
"deployments",
"connectors",
}
and len(segments) == 1
):
kind_map = {
"capabilities": _capability_resource,
"traces": _trace_resource,
"sessions": _session_resource,
"datasets": _dataset_resource,
"eval-runs": _eval_run_resource,
"eval-sets": _eval_set_resource,
"finetunes": _finetune_resource,
"deployments": _deployment_resource,
"connectors": _connector_resource,
}
return kind_map[host](project, segments[0], raw_uri)
if (
host == "jobs"
and len(segments) == 3
and segments[0] == "data_exploration"
and segments[2] == "strata"
):
record = model_workflows.find(project, "data_exploration", uuid_reference(segments[1]))
if record is None:
raise _not_found("data_exploration", segments[1])
params = parse_qs(urlparse(raw_uri).query)
try:
return model_workflows.exploration.strata(
record,
limit=int(params.get("limit", [100])[0]),
offset=int(params.get("offset", [0])[0]),
)
except ValueError as exc:
raise MCPError("invalid_input", str(exc)) from None
if host == "jobs" and len(segments) == 2:
result = _job_resource(project, segments[0], segments[1], raw_uri)
operation_kind = "finetune_job" if segments[0] == "decision_job" else segments[0]
operation_id = result.get("finetuning_job", segments[1])
operation = operational_progress.latest(project.pk, operation_kind, operation_id)
if operation:
result["operation"] = operation
result["operation_resource"] = resource_link(
"operations", operation["id"], "Operational timeline"
)
return result
raise _not_found("resource", raw_uri)
def resource_list() -> list[types.Resource]:
return [
types.Resource(
name="interface",
title="Connected interface",
uri="overmind://interface/current",
description="Connected contract version, catalog fingerprint and data-first lifecycle rules.",
mimeType=JSON_MIME,
),
types.Resource(
name="current-project",
title="Current project",
uri="overmind://project/current",
description="Project identity and safe metadata. Account connections must add ?project_id=ID from list_projects.",
mimeType=JSON_MIME,
),
types.Resource(
name="dataset-upload",
title="Local dataset upload",
uri="overmind://dataset-upload",
description="CLI guidance for uploading local dataset bytes.",
mimeType=JSON_MIME,
),
types.Resource(
name="dataset-export",
title="Local dataset export",
uri="overmind://dataset-export",
description="CLI guidance for downloading committed dataset bytes locally.",
mimeType=JSON_MIME,
),
types.Resource(
name="checkpoint-download",
title="Local checkpoint download",
uri="overmind://checkpoint-download",
description="CLI guidance for downloading an archived fine-tuned checkpoint locally.",
mimeType=JSON_MIME,
),
types.Resource(
name="connector-setup",
title="Connector credential setup",
uri="overmind://connector-setup",
description="CLI guidance for adding provider credentials without putting them in MCP.",
mimeType=JSON_MIME,
),
types.Resource(
name="signal-events",
title="Signal events",
uri="overmind://signal-events",
description="The project's newest signal events and its unseen count. Account connections must add ?project_id=ID.",
mimeType=JSON_MIME,
),
types.Resource(
name="activity",
title="Activity and onboarding",
uri="overmind://activity",
description="Saved outcomes and setup milestones. Account connections add project_id; optional offset, limit (1–100), path=agent|data. Reads never mark notifications seen.",
mimeType=JSON_MIME,
),
]
def resource_templates() -> list[types.ResourceTemplate]:
templates = [
(
"experiment-automation",
"overmind://experiments/{id}/automation",
"Trigger configuration and firings; cursor and limit page retained receipts",
),
("context-snapshot", "overmind://context/snapshots/{id}", "Retained context identity"),
("capability", "overmind://capabilities/{capability}", "Capability details"),
("trace", "overmind://traces/{trace_id}", "Trace and bounded spans"),
("session", "overmind://sessions/{session}", "Session and trace references"),
("dataset", "overmind://datasets/{dataset}", "Dataset metadata"),
(
"dataset-pipeline",
"overmind://dataset-pipelines/{id}",
"Immutable reusable transformation revision",
),
(
"dataset-pipeline-package",
"overmind://dataset-pipeline-packages/{id}",
"Package inventory; file, offset and limit page retained code",
),
(
"dataset-pipeline-binding",
"overmind://dataset-pipeline-bindings/{id}",
"Source binding, limits and checkpoint",
),
(
"pipeline-diagnostics",
"overmind://pipeline-diagnostics/{id}",
"Untrusted script diagnostics; step, channel, offset and limit",
),
("checkpoint", "overmind://checkpoints/{id}", "Retained checkpoint identity"),
("eval-row", "overmind://eval-rows/{row}", "Evaluation row definition"),
("eval-run", "overmind://eval-runs/{eval_run}", "Evaluation run status"),
(
"eval-set",
"overmind://eval-sets/{eval_set}",
"Eval set and complete matrix rows; limit (1–100, default 20) and offset page the set",
),
("finetune", "overmind://finetunes/{job_id}", "Fine-tuning job status"),
("deployment", "overmind://deployments/{deployment}", "Deployment status"),
("connector", "overmind://connectors/{connector}", "Connector metadata and sync status"),
("job", "overmind://jobs/{kind}/{id}", "Project job status"),
(
"operation",
"overmind://operations/{id}",
"Durable operational events; after and limit page the timeline",
),
(
"exploration-strata",
"overmind://jobs/data_exploration/{id}/strata",
"Sampling strata; optional limit (1–100) and offset query parameters",
),
]
return [
types.ResourceTemplate(
name=name,
title=title,
uriTemplate=template + "{?project_id}",
description=(
f"{title}. Current worker state and measurements, inference metrics and activity; optional period=1h|24h|7d|30d|all and source=application|all query parameters (defaults: all)."
if name == "deployment"
else title
),
mimeType=JSON_MIME,
)
for name, template, title in templates
]
async def read_resource(uri: AnyUrl) -> Iterable[ReadResourceContents]:
context = get_context()
try:
if not context.has_permission("read"):
raise MCPError("permission_denied", "The connection does not grant resource access.")
parsed = urlparse(str(uri))
ids = parse_qs(parsed.query, keep_blank_values=True).get("project_id", [])
if len(ids) > 1:
raise MCPError("invalid_input", "Pass exactly one project_id.")
static = parsed.netloc in {
"interface",
"dataset-upload",
"dataset-export",
"checkpoint-download",
"connector-setup",
}
if not static or ids:
context = await sync_to_async(project_context, thread_sensitive=True)(
context, ids[0] if ids else None
)
payload = await sync_to_async(_read_resource_sync, thread_sensitive=True)(
context.project, str(uri)
)
if context.project and context.token.scope.get("scope") == "account":
payload = project_references(payload, context.project.pk)
except MCPError as error:
code = 404 if error.data.code == "resource_not_found" else 400
raise McpError(
types.ErrorData(code=code, message=error.data.message, data=error_payload(error))
) from None
except Exception:
error = internal_error()
raise McpError(types.ErrorData(code=500, message=error.data.message)) from None
return [
ReadResourceContents(
content=json.dumps(payload, default=str, ensure_ascii=False), mime_type=JSON_MIME
)
]