Streamlining AI-to-API Communication: A Practical Guide to Model Context Protocol

Have you ever wondered how AI assistants/apps/tools interact with external systems like databases, APIs, or file systems? Traditionally, we've relied on point-to-point integrations where each connection requires custom code and handling. Model Context Protocol (MCP) (from Anthropic) can make these integrations more systematic because it creates a standardized interface that works across different services and functions. Today, I'll show you how to build a simple GitHub file search tool using this protocol.

What is Model Context Protocol?

At its core, Model Context Protocol is a simple but powerful pattern that standardizes how AI models communicate with external systems. It defines:

  1. How functions are described to AI models
  2. How AI models decide which functions to call
  3. How parameters are structured and validated
  4. How results are returned in a consistent format

This standardization creates a clean separation between AI models and the tools they use.

Think of MCP like a USB-C port for AI applications. Just as USB-C provides a standardized way to connect your devices to various peripherals and accessories, MCP provides a standardized way to connect AI models to different data sources and tools. - Anthropic

Let's build a simple GitHub search tool that an AI assistant can use to help users find code and repositories. You can find the full source code here.

Function Definitions with JSON Schema

First, we define our available functions with complete schemas that tell the AI model what each function does and what parameters it needs:

Function Implementations

Next, we implement the actual functions that will be called:

MCP Server Implementation

Now we create the server that will handle MCP requests:

AI Model Integration

Here's how an AI model would use this MCP implementation:

  1. First, the AI model learns about available functions:
  1. When a user asks a question, the AI decides which function to call:

    User: "Can you find JavaScript React component examples on GitHub?"

AI's internal reasoning:

This request is about finding code on GitHub. I should use the search_code function with an appropriate query for React components in JavaScript.
  1. The AI model calls the appropriate function:
  1. The AI uses the results to respond to the user:
I found several React component examples on GitHub. Here are some notable ones:

1. A carousel component in user/cool-react-components
2. A date picker in organization/react-date-tools
3. A modal dialog in another-dev/react-ui-kit

Would you like me to show you the code for any of these components?
  1. If the user wants to see a specific file, the AI makes another function call:

    User: "Yes, show me the carousel component"

The AI can then show the user the code, explain it, or help modify it.1

How It All Works Together

Here's how the entire interaction flows:

User → AI Model → MCP Server → GitHub API → MCP Server → AI Model → User
  1. User asks a question about finding code
  2. AI model decides to use search_code function
  3. AI calls the MCP server with function name and parameters
  4. MCP server makes GitHub API request
  5. GitHub returns results to MCP server
  6. MCP server formats and returns results to AI
  7. AI processes results and responds to user

Benefits of Model Context Protocol

This approach provides several key advantages:

  1. Standard Interface: AI apps/tools only need to understand one protocol to interact with many different tools.
  2. Function Discovery: AI apps/tools can dynamically learn what functions are available.
  3. Clear Contracts: Function definitions clearly specify what parameters are required and what they do.
  4. Separation of Concerns: The AI model focuses on understanding user intent while the MCP functions handle implementation details.
  5. Easy Extensibility: Adding new functionality just means adding new function definitions and implementations.

Beyond This Example

The GitHub search tool demonstrates MCP with a real-world API, but the pattern can be applied to virtually any external system:

  • Database queries
  • Authentication services
  • File operations
  • Machine learning model inference
  • IoT device control

Model Context Protocol creates a standardized way for AI models to interact with external systems. By defining a clear interface between AI assistants and tools, MCP enables more powerful and flexible applications without requiring custom integration for each new tool.

The complete code for this example is available in my GitHub repo. Feel free to explore, run it yourself, and extend it with new functions.

The next time you build a tool that needs to communicate with an API or external system, consider using Model Context Protocol as your foundation. It's a simple pattern that can make your code more maintainable and future-proof.

Thanks for reading The AI Engineering Brief! Subscribe for free to receive new posts.


  1. This toy example won’t do this but you can extend it to implement this feature

Subscribe to The AI Engineering Brief

No spam, no sharing to third party. Only you and me.