← Back to Docs
LangChain Integration
Add CAPTCHA solving to any LangChain agent. Use GateSolve as a custom tool or via MCP.
pip install gatesolve+pip install langchain
Method 1: Custom Tool (Recommended)
Wrap GateSolve as a LangChain tool. The agent can call it whenever it encounters a CAPTCHA.
Define the tool
python
from langchain_core.tools import tool
from gatesolve import GateSolve
gs = GateSolve(api_key="gs_your_key")
@tool
def solve_captcha(captcha_type: str, site_key: str, page_url: str) -> str:
"""Solve a CAPTCHA and return the solution token.
Args:
captcha_type: Type of CAPTCHA (turnstile, recaptcha_v2, recaptcha_v3, hcaptcha)
site_key: The site key from the CAPTCHA element
page_url: URL of the page containing the CAPTCHA
"""
solution = gs.solve(
captcha_type=captcha_type,
sitekey=site_key,
pageurl=page_url,
)
return solution.tokenUse it in an Agent
Create agent with CAPTCHA tool
python
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
# Create the agent with the CAPTCHA solving tool
llm = ChatOpenAI(model="gpt-4o")
prompt = ChatPromptTemplate.from_messages([
("system", "You are a web automation agent. When you encounter a CAPTCHA, "
"use the solve_captcha tool to get a token, then inject it into the page."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
agent = create_tool_calling_agent(llm, [solve_captcha], prompt)
executor = AgentExecutor(agent=agent, tools=[solve_captcha], verbose=True)
# The agent can now solve CAPTCHAs autonomously
result = executor.invoke({
"input": "Go to https://example.com/login, solve any CAPTCHAs, and log in."
})Method 2: Async Tool
For async LangChain workflows, use the async client. Solves run concurrently without blocking your agent.
python
from langchain_core.tools import tool
from gatesolve import AsyncGateSolve
gs = AsyncGateSolve(api_key="gs_your_key")
@tool
async def solve_captcha_async(captcha_type: str, site_key: str, page_url: str) -> str:
"""Solve a CAPTCHA asynchronously and return the solution token."""
solution = await gs.solve(
captcha_type=captcha_type,
sitekey=site_key,
pageurl=page_url,
)
return solution.tokenMethod 3: Submit + Poll (Non-blocking)
For long-running workflows, submit the CAPTCHA and poll later. Optionally add a webhook URL for push delivery.
python
from langchain_core.tools import tool
from gatesolve import GateSolve
gs = GateSolve(api_key="gs_your_key")
@tool
def submit_captcha(captcha_type: str, site_key: str, page_url: str) -> str:
"""Submit a CAPTCHA for solving without blocking. Returns a job ID.
Use poll_captcha to check the result."""
job_id = gs.submit(
captcha_type=captcha_type,
sitekey=site_key,
pageurl=page_url,
callback_url="https://your-server.com/webhook/captcha",
)
return f"Submitted. Job ID: {job_id}"
@tool
def poll_captcha(job_id: str) -> str:
"""Check the status of a CAPTCHA solve job."""
result = gs.poll(job_id)
if result["status"] == "solved":
return f"Solved! Token: {result['token']}"
return f"Status: {result['status']}. Try again in a few seconds."Method 4: MCP Server
If your LangChain setup supports MCP, add GateSolve as an MCP server. The agent discovers the solve tool automatically.
json
# Add GateSolve MCP server to your LangChain MCP config
# No Python code needed — just configure the MCP server
# In your MCP config (e.g., mcp_config.json):
{
"servers": {
"gatesolve": {
"command": "npx",
"args": ["@gatesolve/mcp-server"],
"env": {
"GATESOLVE_API_KEY": "gs_your_key"
}
}
}
}
# LangChain agents using MCP will automatically discover
# the solve_captcha tool from the GateSolve MCP serverSupported CAPTCHAs
Cloudflare Turnstile
turnstile · 8-12sreCAPTCHA v2
recaptcha_v2 · 10-15sreCAPTCHA v3
recaptcha_v3 · 8-12shCaptcha
hcaptcha · 10-15s