/mcp-tutorials

How to implement embedding-based context filtering for MCP?

Learn how to implement embedding-based context filtering for MCP with our step-by-step guide featuring code examples, system instructions, and user profiling tips.

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 implement embedding-based context filtering for MCP?

 

Step 1: Understand the Basic Components of MCP

 

To implement embedding-based context filtering for MCP, begin by understanding its core components. MCP consists of:

  • System Instructions:

    Guidelines on behavior and tasks, e.g., "You are a helpful assistant specialized in finance."
  • User Profile:

    User information, preferences, and goals.
  • Document Context:

    Knowledge base or recent uploads relevant to current interactions.
  • Active Tasks/Goals:

    Current objectives or to-dos to focus the model’s operations.
  • Tool Access:

    Defines what the model can access, like web tools, databases, etc.
  • Rules/Constraints:

    Guidelines that restrict certain outputs or maintain focus within a domain.

 

Step 2: Set Up Your Development Environment

 

Ensure that your development environment is equipped for LLM operation and context filtering.

  • Programming Language:

    Python is commonly used for such tasks due to its robust libraries.
  • Libraries:

    Install required libraries such as Hugging Face Transformers, NumPy, and any semantic search libraries like FAISS.

pip install transformers numpy faiss-cpu

 

Step 3: Prepare the System Instructions

 

Define the system instructions to guide the language model appropriately. This is typically a text file or a string within your code that sets the overall behavior of the model.

system_instructions = "You are a helpful assistant specialized in finance."

 

Step 4: Implement the User Profile

 

Create a user profile to tailor responses. This will be a dictionary holding user information, preferences, and goals.

user_profile = {
"name": "John Doe",
"preferences": ["finance", "technology"],
"goals": ["learn about stock market"]
}

 

Step 5: Incorporate Document Context

 

Add relevant document context to support the language model with background knowledge.

document_context = [
"The stock market is unpredictable, but historical data can provide insights.",
"Fintech combines finance and technology to offer new services."
]

 

Step 6: Define Active Tasks/Goals

 

Specify the active tasks or goals to ensure the model focuses on the current objectives.

active_tasks = [
"Explain the basics of stock trading.",
"Discuss the impact of technology on finance."
]

 

Step 7: Set Tool Access

 

Outline which external tools the model can access to enhance functionality.

tool_access = {
"web_access": True,
"database_access": False
}

 

Step 8: Establish Rules/Constraints

 

Implement rules to constrain the model’s output where necessary.

rules_constraints = [
"Do not provide medical or legal advice.",
"Focus on finance-related queries."
]

 

Step 9: Implement Embedding-Based Context Filtering

 

To perform context filtering, use embeddings to measure the relevance of various context elements. Compute embeddings for the context and queries and use cosine similarity or another metric to filter pertinent information.

from transformers import AutoTokenizer, AutoModel
import numpy as np

Load your transformer model

tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')
model = AutoModel.from_pretrained('distilbert-base-uncased')

def embed_text(text):
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)
return outputs.last_hidden_state.mean(dim=1).detach().numpy()

Example usage

query_embedding = embed_text("Tell me about fintech.")
document_embeddings = [embed_text(doc) for doc in document_context]

Calculate cosine similarities

similarities = [(doc, np.dot(embed, query_embedding.T)) for doc, embed in zip(document_context, document_embeddings)]
filtered_context = [doc for doc, sim in similarities if sim > 0.8] # Filter based on threshold

 

Step 10: Integrate into MCP Workflow

 

Finally, integrate the filtered context into the MCP workflow, feeding it into the language model alongside the structured components to enhance response accuracy.

Incorporate filtered context into the MCP

mcp_input = {
"system_instructions": system_instructions,
"user_profile": user_profile,
"context": filtered_context,
"active_tasks": active_tasks,
"tool_access": tool_access,
"rules_constraints": rules_constraints
}

Pass this structured input to your LLM

llm_response = yourLLM(mcp_input)

 

Step 11: Evaluate and Iterate

 

Continuously evaluate the effectiveness of the context filtering and adjust components where necessary to enhance the model's performance and adaptability.

Define evaluation criteria and iterate improvements

def evaluate_model_output(output):
return "Satisfactory" if "finance" in output else "Needs Improvement"

Example output evaluation

result = evaluate_model_output(llm_response)

print(result)

 

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