Overview
The Model Context Protocol (MCP) is an open standard that gives AI agents structured, type-safe access to external tools, data sources, and services. Where Chapter 11 taught you how to engineer context so that an LLM understands your repository, this chapter teaches you how to build the servers that let an LLM act on the world: querying PubChem for molecular properties, searching OpenAlex for literature, running instrument control loops, and recording results in a vector database.
MCP follows a client-server architecture inspired by the Language Server Protocol (LSP) that powers modern code editors. A host application (such as Claude Desktop or a research agent) contains one or more clients, each maintaining a one-to-one connection with an MCP server. The server declares a set of tools (callable functions), resources (readable data), and prompts (reusable prompt templates), all described through JSON Schema so that the agent can discover and invoke them without human intervention.
By the end of this chapter, you will have built a complete MCP server that wraps three scientific APIs (PubChem, OpenAlex, and a local vector database), tested it with pytest and the MCP inspector, and packaged it for distribution. The server will become a building block for the multi-agent teams of Chapter 17 and the AI scientist architecture in Chapter 53.
Prerequisites
You should be comfortable with Python async programming (async/await,
asyncio event loops) and have read
Chapter 10: Prompting to Programming
for the basics of how LLMs call tools. Familiarity with JSON Schema (or at least
Python type hints that generate it) is helpful but not required; we introduce what
you need. Chapter 11: Context Engineering
provides the conceptual backdrop for why structured context delivery matters.
Learning Outcomes
- Explain the MCP architecture: hosts, clients, servers, transports, and the JSON-RPC message flow.
- Define tools, resources, and prompts with JSON Schema validation and rich descriptions.
- Implement an MCP server in Python using the official SDK, exposing scientific database queries as typed tools.
- Integrate external scientific APIs (PubChem, OpenAlex) and local services (vector databases) behind a single MCP interface.
- Apply authentication, sandboxing, and rate-limiting patterns to protect both the server and downstream services.
- Test MCP servers at the unit, integration, and end-to-end levels using pytest and the MCP inspector.
- Package and publish an MCP server for use by Claude Desktop, VS Code, and custom agent frameworks.
Sections
12.1 MCP Architecture
The Model Context Protocol from first principles: hosts, clients, servers, transports (stdio, SSE, Streamable HTTP). JSON-RPC messaging, capability negotiation, and the lifecycle of an MCP connection. Tool, resource, and prompt primitives with JSON Schema validation.
12.2 Implementing Scientific Tools
Building MCP tools that wrap scientific APIs: PubChem compound lookups, OpenAlex literature search, and chemistry computation. Input validation with JSON Schema, structured error handling, and rate-limiting patterns.
12.3 Testing and Publishing
Testing MCP servers with pytest: unit tests for individual tools, integration tests against live APIs, and end-to-end tests with the MCP inspector. Authentication, sandboxing with Docker, and publishing to package registries.
12.4 Building a Scientific MCP Server
Recipe: a complete MCP server combining PubChem, OpenAlex, and vector database tools into a unified scientific discovery interface. Server configuration, resource subscriptions, prompt templates, and integration with the Discovery Workbench.
Bibliography
Specifications & Standards
The official MCP specification defining the JSON-RPC message format, capability negotiation, and the tool/resource/prompt primitives. The authoritative reference for everything in this chapter.
The underlying RPC protocol that MCP builds on. Understanding JSON-RPC request/response/notification semantics clarifies MCP's message flow.
The validation language used by MCP to describe tool inputs and resource schemas. Core vocabulary for defining typed scientific tool parameters.
Tools & Libraries
The official Python SDK for building MCP servers and clients. Provides decorators, transport implementations, and type-safe tool definitions used throughout this chapter.
The TypeScript counterpart of the Python SDK. Useful for building MCP servers that run in Node.js or browser environments.
The Python library used in this chapter for querying PubChem's compound database. Simplifies SMILES lookups, property retrieval, and similarity searches.
The open scholarly metadata catalog that powers the literature search tool in our MCP server. Covers 250 million works with structured metadata.
The vector database used in Section 12.4 for semantic search over scientific documents. Lightweight enough for local development, scalable for production.
Scientific APIs & Databases
PubChem contains over 115 million compounds and is the world's largest open chemistry database. Our MCP server exposes compound property lookups and similarity searches.
The open replacement for Microsoft Academic Graph, providing structured metadata on 250+ million scholarly works used in our literature search tool.
Architecture & Design Patterns
The protocol that inspired MCP's architecture. Understanding LSP's host/client/server separation clarifies why MCP is designed the way it is.
Foundational work on teaching LLMs to use external tools, providing the conceptual motivation for structured tool interfaces like MCP.
A comprehensive survey of how foundation models interact with external tools, covering the design space that MCP occupies.
Tutorials & Guides
The official tutorial for building your first MCP server. Complements this chapter's deeper treatment with a minimal working example.