advanced
ai-engineering

Integrating MCP with AI Agents

Hook up an MCP server to Claude Desktop or arbitrary LangChain scripts.

mcp fundamentals
20 min read

Using MCP with Claude Desktop

The absolute easiest way to consume your custom Model Context Protocol is via Anthropic's official Claude Desktop application.

Step 1: Locate the Config File

Claude Desktop reads a central JSON configuration to discover external servers when it powers on.

  • Mac: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json

Step 2: Register your Server

Open the .json file and append your server module. In this scenario, we use our mcp-weather-server built in the last course.

json
{ "mcpServers": { "weather-server": { "command": "npx", "args": ["tsx", "/absolute/path/to/your/mcp-weather-server/index.ts"] } } }

Step 3: Prompt Claude

Restart Claude Desktop. Look for the small plug icon near your chat bar indicating an active connection. You can now prompt the LLM directly:

"What is the weather in Tokyo?"

Claude will detect your get_weather tool, execute it natively under the hood, and parse the resulting JSON string inside its standard conversation pipeline!

Success!

You have successfully enabled an intelligent AI to autonomously trigger local server execution without giving it root-level API access to everything you own!

Orchestrating MCP in Code (LangChain)

If you aren't using an isolated GUI like Claude Desktop, you must build an MCP Client. Below is a conceptual look at how a custom LangChain AI proxy script consumes MCP dynamically using Python.

python
from langchain_mcp_adapters.client import ClientSession from langchain_openai import ChatOpenAI # 1. Establish the connection to the custom remote/local server session = ClientSession( server_transport="stdio", command="npx", args=["tsx", "index.ts"] ) # 2. Extract tools dynamically requested by the Server mcp_tools = await session.list_tools() # 3. Mount those tools into your target LLM (OpenAI, Anthropic, Gemini) agent = ChatOpenAI(model="gpt-4o").bind_tools(mcp_tools) # 4. Invoke LLM and let it act autonomously based on user question response = await agent.ainvoke("What is the weather in Tokyo today?") print(response)

Completion

Congratulations, you've completed the beginner MCP track! You learned the theoretical architecture of the Model Context Protocol, implemented a functional local server, and hooked it into high-level LLM clients.

Continue Learning
Ready for more? Continue to the Intermediate track and explore the MCP wire protocol in detail.

Next Lesson

Protocol Deep Dive