tool_prepare_answer_key
Extract all assignment questions, answers, and explanations to create a complete answer key for grading preparation in Gradescope.
Instructions
Prepare a complete answer key for an entire assignment.
Extracts ALL questions from the outline (prompt text, reference answers,
explanations) and saves to /tmp/gradescope-answerkey-{assignment_id}.md.
Run this ONCE before grading to avoid re-fetching question details.
Args:
course_id: The Gradescope course ID.
assignment_id: The assignment ID.Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| course_id | Yes | ||
| assignment_id | Yes |
Implementation Reference
- The actual implementation of the tool logic that prepares the answer key.
def prepare_answer_key(course_id: str, assignment_id: str) -> str: """Prepare a complete answer key for an entire assignment. Extracts ALL questions from the assignment outline, including: - Question numbers, types, and weights - Prompt/question text (if available in structured data) - Explanation/reference answers (if provided by the instructor) - For questions without reference answers, generates a rubric-based draft Saves the result to /tmp/gradescope-answerkey-{assignment_id}.md. This file can then be referenced when grading individual submissions, saving context by not having to re-fetch question details each time. Args: course_id: The Gradescope course ID. assignment_id: The assignment ID. """ if not course_id or not assignment_id: return "Error: course_id and assignment_id are required." try: questions = _fetch_assignment_questions(course_id, assignment_id) except AuthError as e: return f"Authentication error: {e}" except ValueError as e: return f"Error: {e}" except Exception as e: return f"Error preparing answer key: {e}" # Outline data is optional (scanned exams don't have AssignmentEditor) try: outline_props = _get_outline_data(course_id, assignment_id) except Exception: outline_props = {} outline_questions = outline_props.get("questions", {}) assignment_info = outline_props.get("assignment", {}) title = assignment_info.get("title", f"Assignment {assignment_id}") # Build a sorted list of questions question_list = [] for qid, q in questions.items(): parent_id = q.get("parent_id") q_data = { "id": qid, "title": q.get("title", ""), "weight": q.get("weight", 0), "type": q.get("type", "Unknown"), "parent_id": parent_id, "index": q.get("index", 0), } # Build label if parent_id and str(parent_id) in questions: parent = questions[str(parent_id)] q_data["label"] = f"Q{parent.get('index', '?')}.{q.get('index', '?')}" else: q_data["label"] = f"Q{q.get('index', '?')}" # Extract prompt text and explanation from outline outline_q = outline_questions.get(str(qid), {}) prompt_parts = [] explanation_parts = [] for item in outline_q.get("content", []): item_type = item.get("type") - src/gradescope_mcp/server.py:689-701 (registration)Registration of the 'tool_prepare_answer_key' tool as an MCP tool, which calls the 'prepare_answer_key' helper function.
@mcp.tool() def tool_prepare_answer_key(course_id: str, assignment_id: str) -> str: """Prepare a complete answer key for an entire assignment. Extracts ALL questions from the outline (prompt text, reference answers, explanations) and saves to /tmp/gradescope-answerkey-{assignment_id}.md. Run this ONCE before grading to avoid re-fetching question details. Args: course_id: The Gradescope course ID. assignment_id: The assignment ID. """ return prepare_answer_key(course_id, assignment_id)