Commit 3cf09f9
Implementation of Custom Tool in MCP (#3048)
## Why make this change?
- Closes on - #2877
## What is this change?
This pull request introduces a new system for dynamically generating and
registering custom MCP tools based on stored procedure entity
configurations in the runtime configuration. The main changes are the
implementation of the `DynamicCustomTool` class, a factory to create
these tools from configuration, and the necessary service registration
logic to ensure these custom tools are available at runtime.
**Dynamic custom MCP tool support:**
* Added the `DynamicCustomTool` class, which implements `IMcpTool` and
provides logic for generating tool metadata, validating configuration,
handling authorization, executing the underlying stored procedure, and
formatting the response. This enables each stored procedure entity with
`custom-tool` enabled to be exposed as a dedicated MCP tool.
* Introduced the `CustomMcpToolFactory` class, which scans the runtime
configuration for stored procedure entities marked with `custom-tool`
enabled and creates corresponding `DynamicCustomTool` instances.
**Dependency injection and service registration:**
* Updated the MCP server startup (`AddDabMcpServer`) to register custom
tools generated from configuration by calling a new
`RegisterCustomTools` method after auto-discovering static tools.
* Modified the `RegisterAllMcpTools` method to exclude
`DynamicCustomTool` from auto-discovery (since these are created
dynamically per configuration) and added the `RegisterCustomTools`
method to register each generated custom tool as a singleton service.
## How was this tested?
- [x] Unit Tests
- [x] Manual Tests using Insomnia and VS code GHCP chat
## Sample Request(s)
1. List All Tools (also includes custom tool)
```
{
"jsonrpc": "2.0",
"method": "tools/list",
"params": {},
"id": 1
}
```
2. Get Books (no parameters)
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "get_books"
},
"id": 2
}
```
3. Get Book by ID
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "get_book",
"arguments": {
"id": 1
}
},
"id": 3
}
```
4. Insert Book
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "insert_book",
"arguments": {
"title": "Test Book from MCP",
"publisher_id": "1234"
}
},
"id": 4
}
```
5. Count Books (no parameters)
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "count_books"
},
"id": 5
}
```
Error Scenarios
6. Missing Required Parameter
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "get_book"
},
"id": 6
}
```
7. Non-Existent Tool
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "non_existent_tool"
},
"id": 7
}
```
8. Invalid Foreign Key
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "insert_book",
"arguments": {
"title": "Test Book",
"publisher_id": "999999"
}
},
"id": 8
}
```
Edge Cases
9. SQL Injection Attempt
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "get_book",
"arguments": {
"id": "1; DROP TABLE books; --"
}
},
"id": 9
}
```
11. Special Characters
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "insert_book",
"arguments": {
"title": "Test Book with 'quotes' and \"double quotes\" and <tags>",
"publisher_id": "1234"
}
},
"id": 10
}
```
12. Empty String
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "insert_book",
"arguments": {
"title": "",
"publisher_id": "1234"
}
},
"id": 11
}
```
13. Invalid Type (string for int)
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "get_book",
"arguments": {
"id": "not_a_number"
}
},
"id": 12
}
```
14. Negative ID
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "get_book",
"arguments": {
"id": -1
}
},
"id": 13
}
```
15. Maximum Integer
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "get_book",
"arguments": {
"id": 2147483647
}
},
"id": 14
}
```
16. Case Sensitivity (should fail)
```
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "GET_BOOKS"
},
"id": 15
}
```
---------
Co-authored-by: Aniruddh Munde <[email protected]>1 parent a62cb9e commit 3cf09f9
5 files changed
Lines changed: 868 additions & 1 deletion
File tree
- src
- Azure.DataApiBuilder.Mcp/Core
- Service.Tests/Mcp
Lines changed: 63 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
0 commit comments