Skip to content

Commit afc1352

Browse files
Filter tools
1 parent 976c3ac commit afc1352

File tree

13 files changed

+347
-3051
lines changed

13 files changed

+347
-3051
lines changed

RestClient.Net.McpGenerator.Cli/Program.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ static void PrintUsage()
4141
Console.WriteLine(
4242
" --ext-class <class> Extensions class name (default: 'ApiExtensions')"
4343
);
44+
Console.WriteLine(
45+
" -t, --tags <tag1,tag2> Comma-separated list of OpenAPI tags to include (optional)"
46+
);
4447
Console.WriteLine(" -h, --help Show this help message");
4548
}
4649

@@ -52,6 +55,7 @@ static void PrintUsage()
5255
var serverName = "ApiMcp";
5356
var extensionsNamespace = "Generated";
5457
var extensionsClass = "ApiExtensions";
58+
string? tagsFilter = null;
5559

5660
for (var i = 0; i < args.Length; i++)
5761
{
@@ -79,6 +83,10 @@ static void PrintUsage()
7983
case "--ext-class":
8084
extensionsClass = GetNextArg(args, i++, "ext-class") ?? extensionsClass;
8185
break;
86+
case "-t"
87+
or "--tags":
88+
tagsFilter = GetNextArg(args, i++, "tags");
89+
break;
8290
default:
8391
break;
8492
}
@@ -104,7 +112,8 @@ static void PrintUsage()
104112
namespaceName,
105113
serverName,
106114
extensionsNamespace,
107-
extensionsClass
115+
extensionsClass,
116+
tagsFilter
108117
);
109118
}
110119

@@ -154,13 +163,26 @@ static async Task GenerateCode(Config config)
154163
}
155164

156165
Console.WriteLine($"Read {openApiSpec.Length} characters\n");
166+
167+
// Parse tags filter if provided
168+
ISet<string>? includeTags = null;
169+
if (!string.IsNullOrWhiteSpace(config.TagsFilter))
170+
{
171+
includeTags = new HashSet<string>(
172+
config.TagsFilter.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries),
173+
StringComparer.OrdinalIgnoreCase
174+
);
175+
Console.WriteLine($"Filtering to tags: {string.Join(", ", includeTags)}");
176+
}
177+
157178
Console.WriteLine("Generating MCP tools code...");
158179

159180
var result = McpServerGenerator.Generate(
160181
openApiSpec,
161182
@namespace: config.Namespace,
162183
serverName: config.ServerName,
163-
extensionsNamespace: config.ExtensionsNamespace
184+
extensionsNamespace: config.ExtensionsNamespace,
185+
includeTags: includeTags
164186
);
165187

166188
#pragma warning disable IDE0010
@@ -186,5 +208,6 @@ internal sealed record Config(
186208
string Namespace,
187209
string ServerName,
188210
string ExtensionsNamespace,
189-
string ExtensionsClass
211+
string ExtensionsClass,
212+
string? TagsFilter
190213
);

RestClient.Net.McpGenerator/McpServerGenerator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ public static class McpServerGenerator
1212
/// <param name="namespace">The namespace for generated MCP tools.</param>
1313
/// <param name="serverName">The MCP server name.</param>
1414
/// <param name="extensionsNamespace">The namespace of the pre-generated extensions.</param>
15+
/// <param name="includeTags">Optional set of tags to include. If specified, only operations with these tags are generated.</param>
1516
/// <returns>A Result containing the generated C# code or error message.</returns>
1617
#pragma warning disable CA1054
1718
public static Result<string, string> Generate(
1819
string openApiContent,
1920
string @namespace,
2021
string serverName,
21-
string extensionsNamespace
22+
string extensionsNamespace,
23+
ISet<string>? includeTags = null
2224
)
2325
#pragma warning restore CA1054
2426
{
@@ -43,7 +45,8 @@ string extensionsNamespace
4345
document,
4446
@namespace,
4547
serverName,
46-
extensionsNamespace
48+
extensionsNamespace,
49+
includeTags
4750
)
4851
);
4952
}

RestClient.Net.McpGenerator/McpToolGenerator.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ internal static class McpToolGenerator
1818
/// <param name="namespace">The namespace for the MCP server.</param>
1919
/// <param name="serverName">The MCP server name.</param>
2020
/// <param name="extensionsNamespace">The namespace of the extensions.</param>
21+
/// <param name="includeTags">Optional set of tags to filter operations. If specified, only operations with these tags are generated.</param>
2122
/// <returns>The generated MCP tools code.</returns>
2223
public static string GenerateTools(
2324
OpenApiDocument document,
2425
string @namespace,
2526
string serverName,
26-
string extensionsNamespace
27+
string extensionsNamespace,
28+
ISet<string>? includeTags = null
2729
)
2830
{
2931
var tools = new List<string>();
@@ -38,6 +40,21 @@ string extensionsNamespace
3840

3941
foreach (var operation in path.Value.Operations)
4042
{
43+
// Skip if tags filter is specified and operation doesn't match
44+
if (includeTags != null && includeTags.Count > 0)
45+
{
46+
var operationTags = operation.Value.Tags;
47+
if (
48+
operationTags == null
49+
|| !operationTags.Any(tag =>
50+
includeTags.Contains(tag.Name, StringComparer.OrdinalIgnoreCase)
51+
)
52+
)
53+
{
54+
continue;
55+
}
56+
}
57+
4158
var toolMethod = GenerateTool(
4259
path.Key,
4360
operation.Key,

Samples/NucliaDbClient.Demo/NucliaDbClient.Demo.csproj

Lines changed: 0 additions & 11 deletions
This file was deleted.

Samples/NucliaDbClient.Demo/Program.cs

Lines changed: 0 additions & 64 deletions
This file was deleted.

Samples/NucliaDbClient.McpServer/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
3030
builder.Services.AddSingleton<NucliaDbTools>();
3131

3232
// Add MCP server with stdio transport and tools from assembly
33-
builder.Services
34-
.AddMcpServer()
35-
.WithStdioServerTransport()
36-
.WithToolsFromAssembly();
33+
builder.Services.AddMcpServer().WithStdioServerTransport().WithToolsFromAssembly();
3734

3835
var host = builder.Build();
3936
await host.RunAsync();

Samples/NucliaDbClient.McpServer/README.md

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,20 @@ This is a Model Context Protocol (MCP) server that provides Claude Code with acc
1010

1111
## Quick Start
1212

13-
### 1. Start NucliaDB and MCP Server
13+
### Run the setup script
1414

1515
```bash
1616
cd Samples/NucliaDbClient.McpServer
17-
./start-mcp-server.sh
17+
./run-for-claude.sh
1818
```
1919

20-
This will:
21-
- Start NucliaDB via docker-compose (PostgreSQL + NucliaDB containers)
22-
- Wait for NucliaDB to be ready
23-
- Build and run the MCP server
20+
This ONE script does everything:
21+
- Starts NucliaDB via docker-compose (if not already running)
22+
- Waits for NucliaDB to be ready
23+
- Builds the MCP server
24+
- Adds it to Claude Code configuration
2425

25-
### 2. Alternative: Run MCP Server Only
26-
27-
If NucliaDB is already running:
28-
29-
```bash
30-
./run-mcp-server.sh
31-
```
32-
33-
### 3. Stop NucliaDB
26+
### Stop NucliaDB
3427

3528
```bash
3629
./stop-nucliadb.sh
@@ -94,16 +87,22 @@ Then edit the file to update paths for your system.
9487

9588
## Available Tools
9689

97-
The MCP server provides access to all NucliaDB REST API operations, including:
90+
The MCP server is **filtered to Search operations only** (18 tools) to avoid flooding Claude Code:
9891

99-
- **Knowledge Box Management**: Get, create, delete knowledge boxes
100-
- **Search**: Full-text search, semantic search, catalog search
10192
- **Ask**: Question-answering on knowledge bases
102-
- **Resources**: Create, read, update, delete resources
103-
- **Labels & Entities**: Manage labels and entity recognition
104-
- **Configuration**: Configure models and settings
93+
- **Search**: Full-text search, semantic search, catalog search
94+
- **List Resources**: Query and list knowledge box contents
95+
- **Suggest**: Get suggestions based on queries
10596

106-
See the [generated MCP tools](../NucliaDbClient/Generated/NucliaDbMcpTools.g.cs) for the complete list.
97+
The full NucliaDB API has 110+ operations across these tags:
98+
- Search (18 tools) ✓ Currently enabled
99+
- Resources - Resource CRUD operations
100+
- Knowledge Boxes - KB management
101+
- Models - Model configuration
102+
- Knowledge Box Services - Advanced KB services
103+
- And more...
104+
105+
See the [generated MCP tools](../NucliaDbClient/Generated/NucliaDbMcpTools.g.cs) for the current filtered list. To include different operations, see the **Tag Filtering** section in Development below.
107106

108107
## Environment Variables
109108

@@ -166,14 +165,57 @@ dotnet run --project RestClient.Net.OpenApiGenerator.Cli/RestClient.Net.OpenApiG
166165
-n NucliaDB.Generated \
167166
-c NucliaDBApiExtensions
168167

169-
# Regenerate the MCP tools
168+
# Regenerate the MCP tools with tag filtering (recommended to reduce tool count)
169+
# Only include Search-related tools (18 tools instead of 110)
170+
dotnet run --project RestClient.Net.McpGenerator.Cli/RestClient.Net.McpGenerator.Cli.csproj -- \
171+
--openapi-url Samples/NucliaDbClient/api.yaml \
172+
--output-file Samples/NucliaDbClient/Generated/NucliaDbMcpTools.g.cs \
173+
--namespace NucliaDB.Mcp \
174+
--server-name NucliaDb \
175+
--ext-namespace NucliaDB.Generated \
176+
--tags Search
177+
178+
# Or generate all tools (not recommended - creates 110 tools)
170179
dotnet run --project RestClient.Net.McpGenerator.Cli/RestClient.Net.McpGenerator.Cli.csproj -- \
171180
--openapi-url Samples/NucliaDbClient/api.yaml \
172181
--output-file Samples/NucliaDbClient/Generated/NucliaDbMcpTools.g.cs \
173182
--namespace NucliaDB.Mcp \
174183
--server-name NucliaDb \
175184
--ext-namespace NucliaDB.Generated
176185

186+
# Or include multiple tags (e.g., Search and Resources)
187+
dotnet run --project RestClient.Net.McpGenerator.Cli/RestClient.Net.McpGenerator.Cli.csproj -- \
188+
--openapi-url Samples/NucliaDbClient/api.yaml \
189+
--output-file Samples/NucliaDbClient/Generated/NucliaDbMcpTools.g.cs \
190+
--namespace NucliaDB.Mcp \
191+
--server-name NucliaDb \
192+
--ext-namespace NucliaDB.Generated \
193+
--tags "Search,Resources"
194+
177195
# Rebuild the MCP server
178196
dotnet build Samples/NucliaDbClient.McpServer
179197
```
198+
199+
### Tag Filtering
200+
201+
The NucliaDB OpenAPI spec has many operations organized by tags. To avoid flooding Claude Code with too many tools, use the `--tags` parameter to specify which sections you want:
202+
203+
**Available tags:**
204+
- `Search` - Search and ask operations (18 tools)
205+
- `Resources` - Resource CRUD operations
206+
- `Knowledge Boxes` - Knowledge box management
207+
- `Models` - Model configuration
208+
- `Knowledge Box Services` - Advanced KB services
209+
- And more...
210+
211+
**Examples:**
212+
```bash
213+
# Only search operations (recommended for most use cases)
214+
--tags Search
215+
216+
# Search and resources only
217+
--tags "Search,Resources"
218+
219+
# Multiple tags
220+
--tags "Search,Resources,Knowledge Boxes"
221+
```

0 commit comments

Comments
 (0)