Skip to content

Revert "fix: make MCP package import optional to prevent errors when not installed"#1098

Closed
MervinPraison wants to merge 1 commit intomainfrom
revert-1096-fix/issue-1091-mcp-optional-import
Closed

Revert "fix: make MCP package import optional to prevent errors when not installed"#1098
MervinPraison wants to merge 1 commit intomainfrom
revert-1096-fix/issue-1091-mcp-optional-import

Conversation

@MervinPraison
Copy link
Copy Markdown
Owner

@MervinPraison MervinPraison commented Dec 30, 2025

User description

Reverts #1096


PR Type

Bug fix


Description

  • Removes optional MCP import handling to require MCP package

  • Eliminates MCP_AVAILABLE flag checks from initialization

  • Converts aiohttp import error to immediate failure in HTTP Stream

  • Deletes test suite for optional MCP import functionality


Diagram Walkthrough

flowchart LR
  A["Optional MCP imports<br/>with try/except"] -->|Revert| B["Direct MCP imports<br/>required"]
  C["MCP_AVAILABLE flag<br/>checks"] -->|Remove| D["No availability checks<br/>in __init__"]
  E["Lazy aiohttp handling"] -->|Convert| F["Immediate aiohttp<br/>import failure"]
  G["Optional import tests"] -->|Delete| H["No test coverage"]
Loading

File Walkthrough

Relevant files
Bug fix
mcp.py
Remove optional MCP import handlingΒ  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β 

src/praisonai-agents/praisonaiagents/mcp/mcp.py

  • Removes try/except block around MCP imports, making them required
  • Eliminates MCP_AVAILABLE flag and None assignments
  • Removes availability check from MCPToolRunner.__init__
  • Simplifies import statements to direct imports
+3/-17Β  Β 
mcp_http_stream.py
Require MCP and aiohttp imports directlyΒ  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β 

src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py

  • Removes try/except block around MCP ClientSession import
  • Converts aiohttp import error handling to immediate ImportError
  • Eliminates MCP_AVAILABLE flag checks from __init__ method
  • Removes conditional aiohttp availability checks
+3/-23Β  Β 
mcp_sse.py
Remove optional MCP import handlingΒ  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β 

src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py

  • Removes try/except block around MCP imports
  • Eliminates MCP_AVAILABLE flag and None assignments
  • Removes availability check from MCPSSEClient.__init__
  • Converts to direct imports of ClientSession and sse_client
+3/-16Β  Β 
Tests
test_mcp_optional_import.py
Delete optional MCP import test suiteΒ  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β 

src/praisonai-agents/tests/test_mcp_optional_import.py

  • Deletes entire test file covering optional MCP import functionality
  • Removes tests for lazy loading and availability checks
  • Eliminates test coverage for MCP_AVAILABLE flag behavior
  • Removes tests validating graceful degradation without MCP package
+0/-71Β  Β 

Note

Reverts optional-import behavior for MCP modules and makes MCP dependencies mandatory.

  • Replace guarded/optional imports with hard imports in mcp.py, mcp_sse.py, and mcp_http_stream.py; remove MCP_AVAILABLE checks
  • In mcp_http_stream.py, raise explicit ImportError if aiohttp is missing and default endpoint handling for /mcp
  • Adjust logging defaults; minor cleanup of initialization code paths
  • Delete tests/test_mcp_optional_import.py that validated optional MCP imports

Impact: importing MCP modules now requires mcp (and aiohttp for HTTP stream); apps without these deps will error on import.

Written by Cursor Bugbot for commit 6505f6a. This will update automatically on new commits. Configure here.

Summary by CodeRabbit

  • Refactor

    • MCP dependencies are now required and validated at import time instead of runtime initialization.
    • aiohttp is required for MCP HTTP streaming with immediate installation error notification.
  • Tests

    • Removed tests for optional MCP import behavior.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 30, 2025

πŸ“ Walkthrough

Walkthrough

This PR removes conditional import guards for MCP (Model Context Protocol) components across three modules, converting them from optional to unconditional imports. The accompanying test module validating optional import behavior is deleted, reflecting the architectural shift away from optional MCP support.

Changes

Cohort / File(s) Summary
MCP Module Import Consolidation
src/praisonai-agents/praisonaiagents/mcp/mcp.py, src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py, src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py
Removes conditional MCP availability checks (MCP_AVAILABLE flags and try/except ImportError blocks). MCP components (ClientSession, StdioServerParameters, stdio_client, sse_client) are now unconditionally imported. mcp_http_stream.py additionally replaces aiohttp fallback with immediate ImportError when unavailable. Error handling shifts from runtime to import time.
Test File Removal
src/praisonai-agents/tests/test_mcp_optional_import.py
Entire test suite deleted that verified optional MCP import behavior, including TestMCPOptionalImport class and methods testing lazy loading, agent creation without MCP tools, and submodule imports without MCP availability.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

Review effort 3/5

Poem

🐰 No more guards or cautious checks, I sayβ€”
MCP's now required to light the way!
Dependencies once shy now stand up tall,
The rabbit hops through imports, free of all.
Hopping code, no fallbacks left behind! 🌰

Pre-merge checks and finishing touches

βœ… Passed checks (3 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title clearly and specifically describes the main changeβ€”it reverts a prior commit that made MCP imports optional, returning to mandatory MCP imports.
Docstring Coverage βœ… Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • πŸ“ Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @MervinPraison, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request performs a full revert of a previous change that aimed to make the Model Context Protocol (MCP) package an optional dependency. The core impact is that mcp is now a mandatory dependency for the modules that utilize it, and its absence will lead to an ImportError at the module import stage. This streamlines the dependency management by removing conditional import logic and runtime checks for mcp availability.

Highlights

  • Revert Optional MCP Import: This pull request reverts a previous change that made the mcp (Model Context Protocol) package import optional. Consequently, mcp is now treated as a direct dependency, and its absence will result in an ImportError upon module import.
  • Removed Conditional Import Logic: All try...except ImportError blocks and MCP_AVAILABLE flags related to the mcp package have been removed from mcp.py, mcp_http_stream.py, and mcp_sse.py, simplifying the import mechanism.
  • Updated aiohttp Import Handling: In mcp_http_stream.py, the import handling for aiohttp has been modified to immediately raise an ImportError if the package is not found, rather than setting a variable to None and checking it later.
  • Removed Test File: The test file test_mcp_optional_import.py, which was specifically designed to verify the optional import behavior of mcp, has been removed as its purpose is no longer relevant.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with πŸ‘ and πŸ‘Ž on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@qodo-code-review
Copy link
Copy Markdown

PR Compliance Guide πŸ”

Below is a summary of compliance checks for this PR:

Security Compliance
🟒
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
βšͺ
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
βšͺ
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟒
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

βšͺ
Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status:
No graceful degradation: The new direct imports and import-time ImportError raising remove previous runtime
availability checks, which may prevent graceful degradation in environments where optional
dependencies are intentionally absent.

Referred Code
from mcp import ClientSession
try:
    import aiohttp
except ImportError:
    raise ImportError("aiohttp is required for HTTP Stream transport. Install with: pip install praisonaiagents[mcp]")

Learn more about managing compliance generic rules or creating your own custom rules

Compliance status legend 🟒 - Fully Compliant
🟑 - Partial Compliant
πŸ”΄ - Not Compliant
βšͺ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestionΒ  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Impact
General
Preserve original exception during import

Improve the aiohttp import error handling by chaining the original exception
using from e to preserve the full traceback for easier debugging.

src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py [20-23]

 try:
     import aiohttp
-except ImportError:
-    raise ImportError("aiohttp is required for HTTP Stream transport. Install with: pip install praisonaiagents[mcp]")
+except ImportError as e:
+    raise ImportError(
+        "aiohttp is required for HTTP Stream transport. "
+        "Install with: pip install praisonaiagents[mcp]"
+    ) from e
  • Apply / Chat
Suggestion importance[1-10]: 5

__

Why: The suggestion correctly identifies an opportunity to improve error handling by chaining exceptions, which enhances debuggability without altering the PR's core logic.

Low
  • More

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly reverts the optional import handling for the MCP package, making it a required dependency for the MCP-related modules. The changes simplify the code by removing conditional logic and flags, which improves clarity and maintainability. The updated aiohttp import handling to fail faster is also a good improvement. The deletion of the test file for the removed optional import functionality is appropriate. One thing to consider for a follow-up is the lazy-loading mechanism in praisonaiagents/__init__.py. Currently, it catches the ImportError when mcp is not installed and causes from praisonaiagents import MCP to assign None to MCP. This can lead to a less clear TypeError when a user tries to instantiate MCP(). To make mcp a truly required dependency at the package level, you might want to let the ImportError propagate from __init__.py as well. Overall, the changes in this PR are solid and a good step towards simplifying the dependency management.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py (1)

19-23: MCP and aiohttp are now required dependencies.

The unconditional import of ClientSession and the ImportError for missing aiohttp establish these as mandatory dependencies. The error message provides helpful installation guidance.

Optional: Follow exception chaining best practice

Per static analysis, use raise ... from None to suppress exception context when the ImportError is intentional:

 try:
     import aiohttp
 except ImportError:
-    raise ImportError("aiohttp is required for HTTP Stream transport. Install with: pip install praisonaiagents[mcp]")
+    raise ImportError("aiohttp is required for HTTP Stream transport. Install with: pip install praisonaiagents[mcp]") from None

This clarifies that the new exception is not caused by the caught ImportError but is an intentional error message.

πŸ“œ Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between e59be8f and 6505f6a.

πŸ“’ Files selected for processing (4)
  • src/praisonai-agents/praisonaiagents/mcp/mcp.py
  • src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py
  • src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py
  • src/praisonai-agents/tests/test_mcp_optional_import.py
πŸ’€ Files with no reviewable changes (1)
  • src/praisonai-agents/tests/test_mcp_optional_import.py
🧰 Additional context used
πŸ““ Path-based instructions (2)
src/praisonai-agents/praisonaiagents/**/*.py

πŸ“„ CodeRabbit inference engine (src/praisonai-agents/CLAUDE.md)

Support async execution using await workflow.astart() and await agent.aexecute(task) for asynchronous task processing

Files:

  • src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py
  • src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py
  • src/praisonai-agents/praisonaiagents/mcp/mcp.py
src/praisonai-agents/praisonaiagents/mcp/**/*.py

πŸ“„ CodeRabbit inference engine (src/praisonai-agents/CLAUDE.md)

Use MCP (Model Context Protocol) server with Server-Sent Events (SSE) support for distributed tool execution and real-time communication

Files:

  • src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py
  • src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py
  • src/praisonai-agents/praisonaiagents/mcp/mcp.py
🧠 Learnings (1)
πŸ“š Learning: 2025-12-30T16:10:06.589Z
Learnt from: CR
Repo: MervinPraison/PraisonAI PR: 0
File: src/praisonai-agents/CLAUDE.md:0-0
Timestamp: 2025-12-30T16:10:06.589Z
Learning: Applies to src/praisonai-agents/praisonaiagents/mcp/**/*.py : Use MCP (Model Context Protocol) server with Server-Sent Events (SSE) support for distributed tool execution and real-time communication

Applied to files:

  • src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py
  • src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py
  • src/praisonai-agents/praisonaiagents/mcp/mcp.py
πŸͺ› Ruff (0.14.10)
src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py

23-23: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


23-23: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: Cursor Bugbot
  • GitHub Check: test-core (3.11)
  • GitHub Check: Run tests and collect coverage
  • GitHub Check: quick-test
  • GitHub Check: quick-test
πŸ”‡ Additional comments (2)
src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py (1)

14-15: LGTM - Consistent with the revert.

These unconditional imports align with the architectural change making MCP a required dependency across all transport implementations.

src/praisonai-agents/praisonaiagents/mcp/mcp.py (1)

14-15: The mcp submodule requires the mcp package to be installed, despite it being an optional dependency.

Lines 14-15 have unconditional imports from the mcp package. Since mcp/__init__.py also unconditionally imports from mcp.py (line 13), any attempt to use the mcp submodule will fail with ImportError if the mcp package is not installed, even though mcp is defined as an optional dependency in pyproject.toml.

This is a breaking change: users must install the [mcp] extra to use any part of the mcp submodule. Without a try/except wrapper around lines 14-15 or lazy loading at the submodule level, the failure happens at import time rather than at usage time.

Consider either:

  • Wrapping the mcp imports with try/except and deferring initialization, or
  • Ensuring documentation clearly states that mcp submodule requires pip install praisonaiagents[mcp]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant