mirror of
https://github.com/mcp-use/mcp-use.git
synced 2026-09-14 18:06:01 +08:00
8348c59a53
* docs: reorganize navigation and clean up content - Remove 'Misc' group with misplaced blog posts - Remove 'Libraries' redirect group from Home nav - Simplify 'Dev Tools' group (flatten Inspector, CLI, Tunneling) - Remove individual changelog versions from Python nav (keep summary) - Remove changelog from TypeScript 'Get Started' group - Remove tunneling duplicate from Python Development group - Rewrite mcp101 as clean MCP concepts page (was pitch deck) - Delete typescript/blog posts (not technical docs) - Add redirects for deleted blog post URLs * docs: fix content accuracy and remove sloppy language - Fix broken python/server/mcp-apps link in home page - Remove informal pitch-speak from python/server/index.mdx - Fix grammar error in typescript/server/index.mdx - Update typescript/client/logging.mdx: remove fake 'coming soon' API code - Fix non-existent model name gpt-5.1 in typescript quickstart - Fix mcp_use naming in development contributing guide * docs: fix incorrect TypeScript server API calls in inspector docs - Replace server.addTool() with server.tool() (correct mcp-use API) - Add missing zod import to debugging-chatgpt-apps example * docs: fix fake model names (gpt-5.1 → gpt-4o) in agent docs * docs: update model names, fix APIs, and add agent visibility hints - Replace all deprecated model names with latest: gpt-4.1, gpt-4.1-mini, claude-sonnet-4-6, claude-opus-4-7, gemini-2.5-flash across all docs - Fix TypeScript server.tool() API in installation.mdx (old positional string form) - Fix TypeScript agent.run() in quickstart.mdx (deprecated string form → object) - Remove broken manufact.com/docs/llms.txt hidden hints from 5 pages - Add <Visibility for="agents"> block to mcp101.mdx with install commands, key classes, and API signatures for agent-oriented navigation - Add LangChain provider install note to Python quickstart (mcp-use agent requires a separate langchain-* provider package) - Update model name cards in Python llm-integration.mdx * docs: clean up remaining stale model names and improve accuracy - Update supported model lists in Python and TypeScript llm-integration pages - Fix cost optimization table to use current model IDs - Update TypeScript agent overview supported providers list * docs: add LangChain provider install to TypeScript quickstart agent section The MCPAgent requires a separate @langchain/* provider package; this was not mentioned before the code example that imports from @langchain/openai. * docs: update to latest flagship model IDs (May 2026) - OpenAI: gpt-4.1 → gpt-5.5, gpt-4.1-mini → gpt-5.5-mini - Google: gemini-2.5-flash → gemini-3.1-flash, gemini-2.5-pro → gemini-3.1-pro - Groq: llama-3.1-* → llama-4-scout-17b-16e-instruct - Anthropic (claude-opus-4-7, claude-sonnet-4-6) already current, no change * docs: replace all em dashes with hyphens * docs: restore llms.txt hints using Visibility component * docs: simplify TypeScript logging page to unsupported notice only * docs: rewrite mcp101 to explain MCP protocol with mcp-use examples and doc links * docs: remove goal.md * docs: revert nav changes, keep only Misc group removal and blog redirects * docs: reorder mcp101 to Server/App -> Client/Inspector -> Agent, restore title * docs: add code examples for Resources and Prompts in mcp101 * docs: remove em dashes from mcp101 * docs: add MCPClient code examples to mcp101 * docs: add React client example to mcp101 * docs: add agent code examples to mcp101 * docs: expand MCP Apps section in mcp101 with example and links * docs: restore Contacts, Socials, and Founders sections to mcp101 * docs: add context-setting intro paragraph to mcp101 for new readers * docs: add MCP stores section to mcp101 intro with Claude, ChatGPT, Cursor * docs: fix Claude store link * docs: fix ChatGPT store link * docs: fix Cursor store link * docs: replace store Cards with linked image grid * docs: add captions to store images * docs: add Notifications to client primitives table in mcp101 * docs: add lead-in paragraph before resource cards at bottom of mcp101 * docs: move resource cards to end of page after Contacts/Socials/Founders * docs: demote Contacts/Socials/Founders to bold text to remove from TOC * docs: move Contacts/Socials/Founders to very end of mcp101 * docs: add Resources and Contacts section headers, link official MCP docs * chore(docs): update site metadata, footer links, and rename mcp_use to mcp-use * update docs metadata + images * docs: enhance MCP section by highlighting the reach of MCP stores with 1 billion weekly active users * api ref
309 lines
7.3 KiB
Plaintext
309 lines
7.3 KiB
Plaintext
---
|
|
title: "LLM Integration"
|
|
description: "Integrate any LLM with mcp_use through LangChain"
|
|
icon: "brain"
|
|
---
|
|
|
|
# LLM Integration Guide
|
|
|
|
mcp_use supports integration with **any** Language Learning Model (LLM) that is compatible with LangChain. This guide covers how to use different LLM providers with mcp_use and emphasizes the flexibility to use any LangChain-supported model.
|
|
|
|
<Note>
|
|
**Key Requirement**: Your chosen LLM must support **tool calling** (also known as function calling) to work with MCP tools. Most modern LLMs support this feature.
|
|
</Note>
|
|
|
|
## Universal LLM Support
|
|
|
|
mcp_use leverages LangChain's architecture to support any LLM that implements the LangChain interface. This means you can use virtually any model from any provider, including:
|
|
|
|
<CardGroup cols={2}>
|
|
<Card title="OpenAI" icon="robot">
|
|
GPT-4.1, GPT-4.1-mini
|
|
</Card>
|
|
<Card title="Anthropic" icon="anthropic">
|
|
claude-opus-4-7, claude-sonnet-4-6
|
|
</Card>
|
|
<Card title="Google" icon="google">
|
|
gemini-3.1-pro, gemini-3.1-flash
|
|
</Card>
|
|
<Card title="Open Source" icon="code">
|
|
Llama, Mistral, CodeLlama via various providers
|
|
</Card>
|
|
</CardGroup>
|
|
|
|
## Popular Provider Examples
|
|
|
|
### OpenAI
|
|
|
|
```python
|
|
from langchain_openai import ChatOpenAI
|
|
from mcp_use import MCPAgent, MCPClient
|
|
|
|
# Initialize OpenAI model
|
|
llm = ChatOpenAI(
|
|
model="gpt-5.5",
|
|
temperature=0.7,
|
|
api_key="your-api-key" # Or set OPENAI_API_KEY env var
|
|
)
|
|
|
|
# Create agent
|
|
agent = MCPAgent(llm=llm, client=client)
|
|
```
|
|
|
|
### Anthropic Claude
|
|
|
|
```python
|
|
from langchain_anthropic import ChatAnthropic
|
|
from mcp_use import MCPAgent, MCPClient
|
|
|
|
# Initialize Claude model
|
|
llm = ChatAnthropic(
|
|
model="claude-sonnet-4-6",
|
|
temperature=0.7,
|
|
api_key="your-api-key" # Or set ANTHROPIC_API_KEY env var
|
|
)
|
|
|
|
# Create agent
|
|
agent = MCPAgent(llm=llm, client=client)
|
|
```
|
|
|
|
### Google Gemini
|
|
|
|
```python
|
|
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
from mcp_use import MCPAgent, MCPClient
|
|
|
|
# Initialize Gemini model
|
|
llm = ChatGoogleGenerativeAI(
|
|
model="gemini-3.1-flash",
|
|
temperature=0.7,
|
|
google_api_key="your-api-key" # Or set GOOGLE_API_KEY env var
|
|
)
|
|
|
|
# Create agent
|
|
agent = MCPAgent(llm=llm, client=client)
|
|
```
|
|
|
|
### Groq (Fast Inference)
|
|
|
|
```python
|
|
from langchain_groq import ChatGroq
|
|
from mcp_use import MCPAgent, MCPClient
|
|
|
|
# Initialize Groq model
|
|
llm = ChatGroq(
|
|
model="llama-4-scout-17b-16e-instruct",
|
|
temperature=0.7,
|
|
api_key="your-api-key" # Or set GROQ_API_KEY env var
|
|
)
|
|
|
|
# Create agent
|
|
agent = MCPAgent(llm=llm, client=client)
|
|
```
|
|
|
|
### Local Models with Ollama
|
|
|
|
```python
|
|
from langchain_ollama import ChatOllama
|
|
from mcp_use import MCPAgent, MCPClient
|
|
|
|
# Initialize local Ollama model
|
|
llm = ChatOllama(
|
|
model="llama3.1:8b",
|
|
base_url="http://localhost:11434", # Default Ollama URL
|
|
temperature=0.7
|
|
)
|
|
|
|
# Create agent
|
|
agent = MCPAgent(llm=llm, client=client)
|
|
```
|
|
|
|
## Model Requirements
|
|
|
|
### Tool Calling Support
|
|
|
|
For MCP tools to work properly, your chosen model **must support tool calling**. Most modern LLMs support this:
|
|
|
|
✅ **Supported Models:**
|
|
- OpenAI: gpt-5.5, gpt-5.5-mini
|
|
- Anthropic: claude-opus-4-7, claude-sonnet-4-6
|
|
- Google: gemini-3.1-pro, gemini-3.1-flash
|
|
- Groq: Llama 3.1, Mixtral models
|
|
- Most recent open-source models
|
|
|
|
❌ **Not Supported:**
|
|
- Basic completion models without tool calling
|
|
- Very old model versions
|
|
- Models without function calling capabilities
|
|
|
|
### Checking Tool Support
|
|
|
|
You can verify if a model supports tools:
|
|
|
|
```python
|
|
# Check if the model supports tool calling
|
|
if hasattr(llm, 'bind_tools') or hasattr(llm, 'with_tools'):
|
|
print("✅ Model supports tool calling")
|
|
else:
|
|
print("❌ Model may not support tool calling")
|
|
```
|
|
|
|
## Model Configuration Tips
|
|
|
|
### Temperature Settings
|
|
|
|
Different tasks benefit from different temperature settings:
|
|
|
|
```python
|
|
# For precise, deterministic tasks
|
|
llm = ChatOpenAI(model="gpt-5.5", temperature=0.1)
|
|
|
|
# For creative tasks
|
|
llm = ChatOpenAI(model="gpt-5.5", temperature=0.8)
|
|
|
|
# Balanced approach (recommended)
|
|
llm = ChatOpenAI(model="gpt-5.5", temperature=0.7)
|
|
```
|
|
|
|
### Model-Specific Parameters
|
|
|
|
Each provider has unique parameters you can configure:
|
|
|
|
```python
|
|
# OpenAI with custom parameters
|
|
llm = ChatOpenAI(
|
|
model="gpt-5.5",
|
|
temperature=0.7,
|
|
max_tokens=4000,
|
|
top_p=0.9,
|
|
frequency_penalty=0.1,
|
|
presence_penalty=0.1
|
|
)
|
|
|
|
# Anthropic with custom parameters
|
|
llm = ChatAnthropic(
|
|
model="claude-sonnet-4-6",
|
|
temperature=0.7,
|
|
max_tokens=4000,
|
|
top_p=0.9
|
|
)
|
|
```
|
|
|
|
## Cost Optimization
|
|
|
|
### Choosing Cost-Effective Models
|
|
|
|
Consider your use case when selecting models:
|
|
|
|
| Use Case | Recommended Models | Reason |
|
|
|----------|-------------------|--------|
|
|
| Development/Testing | gpt-5.5-mini, claude-sonnet-4-6 | Lower cost, good performance |
|
|
| Production/Complex | gpt-5.5, claude-opus-4-7 | Best performance |
|
|
| High Volume | Groq models | Fast inference, competitive pricing |
|
|
| Privacy/Local | Ollama models | No API costs, data stays local |
|
|
|
|
### Token Management
|
|
|
|
```python
|
|
# Set reasonable token limits
|
|
llm = ChatOpenAI(
|
|
model="gpt-5.5",
|
|
max_tokens=2000, # Limit response length
|
|
temperature=0.7
|
|
)
|
|
|
|
# Monitor usage in your application
|
|
agent = MCPAgent(
|
|
llm=llm,
|
|
client=client,
|
|
max_steps=10 # Limit agent steps to control costs
|
|
)
|
|
```
|
|
|
|
## Environment Setup
|
|
|
|
Always use environment variables for API keys:
|
|
|
|
```bash
|
|
# .env file
|
|
OPENAI_API_KEY=sk-...
|
|
ANTHROPIC_API_KEY=sk-ant-...
|
|
GOOGLE_API_KEY=AI...
|
|
GROQ_API_KEY=gsk_...
|
|
```
|
|
|
|
```python
|
|
from dotenv import load_dotenv
|
|
load_dotenv() # Load environment variables
|
|
|
|
# Now LangChain will automatically use the keys
|
|
llm = ChatOpenAI(model="gpt-5.5") # No need to pass api_key
|
|
```
|
|
|
|
## Advanced Integration
|
|
|
|
### Custom Model Wrappers
|
|
|
|
You can create custom wrappers for specialized models:
|
|
|
|
```python
|
|
from langchain_core.language_models import BaseChatModel
|
|
from langchain_core.messages import BaseMessage
|
|
|
|
class CustomModelWrapper(BaseChatModel):
|
|
"""Custom wrapper for your model"""
|
|
|
|
def _generate(self, messages, stop=None, **kwargs):
|
|
# Your custom model implementation
|
|
pass
|
|
|
|
def _llm_type(self):
|
|
return "custom_model"
|
|
|
|
# Use with mcp_use
|
|
llm = CustomModelWrapper()
|
|
agent = MCPAgent(llm=llm, client=client)
|
|
```
|
|
|
|
### Model Switching
|
|
|
|
Switch between models dynamically:
|
|
|
|
```python
|
|
def get_model_for_task(task_type: str):
|
|
if task_type == "complex_reasoning":
|
|
return ChatOpenAI(model="gpt-5.5", temperature=0.1)
|
|
elif task_type == "creative":
|
|
return ChatAnthropic(model="claude-sonnet-4-6", temperature=0.8)
|
|
else:
|
|
return ChatOpenAI(model="gpt-5.5-mini", temperature=0.7)
|
|
|
|
# Use different models for different tasks
|
|
llm = get_model_for_task("complex_reasoning")
|
|
agent = MCPAgent(llm=llm, client=client)
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
### Common Issues
|
|
|
|
1. **"Model doesn't support tools"**: Ensure your model supports function calling
|
|
2. **API key errors**: Check environment variables and API key validity
|
|
3. **Rate limiting**: Implement retry logic or use different models
|
|
4. **Token limits**: Adjust max_tokens or use models with larger context windows
|
|
|
|
### Debug Model Behavior
|
|
|
|
<CodeGroup>
|
|
```python Python
|
|
# Enable verbose logging to see model interactions
|
|
agent = MCPAgent(
|
|
llm=llm,
|
|
client=client,
|
|
verbose=True # Shows detailed model interactions
|
|
)
|
|
```
|
|
</CodeGroup>
|
|
|
|
For more LLM providers and detailed integration examples, visit the [LangChain Chat Models documentation](https://python.langchain.com/docs/integrations/chat/).
|