Files
mcp-server/spark_executor/tools/external_jobs.py
T
ClaudeandClaude Fable 5 6cf68439a2 feat: add external job tools + improve LLM-facing tool descriptions
Add 3 new MCP tools for inspecting YARN applications NOT submitted
through this service: get_external_job_logs, get_external_job_status,
get_external_job_result. Each takes application_id + connection_name
and queries YARN directly, bypassing the local JobStore.

- spark_executor/tools/external_jobs.py: 3 tool functions
- spark_executor/tools/requests.py: 3 new Pydantic body models
  (ExternalJobLogsRequest, ExternalJobStatusRequest,
  ExternalJobResultRequest)
- spark_executor/server.py: 3 new POST routes with explicit operation_id
- tests/unit/test_external_jobs.py: 7 unit tests
- tests/integration/test_mcp_routes.py: assert 20 tool routes
- README.md: list the 3 new tools

To make the LLM pick the right tool and not guess at field values,
also:

- Add Pydantic field descriptions for 22 fields across 8 request models
  (SaveConnectionRequest, UpdatePendingJobRequest, GetJobLogsRequest,
  JobIdRequest, PendingIdRequest, ConnectionNameRequest, plus the new
  ExternalJob*Request models).
- Update 12 route descriptions with cross-references, prerequisite
  context, and 400 behavior notes.
- Refactor _unknown_job_error: an input that looks like a YARN
  application_id (starts with 'application_') now returns HTTP 400
  (ValueError) with a hint message naming the right external tool;
  other not-found cases still return 404 (KeyError). This catches the
  common LLM mistake of passing application_id to the internal
  get_job_* / kill_job tools.
- 4 new unit tests for the 400 behavior.

Tests: 356 passed (up from 242).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-08 20:03:40 +08:00

66 lines
3.0 KiB
Python

# coding=utf-8
"""
@Time :2026/7/8
@Author :tao.chen
Tools for inspecting YARN applications NOT submitted through this service.
These bypass the local JobStore and require the caller to supply both the
YARN application_id and the name of a saved Connection.
"""
import json
from common.logging import logger
from spark_executor.core.yarn_client import YarnClientConfig, get_application_status, get_application_logs
from spark_executor.models import JobStatus, JobResult
from spark_executor.tools.connections import store as conn_store
def get_external_job_logs(application_id: str, connection_name: str, tail_chars: int = 5000) -> str:
"""Fetch aggregated container logs for a YARN application by ID."""
logger.debug(f"get_external_job_logs enter application_id={application_id} connection_name={connection_name} tail_chars={tail_chars}")
conn = conn_store.get(connection_name)
if conn is None:
raise KeyError(f"Connection not found: {connection_name}")
config = YarnClientConfig.from_connection(conn)
full = get_application_logs(application_id, config)
tailed = full[-tail_chars:] if len(full) > tail_chars else full
logger.info(
f"get_external_job_logs ok application_id={application_id} connection_name={connection_name} "
f"full_chars={len(full)} returned_chars={len(tailed)}"
)
return tailed
def get_external_job_status(application_id: str, connection_name: str) -> JobStatus:
"""Query YARN for an external application's current status."""
logger.debug(f"get_external_job_status enter application_id={application_id} connection_name={connection_name}")
conn = conn_store.get(connection_name)
if conn is None:
raise KeyError(f"Connection not found: {connection_name}")
config = YarnClientConfig.from_connection(conn)
state, raw = get_application_status(application_id, config)
logger.info(f"get_external_job_status ok application_id={application_id} connection_name={connection_name} state={state}")
return JobStatus(application_id=application_id, state=state, raw=raw)
def get_external_job_result(application_id: str, connection_name: str) -> JobResult:
"""Query YARN for an external application's terminal result view."""
logger.debug(f"get_external_job_result enter application_id={application_id} connection_name={connection_name}")
conn = conn_store.get(connection_name)
if conn is None:
raise KeyError(f"Connection not found: {connection_name}")
config = YarnClientConfig.from_connection(conn)
state, raw = get_application_status(application_id, config)
app = json.loads(raw).get("app", {})
result = JobResult(
application_id=application_id,
state=state,
final_status=app.get("finalStatus"),
diagnostics=app.get("diagnostics"),
tracking_url=app.get("trackingUrl"),
started_time=app.get("startedTime"),
finished_time=app.get("finishedTime"),
)
logger.info(f"get_external_job_result ok application_id={application_id} connection_name={connection_name} state={state}")
return result