Skip to content

Commit 347aa05

Browse files
committed
filter operations by tag
1 parent 09a550f commit 347aa05

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ Convert OpenAPI spec to MCP server, OpenAPI operations as tools
66

77
When running the MCP server, an OpenAPI spec is required. It can be either a file path or URL.
88

9-
The MCP server also provides options to filter operations to be converted as MCP tools.
9+
The MCP server also provides options to filter operations to be converted as MCP tools. Filter conditions of the same type are combined using `OR`, while filter conditions of different types are combined using 'AND'.
1010

1111
Currently supported filter conditions:
1212

13-
|Condition|CLI option|Example|
14-
|---|---|---|
15-
|HTTP method| `--include-http-method` | `--include-http-method=GET` |
16-
|Path| `--include-path` | `--include-path=/holidays` |
17-
|Operation Id| `--include-operation-id` | `--include-operation-id=Root` |
18-
13+
| Condition | CLI option | Example |
14+
|--------------|--------------------------|-------------------------------|
15+
| HTTP method | `--include-http-method` | `--include-http-method=GET` |
16+
| Path | `--include-path` | `--include-path=/holidays` |
17+
| Operation Id | `--include-operation-id` | `--include-operation-id=Root` |
18+
| Tag | `--include-tag` | `--include-tag=info` |
1919

2020
You can run the JAR file to see the CLI help.
2121

@@ -24,7 +24,8 @@ Usage: openapi-mcp [-hV] [--include-http-method=<includeHttpMethods>[,
2424
<includeHttpMethods>...]]...
2525
[--include-operation-id=<includeOperationIds>[,
2626
<includeOperationIds>...]]... [--include-path=<includePaths>
27-
[,<includePaths>...]]... <openapiSpec>
27+
[,<includePaths>...]]... [--include-tag=<includeTags>[,
28+
<includeTags>...]]... <openapiSpec>
2829
Run OpenAPI MCP server
2930
<openapiSpec> File path or URL of OpenAPI spec
3031
-h, --help Show this help message and exit.
@@ -34,10 +35,11 @@ Run OpenAPI MCP server
3435
Include operations with id (comma separated)
3536
--include-path=<includePaths>[,<includePaths>...]
3637
Include operations with paths (comma separated)
38+
--include-tag=<includeTags>[,<includeTags>...]
39+
Include operations with tags (comma separated)
3740
-V, --version Print version information and exit.
3841
```
3942

40-
4143
## How to Run
4244

4345
### Container

src/main/kotlin/com/javaaidev/mcp/openapi/Cli.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@ class Cli : Callable<Int> {
3535
)
3636
private val includePaths: List<String>? = null
3737

38+
@CommandLine.Option(
39+
names = ["--include-tag"],
40+
split = ",",
41+
description = ["Include operations with tags (comma separated)"]
42+
)
43+
private val includeTags: List<String>? = null
44+
3845
override fun call(): Int {
3946
McpServer.start(
4047
openapiSpec,
41-
OpenAPIOperationFilter(includeOperationIds, includeHttpMethods, includePaths)
48+
OpenAPIOperationFilter(
49+
includeOperationIds,
50+
includeHttpMethods,
51+
includePaths,
52+
includeTags
53+
)
4254
)
4355
return 0
4456
}

src/main/kotlin/com/javaaidev/mcp/openapi/McpTool.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ data class OpenAPIOperationFilter(
2929
val operationIds: List<String>? = null,
3030
val httpMethods: List<String>? = null,
3131
val paths: List<String>? = null,
32+
val tags: List<String>? = null,
3233
) {
3334
fun match(operation: OpenAPIOperation): Boolean {
3435
val checkers = mutableListOf<Supplier<Boolean>>()
@@ -52,6 +53,12 @@ data class OpenAPIOperationFilter(
5253
}
5354
}
5455
}
56+
tags?.let {
57+
checkers.add {
58+
!operation.operation.tags.isNullOrEmpty() &&
59+
it.intersect(operation.operation.tags).isNotEmpty()
60+
}
61+
}
5562
return checkers.all { it.get() }
5663
}
5764
}

src/test/kotlin/com/javaaidev/mcp/openapi/McpToolHelperTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,11 @@ class McpToolHelperTest {
4949
)
5050
)
5151
assertEquals(1, tools.size)
52+
tools = McpToolHelper.toTools(
53+
openAPI, OpenAPIOperationFilter(
54+
tags = listOf("holidays")
55+
)
56+
)
57+
assertEquals(2, tools.size)
5258
}
5359
}

0 commit comments

Comments
 (0)