In which Daina rapidly develops a tutorial for PyData VT (slides here; video here).
A Model Context Protocol (MCP) server that connects Claude Desktop to the New York Times Books API, enabling Claude to search and retrieve information about NYTimes Best Sellers lists.
This tutorial demonstrates how to build an MCP server using Python and conda, providing a practical introduction to extending Claude's capabilities with external data sources.
This tutorial was created to demonstrate:
- Building MCP servers with Python
- Using conda for environment management
- Integrating external APIs with Claude Desktop
Perfect for developers who want to extend Claude's capabilities with custom data sources and tools!
Model Context Protocol (MCP) is an open standard that allows AI assistants like Claude to connect to external data sources and tools. Think of it as a bridge that lets Claude access information beyond its training data. In this case, real-time data from the NYTimes Books API.
When you ask Claude a question about NYTimes best sellers, Claude recognizes it needs external data, calls this MCP server, which then queries the NYTimes API and returns the results.
This MCP server provides two tools that Claude can use:
- get_best_sellers: Retrieve books from a specific NYTimes Best Sellers list (fiction, non-fiction, etc.)
- get_best_sellers_overview: Get an overview of all Best Sellers lists with the top 5 books from each
- Anaconda or Miniconda installed
- Claude Desktop installed
- A NYTimes Developer API key (free)
nytimes-mcp-server/
├── nytimes_mcp_server/ # Main package directory
│ ├── __init__.py # Package initialization
│ ├── __main__.py # Entry point for running as module
│ ├── server.py # MCP server implementation
│ └── nyt_api.py # NYTimes API wrapper
├── .env # Your API key (never commit this!)
├── .env.example # An example environment file just for this tutorial
├── .gitignore # Git ignore rules
├── test_manual.py # Test script for API connectivity
└── README.md # This file
- Go to https://developer.nytimes.com/get-started
- Sign up or log in
- Create a new app in your apps
- Enable the "Books API"
- Copy your API key
git clone https://github.com/yourusername/nytimes-mcp-server.git
cd nytimes-mcp-server# Create the environment with required dependencies
conda create -n mcp_tutorial python=3.13 -y
# Activate the environment
conda activate mcp_tutorial
# Install dependencies
conda install mcp httpx python-dotenv -yCreate a .env file in the project root. You can copy the .env.example file and edit the contents or create your own:
cp .env.example .envEdit .env and add your API key:
NYTIMES_API_KEY=your_actual_api_key_here
Important: Never commit your .env file to version control! Add .env to your .gitignore file.
Run the test script to verify everything works:
python test_manual.pyYou should see output showing successful API calls to the NYTimes Books API.
Get your project's absolute path:
pwdGet your conda Python path (with environment activated):
which pythonEdit the Claude Desktop configuration file.
You can do this by navigating to Settings > Developer > Edit Config OR run the following:
macOS: (TextEdit or your favorite text editor)
open -a TextEdit ~/Library/Application\ Support/Claude/claude_desktop_config.jsonWindows:
%APPDATA%\Claude\claude_desktop_config.json
Linux:
~/.config/Claude/claude_desktop_config.json
Add your server configuration (replace paths with your actual paths from the steps above):
{
"mcpServers": {
"nytimes-books": {
"command": "/path/to/anaconda3/envs/mcp_tutorial/bin/python",
"args": [
"-m",
"nytimes_mcp_server"
],
"cwd": "/path/to/nytimes-mcp-server",
"env": {
"PYTHONPATH": "/path/to/nytimes-mcp-server"
}
}
}
}Configuration explained:
command: Full path to your conda environment's Python interpreter- Must be the absolute path to the Python interpreter
- Points to Python in your specific conda environment
Understanding the path:
/opt/anaconda3/envs/mcp_tutorial/bin/python
│ │ │ │ │ └─ Python executable
│ │ │ │ └───── bin directory (executables)
│ │ │ └────────────────── Your environment name
│ │ └─────────────────────── conda environments directory
│ └───────────────────────────────── Anaconda installation
└────────────────────────────────────── Root directory
args:-mtells Python to run the package as a module andnytimes_mcp_serveris the name of our packagecwd: Current working directory (where.envis located)- Must be the absolute path to where your code lives
- This is where the server runs
env.PYTHONPATH: Tells Python where to find your package
- Completely quit Claude Desktop (not just close the window)
- Reopen Claude Desktop
- Navigate to Settings > Developer to see if your local MCP server is running successfully
Create a new chat and click the Search and Tools icon. Make sure nytimes-books tool is turned on, then ask Claude questions like:
- "What are the fiction best sellers this month?"
- "Show me an overview of all the NYTimes best seller lists"
- "What's currently #1 on the hardcover non-fiction list?"
Claude should now use your MCP server to fetch real-time data!
Check the logs:
# macOS
tail -f ~/Library/Logs/Claude/mcp*.log
# Windows
# Check %APPDATA%\Claude\logs\
# Linux
# Check ~/.config/Claude/logs/Common issues:
-
"No module named nytimes_mcp_server"
- Make sure
PYTHONPATHis set in your config - Verify
cwdandPYTHONPATHboth point to your project root
- Make sure
-
"API key not found"
- Check that
.envexists in the directory specified bycwd - Verify the API key is correctly set in
.env
- Check that
-
API returns 404 errors
- Verify your API key is valid at https://developer.nytimes.com/
- Ensure you've enabled the Books API for your key
You can test the server directly:
cd /path/to/nytimes-mcp-server
conda activate mcp_tutorial
python -m nytimes_mcp_serverThe server should start and display:
INFO:root:Starting NYTimes Books MCP Server
INFO:root:API key found, starting server...
Press Ctrl+C to stop it.
You ask Claude a question
↓
Claude Desktop recognizes it needs NYTimes data
↓
Claude calls your MCP server (via stdio)
↓
Your MCP server queries the NYTimes API
↓
Results flow back to Claude
↓
Claude presents the answer to you
-
nyt_api.py: Handles all interactions with the NYTimes Books API
- Manages HTTP requests
- Formats API responses
- Handles errors gracefully
-
server.py: Implements the MCP server
- Defines tools that Claude can use
- Handles tool execution requests
- Manages communication with Claude Desktop
-
main.py: Entry point for running the server
- Sets up async event loop
- Initializes stdio communication
python test_manual.py- mcp: Official MCP SDK from Anthropic
- httpx: Modern HTTP client for API calls
- python-dotenv: Loads environment variables from .env files
This tutorial covers the basics of building an MCP server but more could be added. Potential enhancements include:
- Caching: Store API results temporarily to reduce API calls
- More endpoints: The NYTimes Books API has additional endpoints (like reviews and history)
- Advanced search: Implement filtering and sorting options
- Error recovery: Add retry logic for transient failures
- Rate limiting: Implement request throttling to respect API limits
A separate tutorial is in the works for MCP + conda + Claude Code.
Contributions are welcome! Please feel free to submit a Pull Request.
- Built with the Model Context Protocol
- Uses the NYTimes Books API
- Tutorial developed for the Anaconda community