-
Notifications
You must be signed in to change notification settings - Fork 289
Refactor common code shared amongst built in MCP tools #2986
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
Open
aaronburtle
wants to merge
30
commits into
main
Choose a base branch
from
dev/aaronburtle/RefactorCommonCodeBuiltInMCPtools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
ed51e3b
Refactor argument parsing
aaronburtle 4f40315
add metadata helper and use in delete and read
aaronburtle a0a38fe
factor out auth
aaronburtle 7f3f786
factor out metadata
aaronburtle dec5291
factor out the auth usage in update tool
aaronburtle 9ec2e66
factor out argument parsing from update record tool
aaronburtle f93205a
erroneus warning ignore
aaronburtle 268fc8e
import ordering?
aaronburtle b3e1b99
import ordering?
aaronburtle 827e3a8
import ordering?
aaronburtle 02ad83a
Update src/Azure.DataApiBuilder.Mcp/BuiltInTools/ReadRecordsTool.cs
aaronburtle 92665c6
co-pilot comments
aaronburtle f436184
Merge branch 'dev/aaronburtle/RefactorCommonCodeBuiltInMCPtools' of g…
aaronburtle b2974af
remove comment
aaronburtle ca56271
revert arbitrary change to style
aaronburtle b983456
remove comment
aaronburtle 346111c
revert arbitrary change to style
aaronburtle 3b8a74d
use metadata objects from initialization
aaronburtle de3fae3
remove redundant comment
aaronburtle 8ffb753
revert arbitrary style change
aaronburtle e5f945f
Update src/Azure.DataApiBuilder.Mcp/BuiltInTools/UpdateRecordTool.cs
aaronburtle f9d0b75
remove redundant comment
aaronburtle 20a71ad
improved exception handling
aaronburtle 0880293
removed unused null forgiving op
aaronburtle 1871f20
remove redundant inner loggers
aaronburtle 40249c9
addressing comments, cleaner namespace usage
aaronburtle 57a5161
move execute logic to helpers
aaronburtle 3d351c0
Class summary for metadatahelper
aaronburtle f13da80
Merge branch 'main' into dev/aaronburtle/RefactorCommonCodeBuiltInMCP…
aaronburtle c306d75
Merge branch 'main' into dev/aaronburtle/RefactorCommonCodeBuiltInMCP…
souvikghosh04 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ | |
| using Azure.DataApiBuilder.Core.Configurations; | ||
| using Azure.DataApiBuilder.Core.Models; | ||
| using Azure.DataApiBuilder.Core.Resolvers; | ||
| using Azure.DataApiBuilder.Core.Resolvers.Factories; | ||
| using Azure.DataApiBuilder.Core.Services; | ||
| using Azure.DataApiBuilder.Core.Services.MetadataProviders; | ||
| using Azure.DataApiBuilder.Mcp.Model; | ||
|
|
@@ -88,7 +87,7 @@ public async Task<CallToolResult> ExecuteAsync( | |
| { | ||
| return McpResponseBuilder.BuildErrorResult( | ||
| "ToolDisabled", | ||
| $"The {this.GetToolMetadata().Name} tool is disabled in the configuration.", | ||
| $"The {GetToolMetadata().Name} tool is disabled in the configuration.", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can refactor these too since its same text in other tools as well ensuring these are always uniform. |
||
| logger); | ||
| } | ||
|
|
||
|
|
@@ -103,26 +102,17 @@ public async Task<CallToolResult> ExecuteAsync( | |
| return McpResponseBuilder.BuildErrorResult("InvalidArguments", parseError, logger); | ||
| } | ||
|
|
||
| IMetadataProviderFactory metadataProviderFactory = serviceProvider.GetRequiredService<IMetadataProviderFactory>(); | ||
| IMutationEngineFactory mutationEngineFactory = serviceProvider.GetRequiredService<IMutationEngineFactory>(); | ||
|
|
||
| // 4) Resolve metadata for entity existence check | ||
| string dataSourceName; | ||
| ISqlMetadataProvider sqlMetadataProvider; | ||
|
|
||
| try | ||
| { | ||
| dataSourceName = config.GetDataSourceNameFromEntityName(entityName); | ||
| sqlMetadataProvider = metadataProviderFactory.GetMetadataProvider(dataSourceName); | ||
| } | ||
| catch (Exception) | ||
| // 4) Resolve metadata for entity existence | ||
| if (!McpMetadataHelper.TryResolveMetadata( | ||
| entityName, | ||
| config, | ||
| serviceProvider, | ||
| out ISqlMetadataProvider sqlMetadataProvider, | ||
| out DatabaseObject dbObject, | ||
| out string dataSourceName, | ||
| out string metadataError)) | ||
| { | ||
| return McpResponseBuilder.BuildErrorResult("EntityNotFound", $"Entity '{entityName}' is not defined in the configuration.", logger); | ||
| } | ||
|
|
||
| if (!sqlMetadataProvider.EntityToDatabaseObject.TryGetValue(entityName, out DatabaseObject? dbObject) || dbObject is null) | ||
| { | ||
| return McpResponseBuilder.BuildErrorResult("EntityNotFound", $"Entity '{entityName}' is not defined in the configuration.", logger); | ||
| return McpResponseBuilder.BuildErrorResult("EntityNotFound", metadataError, logger); | ||
| } | ||
|
|
||
| // Validate it's a table or view | ||
|
|
@@ -152,7 +142,8 @@ public async Task<CallToolResult> ExecuteAsync( | |
| return McpResponseBuilder.BuildErrorResult("PermissionDenied", $"Permission denied: {authError}", logger); | ||
| } | ||
|
|
||
| // 6) Build and validate Delete context | ||
| // Need MetadataProviderFactory for RequestValidator; resolve here. | ||
| IMetadataProviderFactory metadataProviderFactory = serviceProvider.GetRequiredService<IMetadataProviderFactory>(); | ||
| RequestValidator requestValidator = new(metadataProviderFactory, runtimeConfigProvider); | ||
|
|
||
| DeleteRequestContext context = new( | ||
|
|
@@ -172,7 +163,7 @@ public async Task<CallToolResult> ExecuteAsync( | |
|
|
||
| requestValidator.ValidatePrimaryKey(context); | ||
|
|
||
| // 7) Execute | ||
| var mutationEngineFactory = serviceProvider.GetRequiredService<Azure.DataApiBuilder.Core.Resolvers.Factories.IMutationEngineFactory>(); | ||
| DatabaseType dbType = config.GetDataSourceFromDataSourceName(dataSourceName).DatabaseType; | ||
| IMutationEngine mutationEngine = mutationEngineFactory.GetMutationEngine(dbType); | ||
|
|
||
|
|
@@ -333,8 +324,7 @@ public async Task<CallToolResult> ExecuteAsync( | |
| } | ||
| catch (Exception ex) | ||
| { | ||
| ILogger<DeleteRecordTool>? innerLogger = serviceProvider.GetService<ILogger<DeleteRecordTool>>(); | ||
| innerLogger?.LogError(ex, "Unexpected error in DeleteRecordTool."); | ||
| logger?.LogError(ex, "Unexpected error in DeleteRecordTool."); | ||
|
|
||
| return McpResponseBuilder.BuildErrorResult( | ||
| "UnexpectedError", | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could probably move these also into reusable functions or enums for permission denied type of errors in MCP.