Skip to main content

MCP with Streamlit

This guide explains how to properly integrate MCP (Model Context Protocol) tools with PraisonAI Agents in Streamlit applications, addressing common issues and providing working solutions.

Common Issues

When integrating MCP tools with Streamlit, users often encounter these problems:

Issue #1: Agent Re-initialization

Problem: Agent gets re-initialized on every Streamlit interaction, causing MCP tools to fail. Solution: Use Streamlitโ€™s session state to initialize the agent only once.

Issue #2: LLM Provider Format

Problem: Using provider/model format like "ollama/llama3.2" can cause tool calling issues in Streamlit environments. Solution: Use standard model names like "gpt-4o-mini" instead of provider/model format.

Issue #3: Missing Error Handling

Problem: No user feedback when MCP initialization fails. Solution: Implement comprehensive error handling with user-friendly messages.

Working Example

Hereโ€™s a complete working example that demonstrates the correct approach:

Best Practices

1. Session State Management

Always use Streamlitโ€™s session state to manage agent lifecycle:

2. LLM Model Selection

Use standard model names, avoid provider/model format in Streamlit:

3. Error Handling

Implement comprehensive error handling:

4. User Feedback

Provide clear feedback during initialization:

Troubleshooting

MCP Tools Not Found

If you get โ€œMCP tool cannot be foundโ€ errors:
  1. Check MCP Server Command: Ensure the MCP server command is correct and the server starts successfully
  2. Verify Dependencies: Make sure all required dependencies (Node.js, npm, etc.) are installed
  3. Test Manually: Try running the MCP server command manually first
  4. Enable Debug Mode: Set debug=True in MCP constructor for detailed logs

Tool Calling Issues

If tools arenโ€™t being called properly:
  1. Use Standard LLM Format: Avoid provider/model format like "ollama/llama3.2"
  2. Check API Keys: Ensure proper environment variables are set for your chosen LLM
  3. Session State: Verify agent is properly stored in session state
  4. Timeout Settings: Increase timeout if needed: MCP(command, timeout=120)

Threading Conflicts

If you encounter threading-related errors:
  1. Single Agent Instance: Use only one agent instance per session
  2. Proper Cleanup: Let Streamlit handle cleanup naturally
  3. Avoid Manual Threading: Donโ€™t create additional threads in Streamlit

Complete Working Example

For a complete, production-ready example, see: This example includes:
  • โœ… Proper session state management
  • โœ… Comprehensive error handling
  • โœ… User-friendly interface
  • โœ… Debug mode support
  • โœ… Troubleshooting guidance
  • โœ… Configuration options

Environment Setup

Make sure your environment has the required dependencies:

Running the Example

Summary

The key to successfully using MCP with Streamlit is:
  1. Proper session state management - Initialize agent only once
  2. Standard LLM format - Avoid provider/model format in Streamlit
  3. Comprehensive error handling - Provide user feedback
  4. Correct usage patterns - Follow Streamlit best practices
By following these guidelines, MCP tools will work reliably in your Streamlit applications without requiring any modifications to the core MCP implementation.