Skip to main content

How to build an AI agent using Firecrawl and Python

In this guide, we'll walk you through how to build an AI agent using OpenAI and the Firecrawl MCP server

Building AI agents is quickly becoming a de facto standard in software development and other tech-adjacent fields. Whether you’re a developer by trade or simply looking to increase productivity, AI agents can help bring your system to the next level. With Model Context Protocol (MCP), it’s easier than ever to build powerful software with minimal code.

In this guide, we’ll walk you through how to build your own agent using OpenAI and the Firecrawl MCP server. OpenAI gives us access to a vast library of models and the Firecrawl MCP allows these models to use Firecrawl’s tooling in a live environment.

By the time you’re finished with this tutorial, you’ll understand the following concepts.

  • Creating an account with Firecrawl
  • Accessing your Firecrawl API (Application Programming Interface) key
  • Building a basic chat loop using OpenAI
  • Connecting your OpenAI client to an MCP server

Getting started

Setting up Firecrawl

To start, head on over to https://firecrawl.dev and create an account. They offer a variety of plans based on usage but everything from this tutorial is doable using their Free Plan. Once you’ve created your account, you’ll be taken to the Firecrawl dashboard. From here, you can manage your entire Firecrawl account.

Firecrawl dashboard
Firecrawl dashboard

You can copy your API key straight from the front page of the dashboard. If you’d like to add or remove API keys, simply select the API keys tab.

Managing your Firecrawl API keys
Managing your Firecrawl API keys

Once you’ve got your API key, store it in a safe place. This key allows external software to access your Firecrawl account.

Setting up your OpenAI account

Now, you’ll need to create an OpenAI developer account. Go to https://openai.com/api/ to get started and simply create an account. From there, you can use their dashboard. Click on the API keys tab to manage your keys.

OpenAI API keys page
OpenAI API keys page

Once again, create and copy your API key. Store it in a safe place. This API key gives access to your OpenAI account. You do not want it to fall into the wrong hands.

Before we begin coding, we need to install the OpenAI package using pip. This lets you plug any OpenAI model directly from your Python environment.

Install the OpenAI package

pip install openai

Building our AI agent

Now that we’ve got everything required, let’s build our agent. This AI agent is relatively simple. Once it’s finished, it should run using the following steps.

  1. Start the program
  2. Prompt the agent to perform a task
  3. Agent completes the task and outputs a response

Creating the agent

Creating our agent is simple. First, we import OpenAI. Next, we create a client. Our chat_interface() function does all the real heavy lifting. We use several keywords to configure the agent.

  • model: The name of the model you’re using. We used GPT-5 mini. Feel free to choose any model you’d like. Performance and context windows can vary greatly. Their full list of models and pricing can be found here.
  • tools: This is where the real magic happens. In this case, we’re only using one tool. That said, we pass our tool in as a list — this way we can hook our AI model up to as many tools as we want.
  • input: This is the prompt we give to the agent. If we want our agent to find the latest news, we simply tell it to find the latest news.
from openai import OpenAI

client = OpenAI(api_key="your-openai-api-key")

API_TOKEN = "your-firecrawl-api-key"


def chat_interface(prompt: str):
    resp = client.responses.create(
    model="gpt-5-mini",
    tools=[
      {
        "type": "mcp",
        "server_label": "Firecrawl",
        "server_url": f"https://mcp.firecrawl.dev/{API_TOKEN}/v2/mcp",
        "require_approval": "never",
      },
    ],
    input=prompt,)
    return resp.output_text

You should also note our MCP configuration. This is how the agent actually accesses Firecrawl. We use "type": "mcp" to tell the model to call the tool using MCP. "server_label": "Firecrawl" is just a personal configuration — feel free to name it whatever you’d like. The server_url tells the AI agent where to send its requests when calling the tool. We use "require_approval": "never" so the agent can call Firecrawl immediately without asking our permission.

MCP configurations are reusable across languages, architectures and frameworks. You can use this same configuration in no-code workflows or desktop apps such as Claude. MCP lets you build intuitive and interactive AI agents regardless of your programming environment.

Setting a runtime loop

Before we finish, we’ll create a basic runtime loop. We set RUNNING to True when the agent starts. A user enters a prompt and the agent output gets printed to the terminal. If the user inputs exit, RUNNING gets set to false and we exit the program.

RUNNING = True

while RUNNING:
  prompt = input("Input a prompt: ")
  if prompt == "exit":
    RUNNING = False
  else:
    print(chat_interface(prompt))

Putting everything together

Now that we’ve got all the piece, we can put it together and actually use our AI agent. If you need it, we’ve got the full code below. Remember to swap the API keys with your own.

The full code

from openai import OpenAI

client = OpenAI(api_key="your-openai-api-key")

API_TOKEN = "your-firecrawl-api-key"


def chat_interface(prompt: str):
    resp = client.responses.create(
    model="gpt-5-mini",
    tools=[
      {
        "type": "mcp",
        "server_label": "Firecrawl",
        "server_url": f"https://mcp.firecrawl.dev/{API_TOKEN}/v2/mcp",
        "require_approval": "never",
      },
    ],
    input=prompt,)
    return resp.output_text

RUNNING = True

while RUNNING:
  prompt = input("Input a prompt: ")
  if prompt == "exit":
    RUNNING = False
  else:
    print(chat_interface(prompt))

To run the agent simply use python <your-filename>.py. If your file name is firecrawl-agent.py, you’d use python firecrawl-agent.py. When you’re finished, simply type exit to turn off the agent.

Using your new AI agent

Once you’ve started your AI agent, it just needs instructions. In snippet below, we use are you connected to the firecrawl mcp? to double check the MCP connection. Our AI agent responds by telling us what it can do.

Input a prompt: are you connected to the firecrawl mcp?
Yes — in this chat environment I have access to the Firecrawl MCP toolset (search, scrape, map, crawl, extract). I can run web searches, fetch page contents, map site URLs, and extract structured data.

Next, we tell it to get the latest world news: Input a prompt: use firecrawl to get me the latest world news please. The AI agent gave us nine news stories. Then, it output a list of aggregate news sites we can use for further research.

Input a prompt: use firecrawl to get me the latest world news please
I used Firecrawl to pull current headlines. Here are the top world-news stories right now (as of 2025-12-02) with short summaries and links — tell me which ones you want expanded or scraped in full.

- Tens of thousands gather as Pope Leo XIV urges end to “mindset of revenge” in Beirut — large crowd, memorial visit after the 2020 port blast. (NBC) https://www.nbcnews.com/world/lebanon/pope-leo-beirut-mass-tens-of-thousands-rcna246934

- Devastating floods swamp South and Southeast Asia — wide photo coverage of destruction across Indonesia, Sri Lanka, Thailand; heavy losses and mass displacement. (NYT) https://www.nytimes.com/2025/12/02/world/asia/floods-indonesia-sri-lanka-thailand-photos.html

- Ukraine targets Russia’s “shadow fleet” at sea as peace talks advance — Kyiv says it is striking ships thought to support Moscow’s war revenues. (Financial Times) https://www.ft.com/content/3e72db0d-077c-4f48-a4a5-a2b8683dae53

- Bulgarian government withdraws budget proposal after massive nationwide protests — protesters angered at tax moves; PM withdraws the plan amid largest protests in a decade. (POLITICO) https://www.politico.eu/article/bulgarian-government-withdraws-budget-proposal-after-largest-protests-in-a-decade/  

- Indonesia floods and landslides death toll rises (Sumatra) — authorities racing to repair infrastructure and deliver aid; figures updated by disaster agency. (Reuters) https://www.reuters.com/business/environment/death-toll-indonesias-floods-landslide-hits-753-disaster-agency-says-2025-12-02/       

- Hong Kong high-rise blaze kills many, sparking anger over safety and corruption — large death toll reported, arrests in renovation project, calls for accountability. (AP) https://apnews.com/article/hong-wang-china-dissent-fire-construction-ff953aec2bc0201b0e3255805a241fc1

- Sri Lanka cyclone devastation — death toll in the hundreds, hundreds missing; major rescue and relief operations underway. (Reuters) https://www.reuters.com/business/environment/sri-lankas-cyclone-death-toll-climbs-355-with-366-missing-2025-12-01/

- Jamaica secures up to US$6.7bn international package for Hurricane Melissa recovery and reconstruction. (IMF) https://www.imf.org/en/news/articles/2025/12/01/pr25400-jamaica-billions-over-3-years-for-recovery-and-reconstruct-after-hurricane-melissa

- UK political / budget coverage: OBR official says Chancellor Reeves’ Budget messaging “not inconsistent”; Starmer backs Reeves amid claims she misled public. (BBC / ABC) https://www.bbc.com/news/articles/czj0ngnkl2vo  /  https://abcnews.go.com/Business/wireStory/uk-leader-keir-starmer-backs-treasury-chief-claims-127994017

- Aggregators / home pages to browse a broader feed: BBC World, Reuters World, Al Jazeera, CNN, Google News, NYT front pages.
  - BBC World: https://www.bbc.com/news/world
  - Reuters World: https://www.reuters.com/world/
  - Al Jazeera: https://www.aljazeera.com/
  - Google News (world): https://news.google.com/

Would you like me to:
- Open and extract the full text of any specific article?
- Give a short, plain-language summary of one or more items?
- Focus on a specific region or topic (e.g., Ukraine, climate disasters, Hong Kong)?

Finally, we use exit to exit the runtime and turn off our agent.

Input a prompt: exit

Conclusion

Building AI agents with MCP servers is simple and straightforward. We simply need to create a client and pass the MCP server in as one of the tools. In this tutorial, we didn’t add persistent memory to our agent. GPT-5 mini has a context window of 400,000 tokens — more than enough to accomplish a simple demonstration.

In production, developers should always build some type of agent memory. For simpler projects, you can use in-RAM memory or a simple file with read/write access. For more complex agentic architectures, vector databases are a powerful ally.

With MCP, you can build AI agents for virtually any platform easily. Here, we did it using Python. MCP servers aren’t limited to Python. They’re language agnostic. You can plug your AI agent into an MCP server using JavaScript, n8n or any other programming environment.