Custom MCP Client

Custom MCP Client logo

Build your own MCP client to integrate Scrapfly into custom applications. Complete guide for developers creating native MCP integrations with the Model Context Protocol SDK.

Developer Guide Python JavaScript TypeScript Any Language Official Website

Prerequisites

Before getting started, make sure you have the following:

  • Programming experience in Python, JavaScript, or TypeScript
  • Understanding of async/await and HTTP protocols
  • Node.js 18+ or Python 3.8+ installed
  • Your Scrapfly API key (only if not using OAuth2)

Overview

The Model Context Protocol (MCP) is a standard for connecting AI applications to external data sources and tools. This guide shows you how to build a custom MCP client that connects to the Scrapfly MCP server.

What You'll Build: An MCP client application that connects to https://mcp.scrapfly.io/mcp and can call Scrapfly's web scraping tools programmatically.

Setup Instructions

  1. Install MCP SDK

    Install the official Model Context Protocol SDK for your language:

    Python:

    JavaScript/TypeScript:

    Tip: MCP Protocol Specification

    For advanced use cases, review the official MCP specification:

    https://modelcontextprotocol.io/introduction

  2. Create Basic MCP Client (Python)

    Build a simple MCP client that connects to Scrapfly MCP server:

    Important: The pow parameter is required for scraping. Call scraping_instruction_enhanced first to get it, or use OAuth2 which handles this automatically.
  3. Create Basic MCP Client (JavaScript/TypeScript)

    Build an MCP client in JavaScript or TypeScript:

  4. Implement OAuth2 Authentication

    For production applications, use OAuth2 instead of API keys:

    Python OAuth2 Flow

    OAuth2 Benefits:
    • No API keys in code or config files
    • Automatic token refresh
    • Better security and audit trail
    • User-specific authentication
  5. Build AI Agent with MCP Tools

    Integrate Scrapfly MCP tools with an AI agent using Anthropic SDK:

    Pro Tip: This agent will automatically call scraping_instruction_enhanced first to get required parameters!
  6. Error Handling and Best Practices

    Implement robust error handling for production applications:

Example Prompts

Custom Research Assistant
Build a command-line tool that scrapes web pages and answers questions using Claude
Internal Automation Platform
Create an internal tool that integrates web scraping into your company's workflows
Data Collection Pipeline
Build a scheduled job that scrapes data and stores it in your database
Custom AI Agent
Develop a specialized AI agent with web scraping capabilities for your use case

Troubleshooting

Problem: Cannot connect to Scrapfly MCP server

Solution:

  • Verify npx is available: npx --version
  • Check internet connectivity to https://mcp.scrapfly.io/mcp
  • Ensure Node.js 18+ is installed
  • Try running npx mcp-remote https://mcp.scrapfly.io/mcp manually

Problem: Tool calls return errors or fail

Solution:

  • Ensure you call scraping_instruction_enhanced first to get pow parameter
  • Check tool arguments match expected schema
  • Verify API key or OAuth2 authentication is working
  • Review error messages in tool call results

Problem: OAuth2 authorization does not complete

Solution:

  • Detect OAuth2 URL in initial connection response
  • Open URL in browser automatically or prompt user
  • Wait for OAuth2 completion before making tool calls
  • For headless environments, use API key authentication instead

Problem: MCP session times out or disconnects

Solution:

  • Implement connection retry logic
  • Keep session alive with periodic health checks
  • Reconnect automatically on disconnect
  • Increase timeout values for long-running operations

Problem: Tool schema or argument type errors

Solution:

  • Use list_tools() to get exact schema for each tool
  • Validate arguments against schema before calling
  • Check parameter types (string, boolean, integer, etc.)
  • Review MCP protocol specification for correct formats

Problem: Slow tool calls or high latency

Solution:

  • Implement caching for frequently scraped URLs
  • Use async/await properly to avoid blocking
  • Batch similar requests when possible
  • Monitor network latency and optimize connection settings

Next Steps

Summary