> For the complete documentation index, see [llms.txt](https://docs.talordata.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.talordata.com/serp-api/integration/mcp-integration/langchain-integration.md).

# LangChain Integration

Connect LangChain and LangGraph agents to the TalorData MCP server to add real-time web search, scraping, and structured data tools to your AI workflows.

### Hosted MCP

{% stepper %}
{% step %}

#### **Get your API token**

* Go to [TalorData SERP API - API Token](https://dashboard.talordata.com/scraping/serp-api/api-token) to create an API token;
* Copy your API token (format as follows: `sk_7qBRe***************************`）
  {% endstep %}

{% step %}

#### **Install the required software packages**

```
pip install langchain-mcp-adapters
```

{% endstep %}

{% step %}

#### **Configure your MCP server**

```
import asyncio
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent
from langchain_mcp_adapters.client import MultiServerMCPClient
from dotenv import load_dotenv
import os

load_dotenv()

async def main():
    # Configure MCP client
    client = MultiServerMCPClient({
        "talor_data": {
            "url": "https://mcp.talordata.net/<API_TOKEN>/mcp",
            "transport": "http",
        }
    })

    # Get available tools
    tools = await client.get_tools()
    print("Available tools:", [tool.name for tool in tools])

    # Configure LLM
    llm = ChatOpenAI(
        openai_api_key=os.getenv("OPENROUTER_API_KEY"),
        openai_api_base="https://openrouter.ai/api/v1",
        model_name="moonshotai/kimi-k2"
    )

    # System prompt for web search agent
    system_prompt = """
    You are a web search agent with comprehensive scraping capabilities. Your tools include:
    - **search_engine**: Get search results from Google/Bing/Yandex
    - **scrape_as_markdown**: Extract content from any webpage with bot detection bypass
    - **Structured extractors**: Fast, reliable data from major platforms (Amazon, LinkedIn, Instagram, Facebook, X, TikTok, YouTube, Reddit, Zillow, etc.)
    - **Browser automation**: Navigate, click, type, screenshot for complex interactions

    Guidelines:
    - Use structured web_data_* tools for supported platforms when possible (faster/more reliable)
    - Use general scraping for other sites
    - Handle errors gracefully and respect rate limits
    - Think step by step about what information you need and which tools to use
    - Be thorough in your research and provide comprehensive answers

    When responding, follow this pattern:
    1. Think about what information is needed
    2. Choose the appropriate tool(s)
    3. Execute the tool(s)
    4. Analyze the results
    5. Provide a clear, comprehensive answer
    """

    # Create ReAct agent
    agent = create_agent(
        model=llm,
        tools=tools,
        system_prompt=system_prompt
    )

    # Test the agent
    print("Testing ReAct Agent with available tools...")
    print("=" * 50)

    result = await agent.ainvoke({
        "messages": [("human", "Search for the latest news about AI developments")]
    })

    print("\nAgent Response:")
    print(result["messages"][-1].content)

if __name__ == "__main__":
    asyncio.run(main())
```

{% endstep %}

{% step %}

#### **Set environment variables**

Create a `.env` file in your project directory:

```
OPENROUTER_API_KEY=your_openrouter_api_key_here
```

{% endstep %}

{% step %}

#### **Testing to see if it works**

* Please replace <kbd>\<API\_TOKEN></kbd> with your actual TalorData API token.
* Run your LangChain script
* You should see the agent perform a web search and provide a comprehensive response.
  {% endstep %}

{% step %}

#### **Monitor usage**

* View your API usage in [TalorData SERP API – Usage Records](https://dashboard.talordata.com/scraping/serp-api/history).
  {% endstep %}
  {% endstepper %}

For further assistance, please contact us via **online customer support** or email **<support@talordata.com>**.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.talordata.com/serp-api/integration/mcp-integration/langchain-integration.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
