/mcp-tutorials

How to run vector search and inject results into MCP context block?

Run vector search and inject results into an MCP context block. Master embeddings, vector DB setup, result formatting & model integration efficiently.

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 run vector search and inject results into MCP context block?

 

Step 1: Understand the Vector Search Concept

 

  • Before injecting search results into an MCP context block, you need to understand what vector search involves. Vector search uses embedding models to convert data (text, images, etc.) into numerical vectors. Similarity between vectors allows you to retrieve related data efficiently.

 

Step 2: Obtain or Develop Vector Embeddings

 

  • You first need a source of vector embeddings, which might be pre-trained embedding models or ones you develop yourself on specific datasets. For text, models like BERT or GPT-3 generate embeddings for document retrieval.

 

Step 3: Set Up a Vector Database

 

  • Use a vector database like Pinecone, Milvus, or Weaviate to index your embeddings.

  • Example: Setting up a vector database with Pinecone.

    
    import pinecone
    
    # Initialize connection to Pinecone
    pinecone.init(api_key='your-api-key', environment='your-environment')
    
    # Create or connect to an index
    index = pinecone.Index('example-index', dimension=128)  # dimension should match your embedding size
    
    # Upsert vectors (assuming embeddings is a list of tuples with ID)
    index.upsert(vectors=embeddings)
    

 

Step 4: Conduct Your Vector Search

 

  • With your vectors indexed, you can perform a search to find similar items.

    
    # Generate an embedding for your query
    queryembedding = generateembeddingforquery('your query text')
    
    # Perform the search in the vector database
    result = index.query(queries=[queryembedding], topk=5)  # top_k specifies the number of results
    
    # Access search results
    search_results = result['matches']
    

 

Step 5: Format Results Suitable for MCP

 

  • Convert the results from your search into a suitable format for your MCP context block.

  • Example formatting search results:

    
    def formatresultsfor_mcp(results):
        formatted_results = []
        for item in results:
            data = retrievedataby_id(item['id'])  # Function to get raw data using ID
            formatted_results.append({
                'title': data['title'],
                'snippet': data['snippet']
            })
        return formatted_results
    
    searchcontext = formatresultsformcp(search_results)
    

 

Step 6: Inject Results into MCP Context Block

 

  • With the formatted search results, you can integrate them into your MCP context block.

    
    mcpcontextblock = {
        'system_instructions': 'You are a well-versed assistant in literature.',
        'active_tasks': ['Answer query using relevant documents'],
        'documentcontext': searchcontext,  # Injecting search results here
        'user_profile': {
            'name': 'John Doe',
            'preferences': ['concise answers', 'cite documents']
        }
    }
    

 

Step 7: Implement the MCP with Your Model

 

  • Use the MCP context block with a model such as Claude or others that support the MCP format.

    
    def askmodelwithmcp(mcpblock, query):
        response = llm_ask(
            context=mcp_block,
            query=query
        )
        return response
    
    # Final query example
    modelresponse = askmodelwithmcp(mcpcontextblock, 'Tell me about literary styles.')
    

 

Step 8: Refine and Iterate Your Process

 

  • Review results for effectiveness. Tweak embedding models, search parameters, or the structuring of the MCP block as needed to improve performance and relevancy.

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