/mcp-tutorials

How to compute similarity between two MCP states?

Learn to compute similarity between two MCP states using JSON payloads, vectorization, and cosine similarity—with a step-by-step Python guide.

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 compute similarity between two MCP states?

 

Step 1: Understand the Components of MCP

 

  • System Instructions:

    Define what your model is supposed to be, such as “You are a helpful assistant specialized in finance.”
  • User Profile:

    Capture the name, preferences, and goals of the user interacting with the system.
  • Document Context:

    Monitor the knowledge base or any recent uploads that the model has access to.
  • Active Tasks / Goals:

    Specify objectives and to-dos that the model should focus on.
  • Tool Access:

    Determine what external tools the model is permitted to call, such as web, Python, or databases.
  • Rules / Constraints:

    Outline specific limitations, such as “never suggest medical diagnoses.”

 

Step 2: Implement MCP in Your System

 

  • Choose an AI/LLM that supports MCP, such as GPT, Claude, or Gemini.
  • Use a programming framework or platform like AutoGPT, LangChain, or CrewAI to integrate MCP into your application.
  • Define the MCP structure and elements based on your use case.

 

Step 3: Structure MCP Payloads

 

Create a JSON structure to represent the MCP context. Here’s an example skeleton:


{
  "system_instructions": "You are a helpful assistant specialized in finance.",
  "user_profile": {
    "name": "John Doe",
    "preferences": {
      "preferencesList": ["short answers", "data-driven insights"]
    },
    "goals": ["improve budgeting skills"]
  },
  "document_context": {
    "knowledge_base": ["financial terms glossary", "recent uploads"],
    "recentuploads": ["financialreport_q1.pdf"]
  },
  "active_tasks": ["learn about stock market basics"],
  "tool_access": {
    "allowed_tools": ["web browser", "calculator"]
  },
  "rules_constraints": {
    "avoid": ["medical diagnoses"]
  }
}

 

Step 4: Compute Similarity Between Two MCP States

 

To determine the similarity between two MCP states, you need to compare the JSON structures.

  • Extract the key components from both MCP states.
  • Convert each section into a comparable vector or structure. For instance, concatenate key strings or use NLP techniques for sentence meanings.
  • Calculate the similarity. You can use cosine similarity, Jaccard index, or other similarity measures that suit your needs.

Example Python code using cosine similarity from the sklearn library:


from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

Example MCP JSON strings
mcpstate1 = """{
  "system_instructions": "You are a helpful assistant specialized in finance...",
  ...
}"""

mcpstate2 = """{
  "system_instructions": "You are a chatbot designed for assisting users with finance...",
  ...
}"""

vectorizer = TfidfVectorizer()

Convert text into vectors
vectors = vectorizer.fittransform([mcpstate1, mcpstate_2])

Compute cosine similarity
similaritymatrix = cosinesimilarity(vectors)

similarityscore = similaritymatrix[0, 1]

print(f"Similarity score between MCP states: {similarity_score}")

 

Step 5: Evaluate and Adjust MCP Settings

 

  • Based on the similarity scores, adjust MCP elements to bring desired predictability and effectiveness.
  • If a similarity score is lower than expected, refine the system instructions, document context, or constraints to align more closely with your goals.
  • Test the changes in a controlled environment to observe behavior changes.

 

The above steps guide you through understanding, implementing, and analyzing the similarity between two MCP states, facilitating predictable and effective model behavior.

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