Skip to content

Python: Improvements for MCP#14003

Open
moonbox3 wants to merge 3 commits into
microsoft:mainfrom
moonbox3:sk-mcp-improvements
Open

Python: Improvements for MCP#14003
moonbox3 wants to merge 3 commits into
microsoft:mainfrom
moonbox3:sk-mcp-improvements

Conversation

@moonbox3
Copy link
Copy Markdown
Collaborator

Motivation and Context

Improvements for MCP tool calls by adding an optional sampling callback.

Description

Improvements for MCP tool calls by adding an optional sampling callback.

Contribution Checklist

moonbox3 and others added 2 commits May 13, 2026 15:23
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@moonbox3 moonbox3 self-assigned this May 13, 2026
Copilot AI review requested due to automatic review settings May 13, 2026 06:28
@moonbox3 moonbox3 requested a review from a team as a code owner May 13, 2026 06:28
@moonbox3 moonbox3 added the python Pull requests for the Python Semantic Kernel label May 13, 2026
@moonbox3
Copy link
Copy Markdown
Collaborator Author

moonbox3 commented May 13, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
connectors
   mcp.py46220755%81, 86, 98, 108–117, 124–125, 128–129, 138–139, 146, 160–167, 175–179, 181–182, 184, 193, 296–302, 337–339, 344–346, 360–361, 364–365, 373–375, 407–408, 410, 412, 414–420, 424–426, 428–438, 442–443, 447–448, 452, 454, 458–459, 463, 471, 486, 501–509, 528–529, 544–545, 564–565, 568–569, 572–577, 581–582, 585–586, 589–595, 675, 677, 750, 752, 754, 756, 831, 833, 835, 837, 839, 903, 946–951, 953–956, 961, 1014, 1016, 1019, 1032, 1052–1054, 1061–1066, 1069–1072, 1075–1077, 1081–1085, 1088–1089, 1098–1099, 1101–1103, 1117–1118, 1120–1121, 1123–1125, 1128, 1135–1138, 1146, 1151–1156, 1161, 1163, 1166–1169
TOTAL28622563980% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3889 23 💤 0 ❌ 0 🔥 1m 52s ⏱️

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances the Python MCP connector to (1) optionally gate MCP “sampling” requests behind a user-provided async consent callback and (2) avoid tool/prompt name collisions with existing plugin attributes, with accompanying unit tests.

Changes:

  • Add sampling_consent_callback (async) to MCP plugin constructors and enforce it in sampling_callback().
  • Add name-conflict detection to prevent MCP tool/prompt names from overwriting existing plugin attributes.
  • Add unit tests covering sampling consent denial, default auto-approval warning logging, and reserved-name collision behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
python/semantic_kernel/connectors/mcp.py Introduces optional sampling consent callback + warning behavior; adds conflict checks when materializing MCP tools/prompts.
python/tests/unit/connectors/mcp/test_mcp.py Adds tests for consent denial, warning logging when no callback is configured, and prevention of attribute shadowing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +501 to +510
def _has_mcp_function_name_conflict(self, item_type: str, remote_name: str, local_name: str) -> bool:
if not hasattr(self, local_name):
return False
logger.warning(
"Skipping MCP %s '%s' because normalized name '%s' conflicts with an existing plugin attribute.",
item_type,
remote_name,
local_name,
)
return True
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Addressed in 448b887: reserved attribute names are now snapshotted before MCP-generated attributes are added, so reloads can replace existing MCP functions without warning spam.

Comment on lines +395 to +399
elif not await self.sampling_consent_callback(self.name, params):
return types.ErrorData(
code=types.INTERNAL_ERROR,
message="Sampling denied by policy.",
)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Addressed in 448b887: consent callback exceptions are logged and treated as policy denial, returning ErrorData instead of propagating through the MCP session.

@@ -487,6 +534,8 @@ async def load_tools(self):
# Create methods with the kernel_function decorator for each tool
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Addressed in 448b887: moved the comment to directly precede the load_tools loop.

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 91% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by moonbox3's agents

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants