Converting Existing LlamaIndex Workflows & Tools to MCP
Convert your LlamaIndex tools and workflows into MCP servers for broader ecosystem compatibility.
Converting Workflows
Section titled “Converting Workflows”Use workflow_as_mcp
to convert any LlamaIndex Workflow into an FastMCP server:
from workflows import Context, Workflow, stepfrom workflows.events import StartEvent, StopEventfrom llama_index.tools.mcp.utils import workflow_as_mcp
class QueryEvent(StartEvent): query: str
class SimpleWorkflow(Workflow): @step def process_query(self, ctx: Context, ev: QueryEvent) -> StopEvent: result = f"Processed: {ev.query}" return StopEvent(result=result)
# Convert to MCP serverworkflow = SimpleWorkflow()mcp = workflow_as_mcp(workflow, start_event_model=QueryEvent)
If you were using FastMCP directly, it would look something like this:
from fastmcp import FastMCP
# Workflow definition...
mcp = FastMCP("Demo 🚀")workflow = SimpleWorkflow()
@mcp.toolasync def run_my_workflow(input_args: QueryEvent) -> str: """Add two numbers""" if isintance(input_args, dict): input_args = QueryEvent.model_validate(input_args) result = await workflow.run(start_event=input_args) return str(result)
if __name__ == "__main__": mcp.run()
Converting Individual Tools
Section titled “Converting Individual Tools”We can also use FastMCP to directly convert existing functions and tools input MCP endpoints:
from fastmcp import FastMCPfrom llama_index.tools.notion import NotionToolSpec
# Get tools from ToolSpectool_spec = NotionToolSpec(integration_token="your_token")tools = tool_spec.to_tool_list()
# Create MCP servermcp_server = FastMCP("Tool Server")
# Register toolsfor tool in tools: mcp_server.tool( name=tool.metadata.name, description=tool.metadata.description )(tool.real_fn)
Running MCP Server
Section titled “Running MCP Server”You can launch your server from the CLI (which is also great for debugging!):
# Install MCP CLIpip install "mcp[cli]"
# Run servermcp run your-server.py