/mcp-tutorials

How to define a JSON schema for structuring MCP context?

Learn how to create a JSON schema for MCP context by defining components, validating the structure, and implementing robust LLM interactions.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to define a JSON schema for structuring MCP context?

 

Step 1: Understand the Components of MCP

 

To define a JSON schema for MCP, you need to first understand the various components that MCP entails:

  • System Instructions:

    General guidelines on how the model should behave.
  • User Profile:

    Details about the user such as name, preferences, and objectives.
  • Document Context:

    Information from knowledge bases or recent documents that may be relevant.
  • Active Tasks/Goals:

    Current objectives that the model should focus on.
  • Tool Access:

    Tools or APIs that the model can utilize to complete tasks.
  • Rules/Constraints:

    Specific guidelines or restrictions for model behavior.

 

Step 2: Structure Your JSON Schema

 

Once you understand the components, you need to structure these components into a JSON schema. Here is a basic outline of how you might represent these components:


{
  "SystemInstructions": {
    "type": "string",
    "description": "General guidelines for the model's behavior."
  },
  "UserProfile": {
    "name": {
      "type": "string",
      "description": "User's name or identifier."
    },
    "preferences": {
      "type": "object",
      "description": "User preferences for the model behavior.",
      "properties": {
        "language": {"type": "string"},
        "tone": {"type": "string"}
      }
    },
    "goals": {
      "type": "array",
      "items": {"type": "string"},
      "description": "User-defined objectives or goals."
    }
  },
  "DocumentContext": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "title": {"type": "string"},
        "content": {"type": "string"}
      },
      "required": ["title", "content"]
    },
    "description": "Relevant documents or knowledge for context."
  },
  "ActiveTasks": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "task": {"type": "string"},
        "priority": {"type": "string", "enum": ["low", "medium", "high"]}
      }
    },
    "description": "Current tasks the model should handle."
  },
  "ToolAccess": {
    "type": "object",
    "additionalProperties": {
      "type": "boolean"
    },
    "description": "Tools the model has access to."
  },
  "RulesConstraints": {
    "type": "array",
    "items": {"type": "string"},
    "description": "Constraints for the model's output or behavior."
  }
}

 

Step 3: Validate Your JSON Schema

 

Once your schema is defined, it's essential to validate it to ensure it aligns with your intended structure. You can use a JSON Schema validator tool to do this. Input your JSON data and schema into the validator to make sure everything is correct.

 

Step 4: Implement MCP in Your System

 

With a validated schema, implement MCP in your system by using it to structure the context you interact with the LLM. Use libraries like ajv in JavaScript or jsonschema in Python to validate context data dynamically at runtime.


// JavaScript using ajv
const Ajv = require('ajv');
const ajv = new Ajv();
const schema = { / your JSON schema / };
const validate = ajv.compile(schema);

const data = { / your context data / };
const valid = validate(data);

if (!valid) {
  console.log(validate.errors);
} else {
  console.log('Context is valid');
}

 

Step 5: Test and Iterate

 

Ensure thorough testing of the schema within your system, observing how well the structured context improves LLM behavior. Collect feedback, make necessary alterations, and iterate on your MCP schema to enhance performance consistency and predictability of the language model's output.

 

Following these steps will help you define a comprehensive JSON schema for structuring MCP context effectively, allowing more predictable and effective interactions with language models across various applications.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022