Integrating mcp.run Servlets with CrewAI
CrewAI is a framework that brings structure and organization to multi-agent AI systems. It lets you create teams of AI agents that work together in a coordinated way, similar to how human teams operate in organizations. Each agent has specific roles and responsibilities, working together through defined processes to accomplish complex tasks.
The framework is built on four key components:
- The Crew - Your management layer that oversees operations and ensures effective collaboration
- AI Agents - Specialized team members with defined roles, tools, and objectives
- Tasks - Well-defined assignments that can be chained together
- Process - The workflow manager that controls task execution and agent collaboration
In this tutorial, we'll build a system that manages zoo operations and social media presence using CrewAI and mcp.run tools. We'll create two agents: a zookeeper who writes engaging stories about the animals, and a social media manager who publishes these stories to WordPress.
Step 1: Pre-requisites
Before starting, ensure you have the following:
- Python, CrewAI, and uv installed on your system
- An OpenAI Account with API access
- An OpenAI API Key
- A GitHub Account for mcp.run authentication
- A WordPress blog. You can use a free WordPress site hosted on WordPress.com
Required Tools
Because our hypothetical Zoo uses WordPress for its website, you’ll need to install the WordPress servlet here: https://www.mcp.run/mhmd-azeez/wordpress
Install the servlet by:
- Visiting each URL
- Clicking the
Install
button - Verifying they appear in your install profile
Step 2: Create a new Crew
The CrewAI docs has a fantastic quick start page which guides you through the steps, but creating a new crew is as simple as running:
crewai create crew awesome_zoo_blogging
# and then
uv add crewai-tools --extra mcp
Step 3: Generate an SSE URL for your profile
- Go to your profiles page
- Click on the "SSE" button on the profile you installed the wordpress servlet to
- Click on "Other Client"
- Click on "Generate URL"
- Copy the SSE URL by clicking on the "Copy" button
Step 4: Provide environment variables
You'll need to provide the SSE URL you generated and also your OpenAI API key in the .env
file, it should look like this:
MODEL=gpt-4o
OPENAI_API_KEY=
# 1. Go to https://www.mcp.run/settings/~/profiles/
# 2. Click on the "SSE" button on a profile
# 3. Go to "Other Client" tab
# 4. Copy the SSE Client URL
MCP_RUN_SSE=
Step 5: Defining Our Agents
Create a config/agents.yaml
file:
zookeeper:
role: Zookeeper
goal: Manage the daily operations of the zoo
backstory: >
You are a seasoned zookeeper with a passion for wildlife conservation. You have
experience caring for a wide range of animals, from large mammals to exotic birds.
Your responsibilities include feeding, cleaning, and monitoring the health of the
animals under your care. You also interact with visitors, providing educational
information about the animals and their habitats.
social_media_manager:
role: Social Media Manager
goal: Increase engagement and brand awareness on social media platforms
backstory: >
You are a social media guru with a knack for creating engaging content that
resonates with your audience. You have experience managing social media accounts
for various brands and organizations, using data-driven strategies to optimize
reach and engagement. Your goal is to grow the zoo's online presence and foster
a sense of community among its followers. You also can publish directly to WordPress.
Step 6: Defining Tasks
Create a config/tasks.yaml
file:
write_interesting_stories_task:
description: >
Write an engaging zoo update that captures the week's most fascinating stories
and memorable moments. Share stand-out animal moments that show off their unique
personalities. Include visitor interactions, keeper stories, and facility updates.
Focus on interesting details that would surprise and delight readers. Keep the tone
friendly and conversational. Aim for 250 words.
expected_output: 5 engaging stories/updates that summarize the day.
agent: zookeeper
publish_blog_posts_task:
description: >
Publish the stories as one blog post to the zoo's WordPress site. Check recent
posts to avoid content duplication. Set status to "published" directly.
expected_output: A live blog post series showcasing the zoo's animals.
agent: social_media_manager
Step 7: Creating the Crew
Create your crew.py
:
from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
from crewai.agents.agent_builder.base_agent import BaseAgent
from crewai_tools import MCPServerAdapter
import os
from typing import List
@CrewBase
class AwesomeZooBlogging():
agents: List[BaseAgent]
tasks: List[Task]
def __init__(self):
super().__init__()
self.mcp_server_adapter = None
self._setup_mcp_adapter()
def _setup_mcp_adapter(self):
mcp_run_sse = os.environ.get("MCP_RUN_SSE")
if not mcp_run_sse:
raise ValueError("ENVIRONMENT VARIABLE MCP_RUN_SSE IS NOT DEFINED! AAAAAHHHHH!")
serverparams = {"url": mcp_run_sse}
self.mcp_server_adapter = MCPServerAdapter(serverparams)
@agent
def zookeeper(self) -> Agent:
return Agent(
config=self.agents_config['zookeeper'],
verbose=True,
tools=self.mcp_server_adapter.tools
)
@agent
def social_media_manager(self) -> Agent:
return Agent(
config=self.agents_config['social_media_manager'],
verbose=True
)
@task
def write_interesting_stories_task(self) -> Task:
return Task(
config=self.tasks_config['write_interesting_stories_task'],
)
@task
def publish_blog_posts_task(self) -> Task:
return Task(
config=self.tasks_config['publish_blog_posts_task'],
)
@crew
def crew(self) -> Crew:
return Crew(
agents=self.agents,
tasks=self.tasks,
process=Process.sequential,
verbose=True,
)
def __del__(self):
if self.mcp_server_adapter:
try:
self.mcp_server_adapter.stop()
except:
pass
You can find the complete code for this example at: mhmd-azeez/crewai-mcprun
Step 8: Run the crew
$ crewai run
This should run the two agents that each process their own tasks, in the end, you should have a new blog post in your WordPress website.
Support
If you get stuck and need some help, please reach out! Visit our support page to learn how best to get in touch.