-
Notifications
You must be signed in to change notification settings - Fork 154
Improve MCP Extension Support #2551
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
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a2c66d4
register mcp bindings
ahmedmuhsin c54240e
add McpAnnotationProcessor
ahmedmuhsin 9bc5e59
optimize logic
ahmedmuhsin f71b07f
update docs
ahmedmuhsin 7d3204c
make processor static
ahmedmuhsin 92e0c3e
remove need for reflection
ahmedmuhsin d3f20d8
add tests
ahmedmuhsin 22627ce
update plugin version
ahmedmuhsin fc7181d
check for binding name duplicates
ahmedmuhsin b689eff
use stringutils
ahmedmuhsin 079b525
add template for mcptooltrigger
ahmedmuhsin cc68ab2
modify mcptooltrigger template to return a value to the mcp client
ahmedmuhsin 2fd7f32
update template to use 'isRequired' instead of 'required'
ahmedmuhsin d7d5028
always provide default values for certain attributes in mcptoolproper…
ahmedmuhsin 350d700
refine comments in McpAnnotationProcessorTest
ahmedmuhsin 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
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
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
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
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
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
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
135 changes: 135 additions & 0 deletions
135
...java/com/microsoft/azure/toolkit/lib/legacy/function/handlers/McpAnnotationProcessor.java
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,135 @@ | ||
| /* | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| */ | ||
|
|
||
| package com.microsoft.azure.toolkit.lib.legacy.function.handlers; | ||
|
|
||
| import com.microsoft.azure.toolkit.lib.common.utils.JsonUtils; | ||
| import com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding; | ||
| import com.microsoft.azure.toolkit.lib.legacy.function.bindings.BindingEnum; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Processor for handling MCP (Model Context Protocol) annotations in Azure Functions. | ||
| * This class is responsible for processing McpToolTrigger and McpToolProperty annotations | ||
| * and generating the appropriate binding configurations for function.json. | ||
| * | ||
| * McpToolTrigger annotations define tool invocation triggers with a toolName. | ||
| * McpToolProperty annotations define tool properties that are aggregated into toolProperties JSON. | ||
| */ | ||
| public class McpAnnotationProcessor { | ||
|
|
||
| /** | ||
| * Private constructor to prevent instantiation of utility class. | ||
| */ | ||
| private McpAnnotationProcessor() { | ||
| // Utility class - no instances allowed | ||
| } | ||
|
|
||
| /** | ||
| * Processes all MCP-related annotations and updates the bindings accordingly. | ||
| * This performs patching of individual bindings and generation of toolProperties in a single pass | ||
| * for optimal performance. | ||
| * | ||
| * Note: Duplicate name validation is handled at the function level by AnnotationHandlerImpl. | ||
| * | ||
| * Assumes each method has at most one McpToolTrigger that receives all McpToolProperty data. | ||
| * | ||
| * @param bindings the list of bindings to update | ||
| */ | ||
| public static void processMcpAnnotations(final List<Binding> bindings) { | ||
| if (bindings == null || bindings.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| final List<Map<String, Object>> allProperties = new ArrayList<>(); | ||
| final List<Binding> mcpTriggers = new ArrayList<>(); | ||
|
|
||
| // Single pass: Process all bindings and categorize them | ||
| for (final Binding binding : bindings) { | ||
| final BindingEnum bindingType = binding.getBindingEnum(); | ||
|
|
||
| if (bindingType == BindingEnum.McpToolProperty) { | ||
| processPropertyBinding(binding, allProperties); | ||
| } else if (bindingType == BindingEnum.McpToolTrigger) { | ||
| patchMcpToolTrigger(binding); | ||
| mcpTriggers.add(binding); | ||
| } | ||
| } | ||
|
|
||
| // Apply toolProperties to all triggers (only generate JSON once if needed) | ||
| if (!allProperties.isEmpty() && !mcpTriggers.isEmpty()) { | ||
| final String toolPropertiesJson = JsonUtils.toJson(allProperties); | ||
| for (final Binding trigger : mcpTriggers) { | ||
| trigger.setAttribute("toolProperties", toolPropertiesJson); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the 'name' attribute from an McpToolTrigger binding and sets it as 'toolName' | ||
| * on the binding for function.json generation. | ||
| * | ||
| * @param binding the binding to update | ||
| */ | ||
| private static void patchMcpToolTrigger(final Binding binding) { | ||
| final String name = (String) binding.getAttribute("name"); | ||
| if (StringUtils.isNotEmpty(name)) { | ||
| binding.setAttribute("toolName", name); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the 'name' attribute from an McpToolProperty binding and sets it as 'propertyName' | ||
| * on the binding. Note: Does NOT set 'toolName' on property bindings. | ||
| * | ||
| * @param binding the binding to update | ||
| */ | ||
| private static void patchMcpToolProperty(final Binding binding) { | ||
| final String name = (String) binding.getAttribute("name"); | ||
| if (StringUtils.isNotEmpty(name)) { | ||
| binding.setAttribute("propertyName", name); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Processes a single McpToolProperty binding: patches it and adds its attributes | ||
| * to the properties collection. | ||
| * | ||
| * @param binding the property binding to process | ||
| * @param allProperties the collection to add processed attributes to | ||
| */ | ||
| private static void processPropertyBinding(final Binding binding, | ||
| final List<Map<String, Object>> allProperties) { | ||
| patchMcpToolProperty(binding); | ||
|
|
||
| // Create filtered attributes map (excluding 'name' for toolProperties) | ||
| final Map<String, Object> propertyAttributes = createFilteredAttributesMap(binding); | ||
| allProperties.add(propertyAttributes); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a filtered map of binding attributes, excluding the 'name' attribute. | ||
| * | ||
| * @param binding the binding to extract attributes from | ||
| * @return a new map with all attributes except 'name' | ||
| */ | ||
| private static Map<String, Object> createFilteredAttributesMap(final Binding binding) { | ||
| final Map<String, Object> propertyAttributes = new HashMap<>(); | ||
| final Map<String, Object> bindingAttributes = binding.getBindingAttributes(); | ||
|
|
||
| for (final Map.Entry<String, Object> entry : bindingAttributes.entrySet()) { | ||
| if (!"name".equals(entry.getKey())) { | ||
| propertyAttributes.put(entry.getKey(), entry.getValue()); | ||
| } | ||
| } | ||
|
|
||
| return propertyAttributes; | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.