-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add STDIO MCP documentation and update navigation menu #472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| --- | ||
| title: "MCP STDIO Integration" | ||
| sidebarTitle: "MCP STDIO" | ||
| description: "Guide for integrating Standard Input/Output (STDIO) with PraisonAI agents using MCP" | ||
| icon: "terminal" | ||
| --- | ||
|
|
||
| ## Add STDIO Tool to AI Agent | ||
|
|
||
| ```mermaid | ||
| flowchart LR | ||
| In[In] --> Agent[AI Agent] | ||
| Agent --> Tool[STDIO MCP] | ||
| Tool --> Agent | ||
| Agent --> Out[Out] | ||
|
|
||
| style In fill:#8B0000,color:#fff | ||
| style Agent fill:#2E8B57,color:#fff | ||
| style Tool fill:#000000,color:#fff | ||
| style Out fill:#8B0000,color:#fff | ||
| ``` | ||
|
|
||
| ## Quick Start | ||
|
|
||
| <Steps> | ||
| <Step title="Create a client file"> | ||
| Create a new file `calculator_client.py` with the following code: | ||
| ```python | ||
| from praisonaiagents import Agent, MCP | ||
|
|
||
| calculator_agent = Agent( | ||
| instructions="""You are a calculator agent that can perform basic arithmetic operations.""", | ||
| llm="gpt-4o-mini", | ||
| tools=MCP("python calculator_server.py") | ||
| ) | ||
|
|
||
| calculator_agent.start("What is 25 * 16?") | ||
| ``` | ||
| </Step> | ||
|
|
||
| <Step title="Set Up STDIO MCP Server"> | ||
| Create a file `calculator_server.py` with the following code: | ||
| ```python | ||
| # calculator_server.py | ||
| from mcp.server.fastmcp import FastMCP | ||
| import logging | ||
| import sys | ||
|
|
||
| # Set up logging | ||
| logging.basicConfig(level=logging.INFO, filename="calculator_server.log") | ||
| logger = logging.getLogger("calculator-server") | ||
|
|
||
| # Initialize FastMCP server for simple tools | ||
| mcp = FastMCP("calculator-tools") | ||
|
|
||
| @mcp.tool() | ||
| def add(a: float, b: float) -> float: | ||
| """Add two numbers. | ||
|
|
||
| Args: | ||
| a: First number | ||
| b: Second number | ||
|
|
||
| Returns: | ||
| The sum of a and b | ||
| """ | ||
| logger.info(f"Adding {a} + {b}") | ||
| return a + b | ||
|
|
||
| @mcp.tool() | ||
| def subtract(a: float, b: float) -> float: | ||
| """Subtract b from a. | ||
|
|
||
| Args: | ||
| a: First number | ||
| b: Second number | ||
|
|
||
| Returns: | ||
| The result of a - b | ||
| """ | ||
| logger.info(f"Subtracting {b} from {a}") | ||
| return a - b | ||
|
|
||
| @mcp.tool() | ||
| def multiply(a: float, b: float) -> float: | ||
| """Multiply two numbers. | ||
|
|
||
| Args: | ||
| a: First number | ||
| b: Second number | ||
|
|
||
| Returns: | ||
| The product of a and b | ||
| """ | ||
| logger.info(f"Multiplying {a} * {b}") | ||
| return a * b | ||
|
|
||
| @mcp.tool() | ||
| def divide(a: float, b: float) -> float: | ||
| """Divide a by b. | ||
|
|
||
| Args: | ||
| a: First number (numerator) | ||
| b: Second number (denominator) | ||
|
|
||
| Returns: | ||
| The result of a / b | ||
| """ | ||
| if b == 0: | ||
| raise ValueError("Cannot divide by zero") | ||
| logger.info(f"Dividing {a} / {b}") | ||
| return a / b | ||
|
|
||
| if __name__ == "__main__": | ||
| # Run the MCP server using STDIO transport | ||
| mcp.run() | ||
| ``` | ||
| </Step> | ||
|
|
||
| <Step title="Install Dependencies"> | ||
| Make sure you have the required packages installed: | ||
| ```bash | ||
| pip install "praisonaiagents[llm]" mcp | ||
| ``` | ||
| </Step> | ||
| <Step title="Export API Key"> | ||
| ```bash | ||
| export OPENAI_API_KEY="your_api_key" | ||
| ``` | ||
| </Step> | ||
|
|
||
| <Step title="Run the Agent"> | ||
| Run the agent which will automatically start the calculator server: | ||
| ```bash | ||
| python calculator_client.py | ||
| ``` | ||
| </Step> | ||
| </Steps> | ||
|
|
||
| <Note> | ||
| **Requirements** | ||
| - Python 3.10 or higher | ||
| - MCP package | ||
| </Note> | ||
|
|
||
| ## Alternative LLM Integrations | ||
|
|
||
| ### Using Groq with STDIO | ||
|
|
||
| ```python | ||
| from praisonaiagents import Agent, MCP | ||
|
|
||
| calculator_agent = Agent( | ||
| instructions="""You are a calculator agent that can perform basic arithmetic operations.""", | ||
| llm="groq/llama-3.2-90b-vision-preview", | ||
| tools=MCP("python calculator_server.py") | ||
| ) | ||
|
|
||
| calculator_agent.start("What is 144 divided by 12?") | ||
| ``` | ||
|
|
||
| ### Using Ollama with STDIO | ||
|
|
||
| ```python | ||
| from praisonaiagents import Agent, MCP | ||
|
|
||
| calculator_agent = Agent( | ||
| instructions="""You are a calculator agent that can perform basic arithmetic operations.""", | ||
| llm="ollama/llama3.2", | ||
| tools=MCP("python calculator_server.py") | ||
| ) | ||
|
|
||
| calculator_agent.start("What is 15 + 27? Use the add tool with parameters a and b.") | ||
| ``` | ||
|
|
||
| ## Gradio UI Integration | ||
|
|
||
| Create a Gradio UI for your calculator service: | ||
|
|
||
| ```python | ||
| from praisonaiagents import Agent, MCP | ||
| import gradio as gr | ||
|
|
||
| def calculate(query): | ||
| calculator_agent = Agent( | ||
| instructions="""You are a calculator agent that can perform basic arithmetic operations.""", | ||
| llm="gpt-4o-mini", | ||
| tools=MCP("python calculator_server.py") | ||
| ) | ||
|
|
||
| result = calculator_agent.start(query) | ||
| return f"## Calculation Result\n\n{result}" | ||
|
|
||
| demo = gr.Interface( | ||
| fn=calculate, | ||
| inputs=gr.Textbox(placeholder="What is 25 * 16?"), | ||
| outputs=gr.Markdown(), | ||
| title="Calculator MCP Agent", | ||
| description="Ask any arithmetic question:" | ||
| ) | ||
|
|
||
| if __name__ == "__main__": | ||
| demo.launch() | ||
| ``` | ||
|
|
||
| ## Features | ||
|
|
||
| <CardGroup cols={2}> | ||
| <Card title="Simple Integration" icon="plug"> | ||
| Use standard input/output for easy integration with any tool or service. | ||
| </Card> | ||
| <Card title="Cross-Platform" icon="laptop"> | ||
| Works on any operating system that supports Python. | ||
| </Card> | ||
| <Card title="Multiple LLM Options" icon="brain"> | ||
| Use with OpenAI, Groq, Ollama, or other supported LLMs. | ||
| </Card> | ||
| <Card title="Gradio UI" icon="window"> | ||
| Create user-friendly interfaces for your STDIO integrations. | ||
| </Card> | ||
| </CardGroup> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -238,6 +238,7 @@ | |
| "group": "MCP", | ||
| "pages": [ | ||
| "mcp/airbnb", | ||
| "mcp/stdio", | ||
| "mcp/sse", | ||
| "mcp/ollama", | ||
| "mcp/groq", | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this Gradio example, the
AgentandMCPinstances are created inside thecalculatefunction. This means a newAgentand a newMCPserver process will be started for every request to the Gradio interface. This is highly inefficient and will lead to significant overhead and resource consumption.Could you move the
AgentandMCPinitialization outside thecalculatefunction so they are created only once when the Gradio app starts?