Part II: Discovery Through Software Engineering and Vibe Coding
Chapter 12: Building MCP Servers for Scientific Workflows

Building MCP Servers for Scientific Workflows

"I exposed 47 scientific tools through a well-typed JSON-RPC interface, and the agent still asked me for the weather."

An MCP Server That Lost Its Schema

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

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.

What's Next

With a working MCP server that exposes scientific tools to AI agents, Chapter 13: Discovery of Requirements shifts from building the interface between agents and tools to discovering what software should do in the first place. Requirements discovery is itself a search problem (recall the $(S, A, T, f, C)$ framework from Chapter 1), and the MCP servers you built here will become the hands and eyes through which requirements-gathering agents explore problem domains.

Bibliography

Specifications & Standards

Anthropic. (2024). Model Context Protocol Specification.

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.

JSON-RPC Working Group. (2013). JSON-RPC 2.0 Specification.

The underlying RPC protocol that MCP builds on. Understanding JSON-RPC request/response/notification semantics clarifies MCP's message flow.

JSON Schema. (2020). JSON Schema: A Media Type for Describing JSON Documents.

The validation language used by MCP to describe tool inputs and resource schemas. Core vocabulary for defining typed scientific tool parameters.

Tools & Libraries

Anthropic. (2024). MCP Python SDK.

The official Python SDK for building MCP servers and clients. Provides decorators, transport implementations, and type-safe tool definitions used throughout this chapter.

Anthropic. (2024). MCP TypeScript SDK.

The TypeScript counterpart of the Python SDK. Useful for building MCP servers that run in Node.js or browser environments.

Swain, M. (2017). PubChemPy: A Python wrapper for the PubChem PUG REST API.

The Python library used in this chapter for querying PubChem's compound database. Simplifies SMILES lookups, property retrieval, and similarity searches.

OurResearch. (2024). OpenAlex API Documentation.

The open scholarly metadata catalog that powers the literature search tool in our MCP server. Covers 250 million works with structured metadata.

Chroma. (2024). Chroma: The open-source embedding database.

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

Kim, S., et al. (2023). PubChem 2023 update. Nucleic Acids Research, 51(D1), D1373-D1380.

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.

Priem, J., Piwowar, H., & Orr, R. (2022). OpenAlex: A fully-open index of scholarly works, authors, venues, institutions, and concepts. arXiv:2205.01833.

The open replacement for Microsoft Academic Graph, providing structured metadata on 250+ million scholarly works used in our literature search tool.

Architecture & Design Patterns

Microsoft. (2016). Language Server Protocol Specification.

The protocol that inspired MCP's architecture. Understanding LSP's host/client/server separation clarifies why MCP is designed the way it is.

Schick, T., et al. (2023). Toolformer: Language models can teach themselves to use tools. NeurIPS 2023.

Foundational work on teaching LLMs to use external tools, providing the conceptual motivation for structured tool interfaces like MCP.

Qin, Y., et al. (2023). Tool learning with foundation models. arXiv:2305.15334.

A comprehensive survey of how foundation models interact with external tools, covering the design space that MCP occupies.

Tutorials & Guides

Anthropic. (2024). Building MCP Servers: Quickstart Guide.

The official tutorial for building your first MCP server. Complements this chapter's deeper treatment with a minimal working example.