-
Notifications
You must be signed in to change notification settings - Fork 22
Mcp tool Trigger changes #365
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
Closed
Closed
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ed75254
MCP Server Support
swapnil-nagar b4a6221
Fixing the naming
swapnil-nagar 1a9badd
Adding comments
swapnil-nagar 3afe50a
4.8.0-beta.0
swapnil-nagar 87cf071
4.8.0-beta.1
swapnil-nagar 881a6ba
4.9.0-beta.0
swapnil-nagar 1930d06
4.10.0-alpha.0
swapnil-nagar 27680e4
4.10.0-experimental.0
swapnil-nagar e9e6128
4.10.0-experimental.1
swapnil-nagar 2567337
Updating Pacakge for Release
swapnil-nagar 771f873
Adding Properties
swapnil-nagar d16d7d4
Adding .net type fluent API
swapnil-nagar 9ab24aa
Adding support for zod conversion
swapnil-nagar c989f9e
Adding null toolProperties support
swapnil-nagar 02469f2
Adding z.Object support
39990b6
Fixing linting issue
d1ab8d8
Adding more types
8600841
Fixing the property value to property name
b8984fe
Removing Undici Dependency
swapnil-nagar 5bcba5a
Updating the version
swapnil-nagar 71c4a1a
MCP Server Support
swapnil-nagar d33b747
Resolving Merge Conflict
swapnil-nagar 87ea131
Merge branch 'swapnil/mcpTool' of https://github.com/Azure/azure-func…
swapnil-nagar 7d2aef0
Fixing test for Node 18 which is going to deprecate soon
swapnil-nagar 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| export const version = '4.7.0'; | ||
| export const version = '4.8.0'; | ||
|
|
||
| export const returnBindingKey = '$return'; |
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,149 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { McpToolProperty, McpToolTriggerOptions, McpToolTriggerOptionsToRpc } from '../../types'; | ||
|
|
||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| /** | ||
| * Converts an McpToolTriggerOptions object to an McpToolTriggerOptionsToRpc object. | ||
| * | ||
| * @param mcpToolTriggerOptions - The input options to be converted. | ||
| * @returns The converted McpToolTriggerOptionsToRpc object. | ||
| */ | ||
| export function converToMcpToolTriggerOptionsToRpc( | ||
| mcpToolTriggerOptions: McpToolTriggerOptions | ||
| ): McpToolTriggerOptionsToRpc { | ||
| // Base object for the return value | ||
| const baseResult = { | ||
| toolName: mcpToolTriggerOptions.toolName, | ||
| description: mcpToolTriggerOptions.description, | ||
| }; | ||
|
|
||
| // Check for null or undefined toolProperties | ||
| if (!mcpToolTriggerOptions?.toolProperties) { | ||
| return { | ||
| ...baseResult, | ||
| toolProperties: JSON.stringify([]), // Default to an empty array | ||
| }; | ||
| } | ||
|
|
||
| // Check if toolProperties is an array of McpToolProperty objects | ||
| if (Array.isArray(mcpToolTriggerOptions.toolProperties)) { | ||
| const isValid = mcpToolTriggerOptions.toolProperties.every(isMcpToolProperty); | ||
| if (isValid) { | ||
| return { | ||
| ...baseResult, | ||
| toolProperties: JSON.stringify(mcpToolTriggerOptions.toolProperties), | ||
| }; | ||
| } else { | ||
| throw new Error( | ||
| 'Invalid toolProperties: Array contains invalid McpToolProperty, please validate the parameters.' | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Handle cases where toolProperties is an object (e.g., Zod schema) | ||
| if (typeof mcpToolTriggerOptions.toolProperties === 'object') { | ||
| // Define the type of the ZodObject shape and ZodPropertyDef | ||
| type ZodPropertyDef = { | ||
| description?: string; | ||
| typeName: string; | ||
| }; | ||
| type ZodObjectShape = Record<string, { _def: ZodPropertyDef }>; | ||
|
|
||
| // Define the type of the toolProperties object | ||
| type ToolProperties = | ||
| | { | ||
| _def?: { | ||
| typeName?: string; | ||
| }; | ||
| shape?: ZodObjectShape; | ||
| } | ||
| | Record<string, unknown>; | ||
|
|
||
| let isZodObject = false; | ||
|
|
||
| const toolProperties = mcpToolTriggerOptions.toolProperties as ToolProperties; | ||
|
|
||
| // Check if the object is a ZodObject | ||
| if ((toolProperties?._def as { typeName?: string })?.typeName === 'ZodObject') { | ||
| isZodObject = true; | ||
| } | ||
|
|
||
| // Check if shape is a valid ZodObject shape | ||
| const shape: ZodObjectShape | Record<string, unknown> = isZodObject | ||
| ? (toolProperties as { shape: ZodObjectShape }).shape | ||
| : toolProperties; | ||
|
|
||
| // Extract properties from the ZodObject shape | ||
| const result = Object.keys(shape).map((propertyName) => { | ||
| const property = shape[propertyName] as { _def: ZodPropertyDef }; | ||
| const description = property?._def?.description || ''; | ||
| const propertyType = getPropertyType(property?._def?.typeName?.toLowerCase() || 'unknown'); // Extract type name or default to "unknown" | ||
|
|
||
| return { | ||
| propertyName, | ||
| propertyType, | ||
| description, | ||
| }; | ||
| }); | ||
|
|
||
| return { | ||
| ...baseResult, | ||
| toolProperties: JSON.stringify(result), | ||
| }; | ||
| } | ||
| // Handle cases where toolProperties is not an array | ||
| throw new Error('Invalid toolProperties: Expected an array of McpToolProperty objects or zod objects.'); | ||
| } | ||
|
|
||
| // Helper function to infer property type from zod schema | ||
| function getPropertyType(zodType: string): string { | ||
| switch (zodType) { | ||
| case 'zodnumber': | ||
| return 'number'; | ||
| case 'zodstring': | ||
| return 'string'; | ||
| case 'zodboolean': | ||
| return 'boolean'; | ||
| case 'zodarray': | ||
| return 'array'; | ||
| case 'zodobject': | ||
| return 'object'; | ||
| case 'zodbigint': | ||
| return 'long'; | ||
| case 'zoddate': | ||
| return 'DateTime'; | ||
| case 'zodtuple': | ||
| return 'Tuple'; | ||
| default: | ||
| console.warn(`Unknown zod type: ${zodType}`); | ||
| return 'unknown'; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Type guard to check if a given object is of type McpToolProperty. | ||
| * | ||
| * @param property - The object to check. | ||
| * @returns True if the object is of type McpToolProperty, otherwise false. | ||
| * | ||
| * This function ensures that the object: | ||
| * - Is not null and is of type 'object'. | ||
| * - Contains the required properties: 'propertyName', 'propertyValue', and 'description'. | ||
| * - Each of these properties is of the correct type (string). | ||
| */ | ||
| function isMcpToolProperty(property: unknown): property is McpToolProperty { | ||
| return ( | ||
| typeof property === 'object' && | ||
| property !== null && | ||
| 'propertyName' in property && | ||
| 'propertyType' in property && | ||
| 'description' in property && | ||
| typeof (property as McpToolProperty).propertyName === 'string' && | ||
| typeof (property as McpToolProperty).propertyType === 'string' && | ||
| typeof (property as McpToolProperty).description === 'string' | ||
| ); | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { FunctionOptions, FunctionResult, FunctionTrigger } from './index'; | ||
| import { InvocationContext } from './InvocationContext'; | ||
|
|
||
| /** | ||
| * A handler function for MCP Tool triggers. | ||
| * | ||
| * @param messages - The messages or data received by the trigger. | ||
| * @param context - The invocation context for the function. | ||
| * @returns A result that can be a promise or a synchronous value. | ||
| */ | ||
| export type McpToolTriggerHandler = (messages: unknown, context: InvocationContext) => FunctionResult; | ||
|
|
||
| /** | ||
| * Configuration options for an MCP Tool function. | ||
| * This includes trigger-specific options and general function options. | ||
| */ | ||
| export interface McpToolFunctionOptions extends McpToolTriggerOptions, Partial<FunctionOptions> { | ||
| /** | ||
| * The handler function to execute when the trigger is invoked. | ||
| */ | ||
| handler: McpToolTriggerHandler; | ||
|
|
||
| /** | ||
| * The trigger configuration for the MCP Tool. | ||
| */ | ||
| trigger?: McpToolTrigger; | ||
| } | ||
|
|
||
| /** | ||
| * Configuration options for an MCP Tool trigger. | ||
| * These options define the behavior and metadata for the trigger. | ||
| */ | ||
| export interface McpToolTriggerOptions { | ||
| /** | ||
| * The name of the tool associated with the trigger. | ||
| * This is typically an app setting or environment variable. | ||
| */ | ||
| toolName: string; | ||
|
|
||
| /** | ||
| * A description of the tool or trigger. | ||
| * This provides additional context about the trigger's purpose. | ||
| */ | ||
| description: string; | ||
|
|
||
| /** | ||
| * Additional properties or metadata for the tool. | ||
| * This is a dictionary of key-value pairs that can be used to configure the trigger. | ||
| */ | ||
| toolProperties?: any | McpToolProperty[]; | ||
| } | ||
|
|
||
| /** | ||
| * Configuration options for an MCP Tool trigger. | ||
| * These options define the behavior and metadata for the trigger. | ||
| */ | ||
| export interface McpToolTriggerOptionsToRpc { | ||
| /** | ||
| * The name of the tool associated with the trigger. | ||
| * This is typically an app setting or environment variable. | ||
| */ | ||
| toolName: string; | ||
|
|
||
| /** | ||
| * A description of the tool or trigger. | ||
| * This provides additional context about the trigger's purpose. | ||
| */ | ||
| description: string; | ||
|
|
||
| /** | ||
| * Additional properties or metadata for the tool. | ||
| * This is a dictionary of key-value pairs that can be used to configure the trigger. | ||
| */ | ||
| toolProperties?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Represents an MCP Tool trigger, combining base function trigger options | ||
| * with MCP Tool-specific trigger options. | ||
| */ | ||
| export type McpToolTrigger = FunctionTrigger & McpToolTriggerOptionsToRpc; | ||
|
|
||
| export interface McpToolProperty { | ||
| /** | ||
| * The name of the property. | ||
| */ | ||
| propertyName: string; | ||
|
|
||
| /** | ||
| * The type of the property. | ||
| */ | ||
| propertyType: string; | ||
|
|
||
| /** | ||
| * A description of the property. | ||
| * This provides additional context about the purpose or usage of the property. | ||
| */ | ||
| description: string; | ||
|
|
||
| /** | ||
| * Indicates whether the property is required. | ||
| */ | ||
| required?: boolean; | ||
| } |
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.
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.