Skip to content

Commit 84ab5b4

Browse files
committed
add headers and query params
1 parent adfdfab commit 84ab5b4

File tree

6 files changed

+83
-22
lines changed

6 files changed

+83
-22
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ Currently supported filter conditions:
2222
You can run the JAR file to see the CLI help.
2323

2424
```
25-
Usage: openapi-mcp [-hV] [--include-http-method=<includeHttpMethods>[,
25+
Usage: openapi-mcp [-hV] [--header=<headers>[,<headers>...]]...
26+
[--include-http-method=<includeHttpMethods>[,
2627
<includeHttpMethods>...]]...
2728
[--include-operation-id=<includeOperationIds>[,
2829
<includeOperationIds>...]]... [--include-path=<includePaths>
2930
[,<includePaths>...]]... [--include-tag=<includeTags>[,
30-
<includeTags>...]]... <openapiSpec>
31+
<includeTags>...]]... [--query-param=<queryParams>[,
32+
<queryParams>...]]... <openapiSpec>
3133
Run OpenAPI MCP server
3234
<openapiSpec> File path or URL of OpenAPI spec
3335
-h, --help Show this help message and exit.
36+
--header=<headers>[,<headers>...]
37+
Headers (comma separated with format a=b)
3438
--include-http-method=<includeHttpMethods>[,<includeHttpMethods>...]
3539
Include operations with HTTP methods (comma separated)
3640
--include-operation-id=<includeOperationIds>[,<includeOperationIds>...]
@@ -39,6 +43,8 @@ Run OpenAPI MCP server
3943
Include operations with paths (comma separated)
4044
--include-tag=<includeTags>[,<includeTags>...]
4145
Include operations with tags (comma separated)
46+
--query-param=<queryParams>[,<queryParams>...]
47+
Query params (comma separated with format a=b)
4248
-V, --version Print version information and exit.
4349
```
4450

@@ -49,7 +55,7 @@ Run OpenAPI MCP server
4955
Download latest [release JAR file](https://github.com/JavaAIDev/openapi-mcp-server/releases) and run it using `java -jar`.
5056

5157
```sh
52-
java -jar openapi-mcp-server-0.1.3-all.jar https://api.apis.guru/v2/specs/exchangerate-api.com/4/openapi.json
58+
java -jar openapi-mcp-server-0.1.4-all.jar https://api.apis.guru/v2/specs/exchangerate-api.com/4/openapi.json
5359
```
5460

5561
### Container

build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ plugins {
55
}
66

77
group = "com.javaaidev"
8-
version = "0.1.3"
8+
version = "0.1.4"
99

10-
val mcpVersion = "0.7.0"
10+
val mcpVersion = "0.7.2"
1111
val slf4jVersion = "2.0.17"
1212
val logbackVersion = "1.5.18"
13-
val ktorVersion = "3.3.0"
13+
val ktorVersion = "3.3.1"
1414
val picocliVersion = "4.7.7"
1515
val swaggerParserVersion = "2.1.33"
1616

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import kotlin.system.exitProcess
77
@CommandLine.Command(
88
name = "openapi-mcp",
99
mixinStandardHelpOptions = true,
10-
version = ["0.1.3"],
10+
version = ["0.1.4"],
1111
description = ["Run OpenAPI MCP server"],
1212
)
1313
class Cli : Callable<Int> {
@@ -42,6 +42,20 @@ class Cli : Callable<Int> {
4242
)
4343
private val includeTags: List<String>? = null
4444

45+
@CommandLine.Option(
46+
names = ["--header"],
47+
split = ",",
48+
description = ["Headers (comma separated with format a=b)"]
49+
)
50+
private val headers: List<String>? = null
51+
52+
@CommandLine.Option(
53+
names = ["--query-param"],
54+
split = ",",
55+
description = ["Query params (comma separated with format a=b)"]
56+
)
57+
private val queryParams: List<String>? = null
58+
4559
override fun call(): Int {
4660
McpServer.start(
4761
openapiSpec,
@@ -50,10 +64,16 @@ class Cli : Callable<Int> {
5064
includeHttpMethods,
5165
includePaths,
5266
includeTags
53-
)
67+
),
68+
parseToMap(queryParams),
69+
parseToMap(headers),
5470
)
5571
return 0
5672
}
73+
74+
private fun parseToMap(values: List<String>?) = values?.map {
75+
it.split("=")
76+
}?.filter { it.size == 2 }?.associate { it[0] to it[1] }
5777
}
5878

5979
fun main(args: Array<String>) {

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ object McpToolHelper {
9494

9595
fun toTools(
9696
openAPI: OpenAPI,
97-
openAPIOperationFilter: OpenAPIOperationFilter? = null
97+
openAPIOperationFilter: OpenAPIOperationFilter? = null,
98+
queryParams: Map<String, String>? = null,
99+
headers: Map<String, String>? = null,
98100
): List<McpTool> {
99101
logger.info("Create tools with filter {}", openAPIOperationFilter)
100102
val serverUrl = openAPI.servers.first().url
@@ -114,7 +116,15 @@ object McpToolHelper {
114116
logger.info("Use {} operations after filtering", it.size)
115117
}
116118
return operations?.map {
117-
toTool(serverUrl, it.operation, it.httpMethod, it.path, components)
119+
toTool(
120+
serverUrl,
121+
it.operation,
122+
it.httpMethod,
123+
it.path,
124+
components,
125+
queryParams,
126+
headers
127+
)
118128
} ?: listOf()
119129
}
120130

@@ -123,7 +133,9 @@ object McpToolHelper {
123133
operation: Operation,
124134
httpMethod: String,
125135
path: String,
126-
components: Map<String, Schema<*>>?
136+
components: Map<String, Schema<*>>?,
137+
queryParams: Map<String, String>? = null,
138+
headers: Map<String, String>? = null,
127139
): McpTool {
128140
val name = sanitizeToolName(operation.operationId ?: "${httpMethod}_$path")
129141
val description = operation.description ?: (operation.summary ?: "")
@@ -174,7 +186,7 @@ object McpToolHelper {
174186
)
175187
val urlTemplate = serverUrl.removeSuffix("/") + "/" + path.removePrefix("/")
176188
return McpTool(tool) { request ->
177-
callApi(urlTemplate, httpMethod, request)
189+
callApi(urlTemplate, httpMethod, request, queryParams, headers)
178190
}
179191
}
180192

@@ -189,20 +201,36 @@ object McpToolHelper {
189201
private suspend fun callApi(
190202
urlTemplate: String,
191203
httpMethod: String,
192-
request: CallToolRequest
204+
request: CallToolRequest,
205+
queryParams: Map<String, String>? = null,
206+
extraHeaders: Map<String, String>? = null,
193207
): CallToolResult {
194208
return httpClient.request {
195-
url(
196-
parseUrl(expandUriTemplate(urlTemplate, request.arguments))
197-
?: throw RuntimeException("Invalid url")
198-
)
209+
url {
210+
takeFrom(
211+
parseUrl(expandUriTemplate(urlTemplate, request.arguments))
212+
?: throw RuntimeException("Invalid url")
213+
)
214+
queryParams?.let { params ->
215+
params.forEach { (k, v) ->
216+
parameters.append(k, v)
217+
}
218+
}
219+
}
199220
method = when (httpMethod) {
200221
"PUT" -> HttpMethod.Put
201222
"POST" -> HttpMethod.Post
202223
"PATCH" -> HttpMethod.Patch
203224
"DELETE" -> HttpMethod.Delete
204225
else -> HttpMethod.Get
205226
}
227+
extraHeaders?.let {
228+
it.forEach { (k, v) ->
229+
headers.append(k, v)
230+
}
231+
}
232+
contentType(ContentType.Application.Json)
233+
setBody(request.arguments)
206234
}.bodyAsText().let { text ->
207235
CallToolResult(listOf(TextContent(text)))
208236
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ val logger: Logger = LoggerFactory.getLogger("McpServer")
1717

1818
object McpServer {
1919

20-
fun start(openapiSpec: String, openAPIOperationFilter: OpenAPIOperationFilter? = null) {
20+
fun start(
21+
openapiSpec: String,
22+
openAPIOperationFilter: OpenAPIOperationFilter? = null,
23+
queryParams: Map<String, String>? = null,
24+
headers: Map<String, String>? = null,
25+
) {
2126
logger.info("Parse OpenAPI spec {} ", openapiSpec)
2227
val openAPI = OpenAPIParser.parse(openapiSpec)
2328

@@ -34,9 +39,11 @@ object McpServer {
3439
)
3540
)
3641

37-
McpToolHelper.toTools(openAPI, openAPIOperationFilter).forEach { tool ->
38-
server.addTool(tool.tool, tool.handler)
39-
}
42+
logger.info("params: {}", queryParams)
43+
McpToolHelper.toTools(openAPI, openAPIOperationFilter, queryParams, headers)
44+
.forEach { tool ->
45+
server.addTool(tool.tool, tool.handler)
46+
}
4047

4148
val transport = StdioServerTransport(
4249
System.`in`.asInput(),

src/main/resources/logback.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<configuration>
22

3-
<property name="LOG_DIR" value="${LOG_DIR:-/var/log/app}"/>
3+
<property name="LOG_DIR" value="${LOG_DIR:-/tmp/openapi-mcp-server}"/>
44

55
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
66
<file>${LOG_DIR}/server.log</file>

0 commit comments

Comments
 (0)