[PR #22170] minor fix: remove duplicates, fix typo, and add restriction for get mcp server #29858

Closed
opened 2026-02-21 20:46:23 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/22170

State: closed
Merged: Yes


Pull Request Documentation

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

This pull request addresses three minor but important fixes in the MCP (Model Context Protocol) server implementation:

  1. Security Enhancement: Added tenant isolation restriction to the MCP server refresh endpoint
  2. Bug Fix: Corrected a typo in variable naming (clinet_nameclient_name)
  3. Code Cleanup: Removed duplicate tenant_id filter in MCP provider creation

Changes Made

1. Security Fix - Tenant Isolation (AppMCPServerRefreshController)

File: api/controllers/console/app/mcp_server.py (lines 93-97)

Before:

server = db.session.query(AppMCPServer).filter(AppMCPServer.id == server_id).first()

After:

server = (
  db.session.query(AppMCPServer)
  .filter(AppMCPServer.id == server_id and AppMCPServer.tenant_id == current_user.current_tenant_id)
  .first()
)

Impact: This change ensures that users can only refresh MCP servers that belong to their tenant, preventing unauthorized access to other tenants' servers.

2. Typo Fix - Variable Naming (MCPHandler)

File: core/app/apps/base_app_queue_manager.py (lines 115, 121)

Before:

clinet_name = f"{client_info.name}@{client_info.version}"
# ...
name=clinet_name,

After:

client_name = f"{client_info.name}@{client_info.version}"
# ...
name=client_name,

Impact: Corrects the misspelled variable name from clinet_name to client_name, improving code readability and consistency.

3. Code Cleanup - Remove Duplicate Filter (create_mcp_provider)

File: core/tools/provider/mcp/mcp_tool_provider.py (line 72)

Before:

.filter(
  and_(
      MCPToolProvider.server_url_hash == server_url_hash,
      MCPToolProvider.server_identifier == server_identifier,
  ),
  MCPToolProvider.tenant_id == tenant_id,  # This line was removed
)

After:

.filter(
  and_(
      MCPToolProvider.server_url_hash == server_url_hash,
      MCPToolProvider.server_identifier == server_identifier,
  ),
)

Impact: Removes redundant tenant_id filtering that was likely duplicated, cleaning up the query logic.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Additional Notes

This PR represents a collection of minor but important fixes that improve both security and code quality. The changes are backward-compatible and should not affect existing functionality while enhancing the overall robustness of the MCP server implementation.

**Original Pull Request:** https://github.com/langgenius/dify/pull/22170 **State:** closed **Merged:** Yes --- # Pull Request Documentation > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 2. Ensure there is an associated issue and you have been assigned to it > 3. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary This pull request addresses three minor but important fixes in the MCP (Model Context Protocol) server implementation: 1. **Security Enhancement**: Added tenant isolation restriction to the MCP server refresh endpoint 2. **Bug Fix**: Corrected a typo in variable naming (`clinet_name` → `client_name`) 3. **Code Cleanup**: Removed duplicate tenant_id filter in MCP provider creation ### Changes Made #### 1. Security Fix - Tenant Isolation (AppMCPServerRefreshController) **File**: `api/controllers/console/app/mcp_server.py` (lines 93-97) **Before**: ```python server = db.session.query(AppMCPServer).filter(AppMCPServer.id == server_id).first() ``` **After**: ```python server = ( db.session.query(AppMCPServer) .filter(AppMCPServer.id == server_id and AppMCPServer.tenant_id == current_user.current_tenant_id) .first() ) ``` **Impact**: This change ensures that users can only refresh MCP servers that belong to their tenant, preventing unauthorized access to other tenants' servers. #### 2. Typo Fix - Variable Naming (MCPHandler) **File**: `core/app/apps/base_app_queue_manager.py` (lines 115, 121) **Before**: ```python clinet_name = f"{client_info.name}@{client_info.version}" # ... name=clinet_name, ``` **After**: ```python client_name = f"{client_info.name}@{client_info.version}" # ... name=client_name, ``` **Impact**: Corrects the misspelled variable name from `clinet_name` to `client_name`, improving code readability and consistency. #### 3. Code Cleanup - Remove Duplicate Filter (create_mcp_provider) **File**: `core/tools/provider/mcp/mcp_tool_provider.py` (line 72) **Before**: ```python .filter( and_( MCPToolProvider.server_url_hash == server_url_hash, MCPToolProvider.server_identifier == server_identifier, ), MCPToolProvider.tenant_id == tenant_id, # This line was removed ) ``` **After**: ```python .filter( and_( MCPToolProvider.server_url_hash == server_url_hash, MCPToolProvider.server_identifier == server_identifier, ), ) ``` **Impact**: Removes redundant tenant_id filtering that was likely duplicated, cleaning up the query logic. ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods ## Additional Notes This PR represents a collection of minor but important fixes that improve both security and code quality. The changes are backward-compatible and should not affect existing functionality while enhancing the overall robustness of the MCP server implementation.
yindo added the pull-request label 2026-02-21 20:46:23 -05:00
yindo closed this issue 2026-02-21 20:46:23 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#29858