|
| 1 | +--- |
| 2 | +title: "MCP STDIO Integration" |
| 3 | +sidebarTitle: "MCP STDIO" |
| 4 | +description: "Guide for integrating Standard Input/Output (STDIO) with PraisonAI agents using MCP" |
| 5 | +icon: "terminal" |
| 6 | +--- |
| 7 | + |
| 8 | +## Add STDIO Tool to AI Agent |
| 9 | + |
| 10 | +```mermaid |
| 11 | +flowchart LR |
| 12 | + In[In] --> Agent[AI Agent] |
| 13 | + Agent --> Tool[STDIO MCP] |
| 14 | + Tool --> Agent |
| 15 | + Agent --> Out[Out] |
| 16 | + |
| 17 | + style In fill:#8B0000,color:#fff |
| 18 | + style Agent fill:#2E8B57,color:#fff |
| 19 | + style Tool fill:#000000,color:#fff |
| 20 | + style Out fill:#8B0000,color:#fff |
| 21 | +``` |
| 22 | + |
| 23 | +## Quick Start |
| 24 | + |
| 25 | +<Steps> |
| 26 | + <Step title="Create a client file"> |
| 27 | + Create a new file `calculator_client.py` with the following code: |
| 28 | + ```python |
| 29 | + from praisonaiagents import Agent, MCP |
| 30 | + |
| 31 | + calculator_agent = Agent( |
| 32 | + instructions="""You are a calculator agent that can perform basic arithmetic operations.""", |
| 33 | + llm="gpt-4o-mini", |
| 34 | + tools=MCP("python calculator_server.py") |
| 35 | + ) |
| 36 | + |
| 37 | + calculator_agent.start("What is 25 * 16?") |
| 38 | + ``` |
| 39 | + </Step> |
| 40 | + |
| 41 | + <Step title="Set Up STDIO MCP Server"> |
| 42 | + Create a file `calculator_server.py` with the following code: |
| 43 | + ```python |
| 44 | + # calculator_server.py |
| 45 | + from mcp.server.fastmcp import FastMCP |
| 46 | + import logging |
| 47 | + import sys |
| 48 | + |
| 49 | + # Set up logging |
| 50 | + logging.basicConfig(level=logging.INFO, filename="calculator_server.log") |
| 51 | + logger = logging.getLogger("calculator-server") |
| 52 | + |
| 53 | + # Initialize FastMCP server for simple tools |
| 54 | + mcp = FastMCP("calculator-tools") |
| 55 | + |
| 56 | + @mcp.tool() |
| 57 | + def add(a: float, b: float) -> float: |
| 58 | + """Add two numbers. |
| 59 | + |
| 60 | + Args: |
| 61 | + a: First number |
| 62 | + b: Second number |
| 63 | + |
| 64 | + Returns: |
| 65 | + The sum of a and b |
| 66 | + """ |
| 67 | + logger.info(f"Adding {a} + {b}") |
| 68 | + return a + b |
| 69 | + |
| 70 | + @mcp.tool() |
| 71 | + def subtract(a: float, b: float) -> float: |
| 72 | + """Subtract b from a. |
| 73 | + |
| 74 | + Args: |
| 75 | + a: First number |
| 76 | + b: Second number |
| 77 | + |
| 78 | + Returns: |
| 79 | + The result of a - b |
| 80 | + """ |
| 81 | + logger.info(f"Subtracting {b} from {a}") |
| 82 | + return a - b |
| 83 | + |
| 84 | + @mcp.tool() |
| 85 | + def multiply(a: float, b: float) -> float: |
| 86 | + """Multiply two numbers. |
| 87 | + |
| 88 | + Args: |
| 89 | + a: First number |
| 90 | + b: Second number |
| 91 | + |
| 92 | + Returns: |
| 93 | + The product of a and b |
| 94 | + """ |
| 95 | + logger.info(f"Multiplying {a} * {b}") |
| 96 | + return a * b |
| 97 | + |
| 98 | + @mcp.tool() |
| 99 | + def divide(a: float, b: float) -> float: |
| 100 | + """Divide a by b. |
| 101 | + |
| 102 | + Args: |
| 103 | + a: First number (numerator) |
| 104 | + b: Second number (denominator) |
| 105 | + |
| 106 | + Returns: |
| 107 | + The result of a / b |
| 108 | + """ |
| 109 | + if b == 0: |
| 110 | + raise ValueError("Cannot divide by zero") |
| 111 | + logger.info(f"Dividing {a} / {b}") |
| 112 | + return a / b |
| 113 | + |
| 114 | + if __name__ == "__main__": |
| 115 | + # Run the MCP server using STDIO transport |
| 116 | + mcp.run() |
| 117 | + ``` |
| 118 | + </Step> |
| 119 | + |
| 120 | + <Step title="Install Dependencies"> |
| 121 | + Make sure you have the required packages installed: |
| 122 | + ```bash |
| 123 | + pip install "praisonaiagents[llm]" mcp |
| 124 | + ``` |
| 125 | + </Step> |
| 126 | + <Step title="Export API Key"> |
| 127 | + ```bash |
| 128 | + export OPENAI_API_KEY="your_api_key" |
| 129 | + ``` |
| 130 | + </Step> |
| 131 | + |
| 132 | + <Step title="Run the Agent"> |
| 133 | + Run the agent which will automatically start the calculator server: |
| 134 | + ```bash |
| 135 | + python calculator_client.py |
| 136 | + ``` |
| 137 | + </Step> |
| 138 | +</Steps> |
| 139 | + |
| 140 | +<Note> |
| 141 | + **Requirements** |
| 142 | + - Python 3.10 or higher |
| 143 | + - MCP package |
| 144 | +</Note> |
| 145 | + |
| 146 | +## Alternative LLM Integrations |
| 147 | + |
| 148 | +### Using Groq with STDIO |
| 149 | + |
| 150 | +```python |
| 151 | +from praisonaiagents import Agent, MCP |
| 152 | + |
| 153 | +calculator_agent = Agent( |
| 154 | + instructions="""You are a calculator agent that can perform basic arithmetic operations.""", |
| 155 | + llm="groq/llama-3.2-90b-vision-preview", |
| 156 | + tools=MCP("python calculator_server.py") |
| 157 | +) |
| 158 | + |
| 159 | +calculator_agent.start("What is 144 divided by 12?") |
| 160 | +``` |
| 161 | + |
| 162 | +### Using Ollama with STDIO |
| 163 | + |
| 164 | +```python |
| 165 | +from praisonaiagents import Agent, MCP |
| 166 | + |
| 167 | +calculator_agent = Agent( |
| 168 | + instructions="""You are a calculator agent that can perform basic arithmetic operations.""", |
| 169 | + llm="ollama/llama3.2", |
| 170 | + tools=MCP("python calculator_server.py") |
| 171 | +) |
| 172 | + |
| 173 | +calculator_agent.start("What is 15 + 27? Use the add tool with parameters a and b.") |
| 174 | +``` |
| 175 | + |
| 176 | +## Gradio UI Integration |
| 177 | + |
| 178 | +Create a Gradio UI for your calculator service: |
| 179 | + |
| 180 | +```python |
| 181 | +from praisonaiagents import Agent, MCP |
| 182 | +import gradio as gr |
| 183 | + |
| 184 | +def calculate(query): |
| 185 | + calculator_agent = Agent( |
| 186 | + instructions="""You are a calculator agent that can perform basic arithmetic operations.""", |
| 187 | + llm="gpt-4o-mini", |
| 188 | + tools=MCP("python calculator_server.py") |
| 189 | + ) |
| 190 | + |
| 191 | + result = calculator_agent.start(query) |
| 192 | + return f"## Calculation Result\n\n{result}" |
| 193 | + |
| 194 | +demo = gr.Interface( |
| 195 | + fn=calculate, |
| 196 | + inputs=gr.Textbox(placeholder="What is 25 * 16?"), |
| 197 | + outputs=gr.Markdown(), |
| 198 | + title="Calculator MCP Agent", |
| 199 | + description="Ask any arithmetic question:" |
| 200 | +) |
| 201 | + |
| 202 | +if __name__ == "__main__": |
| 203 | + demo.launch() |
| 204 | +``` |
| 205 | + |
| 206 | +## Features |
| 207 | + |
| 208 | +<CardGroup cols={2}> |
| 209 | + <Card title="Simple Integration" icon="plug"> |
| 210 | + Use standard input/output for easy integration with any tool or service. |
| 211 | + </Card> |
| 212 | + <Card title="Cross-Platform" icon="laptop"> |
| 213 | + Works on any operating system that supports Python. |
| 214 | + </Card> |
| 215 | + <Card title="Multiple LLM Options" icon="brain"> |
| 216 | + Use with OpenAI, Groq, Ollama, or other supported LLMs. |
| 217 | + </Card> |
| 218 | + <Card title="Gradio UI" icon="window"> |
| 219 | + Create user-friendly interfaces for your STDIO integrations. |
| 220 | + </Card> |
| 221 | +</CardGroup> |
0 commit comments