Tuesday, 25 November 2025

#Module 3 – MCP JSON Schemas & Validation Layer

Module 3 – MCP JSON Schemas & Validation Layer

Module 3 – MCP JSON Schemas & Validation Layer

This module defines JSON Schemas for all core MCP data objects for our University Attendance System.

What You Will Build

  • Define JSON Schemas for: Course, Semester, Subject, Staff, Student, Attendance
  • Create schemas/ folder
  • Implement Python JSON schema validation
  • Expose validation as an MCP Tool

๐Ÿ“ Step 1 — Create Folder for Schemas

mkdir schemas

๐Ÿ“„ Step 2 — Create JSON Schemas

Create the file: schemas/course.schema.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Course",
  "type": "object",
  "properties": {
    "code": { "type": "string" },
    "name": { "type": "string" },
    "total_semesters": { "type": "number" }
  },
  "required": ["code", "name", "total_semesters"]
}

Semester Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Semester",
  "type": "object",
  "properties": {
    "course_id": { "type": "number" },
    "number": { "type": "number" },
    "type": { "type": "string", "enum": ["odd", "even"] }
  },
  "required": ["course_id", "number", "type"]
}

Subject Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Subject",
  "type": "object",
  "properties": {
    "semester_id": { "type": "number" },
    "code": { "type": "string" },
    "name": { "type": "string" }
  },
  "required": ["semester_id", "code", "name"]
}

Attendance Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Attendance",
  "type": "object",
  "properties": {
    "subject_id": { "type": "number" },
    "staff_id": { "type": "number" },
    "date": { "type": "string", "format": "date" },
    "hour": { "type": "number" },
    "student_id": { "type": "number" },
    "status": { "type": "string", "enum": ["present", "absent", "late"] }
  },
  "required": ["subject_id", "staff_id", "date", "hour", "student_id", "status"]
}

๐Ÿงช Step 3 — Python Validation Utility

# file: mcp_server/validator.py
import json, os
from jsonschema import validate, ValidationError

SCHEMA_PATH = "schemas"

def load_schema(name):
    with open(os.path.join(SCHEMA_PATH, f"{name}.schema.json")) as f:
        return json.load(f)

def validate_payload(schema_name, payload):
    try:
        schema = load_schema(schema_name)
        validate(payload, schema)
        return {"valid": True}
    except ValidationError as err:
        return {"valid": False, "error": str(err)}

๐Ÿ”ง Step 4 — Register MCP Validation Tool

# file: mcp_server/server.py (append)
from validator import validate_payload

@app.tool()
def validate_json(schema: str, payload: dict):
    """Validate a JSON object using the MCP schema system."""
    return validate_payload(schema, payload)

▶️ Step 5 — Test With Sample JSON

{
  "subject_id": 12,
  "staff_id": 4,
  "date": "2025-07-22",
  "hour": 3,
  "student_id": 91,
  "status": "present"
}

No comments:

Post a Comment

#MCP Index

MCP Tutorial – Module Index MCP Tutorial – 12 Modules Latest Edition for AU-CSE Final year Students Module 1 Introducti...