Building a 100% local MCP Client

...with complete code walkthrough and explanation.

👉
Hey! Enjoy our free data science newsletter! Subscribe below and receive a free data science PDF (530+ pages) with 150+ core data science and machine learning lessons.

TODAY'S ISSUE

TODAY’S DAILY DOSE OF DATA SCIENCE

​Building a 100% local MCP Client​

An MCP client is a component within an AI application (like Cursor) that establishes standardized connections to external tools and data sources via the Model Context Protocol (MCP).

Today, let us show you how it is built 100% locally.

Tech stack:

  • LlamaIndex to build the MCP-powered Agent.
  • Ollama to locally serve Deepseek-R1.
  • LightningAI for development and hosting.

Here's our workflow:

  • The user submits a query.
  • Agent connects to the MCP server to discover tools.
  • Based on the query, the agent invokes the right tool and gets context
  • Agent returns a context-aware response.

The code is available in this Studio: ​Build a 100% local MCP Client​. You can run it without any installations by reproducing our environment below:

If you prefer watching, here's a video walkthrough:

Code walkthrough

Local MCP Client Implementation

Let’s implement this!

Build an SQLite MCP Server

For this demo, we've built a simple SQLite server with two tools:

  • add data
  • fetch data

This is done to keep things simple, but the client we're building can connect to any MCP server out there.

Set Up LLM

We'll use a locally served Deepseek-R1 via Ollama as the LLM for our MCP-powered agent.

Define system prompt

We define our agent’s guiding instructions to use tools before answering user queries.

Feel free to tweak this to fit your requirements.

Define the Agent

We define a function that builds a typical LlamaIndex agent with its appropriate arguments.

The tools passed to the agent are MCP tools, which LlamaIndex wraps as native tools that can be easily used by our FunctionAgent.

Define Agent Interaction

We pass user messages to our FunctionAgent with a shared Context for memory, stream tool calls, and return its reply.

We manage all the chat history and tool calls here.

Initialize MCP Client and the Agent

Launch the MCP client, load its tools, and wrap them as native tools for function-calling agents in LlamaIndex.

Then, pass these tools to the agents and add the context manager.

Run the Agent

Finally, we start interacting with our agent and get access to the tools from our SQLite MCP server.

As shown above:

  • When we say something like "Add Rafael Nadal...," the agent understands the intent, generates a corresponding SQL INSERT command, and stores the data in the database.
  • When we say "fetch data," it runs a SELECT query and retrieves the data.
  • It then presents the result back to the user in a readable format.

And there you go, we have built our 100% local MCP client!

The code is available in this Studio: ​Build a 100% local MCP Client​. You can run it without any installations by reproducing our environment below:

Thanks for reading!

ROADMAP

From local ML to production ML

Once a model has been trained, we move to productionizing and deploying it.

If ideas related to production and deployment intimidate you, here’s a quick roadmap for you to upskill (assuming you know how to train a model):

This roadmap should set you up pretty well, even if you have NEVER deployed a single model before since everything is practical and implementation-driven.

THAT'S A WRAP

No-Fluff Industry ML resources to

Succeed in DS/ML roles

At the end of the day, all businesses care about impact. That’s it!

  • Can you reduce costs?
  • Drive revenue?
  • Can you scale ML models?
  • Predict trends before they happen?

We have discussed several other topics (with implementations) in the past that align with such topics.

Here are some of them:

  • Learn sophisticated graph architectures and how to train them on graph data in this crash course.
  • So many real-world NLP systems rely on pairwise context scoring. Learn scalable approaches here.
  • Run large models on small devices using Quantization techniques.
  • Learn how to generate prediction intervals or sets with strong statistical guarantees for increasing trust using Conformal Predictions.
  • Learn how to identify causal relationships and answer business questions using causal inference in this crash course.
  • Learn how to scale and implement ML model training in this practical guide.
  • Learn 5 techniques with implementation to reliably test ML models in production.
  • Learn how to build and implement privacy-first ML systems using Federated Learning.
  • Learn 6 techniques with implementation to compress ML models.

All these resources will help you cultivate key skills that businesses and companies care about the most.

Our newsletter puts your products and services directly in front of an audience that matters — thousands of leaders, senior data scientists, machine learning engineers, data analysts, etc., around the world.

Get in touch today →


Join the Daily Dose of Data Science Today!

A daily column with insights, observations, tutorials, and best practices on data science.

Get Started!
Join the Daily Dose of Data Science Today!

Great! You’ve successfully signed up. Please check your email.

Welcome back! You've successfully signed in.

You've successfully subscribed to Daily Dose of Data Science.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.