-
Notifications
You must be signed in to change notification settings - Fork 78
Support for MCP definations using kotlin annotation #80
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
alpeshvas
wants to merge
7
commits into
modelcontextprotocol:main
Choose a base branch
from
alpeshvas:ft-tool-annotation
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.
+1,064
−31
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6803c11
build success
36cd51a
tests
alpeshvas 2588881
fix function invokes
alpeshvas baa84bf
fix more tests
alpeshvas eb99347
fix exception propgation
alpeshvas 479f097
fix all tests
alpeshvas d074336
Merge branch 'main' into ft-tool-annotation
alpeshvas 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
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
77 changes: 77 additions & 0 deletions
77
...server/src/main/kotlin/io/modelcontextprotocol/sample/server/AnnotatedMcpWeatherServer.kt
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.modelcontextprotocol.sample.server | ||
|
||
import io.ktor.client.* | ||
import io.ktor.client.plugins.* | ||
import io.ktor.client.plugins.contentnegotiation.* | ||
import io.ktor.http.* | ||
import io.ktor.serialization.kotlinx.json.* | ||
import io.modelcontextprotocol.kotlin.sdk.* | ||
import io.modelcontextprotocol.kotlin.sdk.server.Server | ||
import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions | ||
import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport | ||
import io.modelcontextprotocol.kotlin.sdk.server.registerAnnotatedTools | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.io.asSink | ||
import kotlinx.io.buffered | ||
import kotlinx.serialization.json.* | ||
|
||
/** | ||
* Alternative implementation of the Weather MCP server using annotations. | ||
* This demonstrates how to use @McpTool annotations to simplify tool registration. | ||
*/ | ||
fun `run annotated mcp server`() { | ||
// Base URL for the Weather API | ||
val baseUrl = "https://api.weather.gov" | ||
|
||
// Create an HTTP client with a default request configuration and JSON content negotiation | ||
val httpClient = HttpClient { | ||
defaultRequest { | ||
url(baseUrl) | ||
headers { | ||
append("Accept", "application/geo+json") | ||
append("User-Agent", "WeatherApiClient/1.0") | ||
} | ||
contentType(ContentType.Application.Json) | ||
} | ||
// Install content negotiation plugin for JSON serialization/deserialization | ||
install(ContentNegotiation) { | ||
json(Json { | ||
ignoreUnknownKeys = true | ||
prettyPrint = true | ||
}) | ||
} | ||
} | ||
|
||
// Create the MCP Server instance | ||
val server = Server( | ||
Implementation( | ||
name = "weather-annotated", | ||
version = "1.0.0" | ||
), | ||
ServerOptions( | ||
capabilities = ServerCapabilities(tools = ServerCapabilities.Tools(listChanged = true)) | ||
) | ||
) | ||
|
||
// Create an instance of our annotated tools class | ||
val weatherTools = WeatherToolsAnnotated(httpClient) | ||
|
||
// Register all annotated tools from the weatherTools instance | ||
server.registerAnnotatedTools(weatherTools) | ||
|
||
// Create a transport using standard IO for server communication | ||
val transport = StdioServerTransport( | ||
System.`in`.asInput(), | ||
System.out.asSink().buffered() | ||
) | ||
|
||
runBlocking { | ||
server.connect(transport) | ||
val done = Job() | ||
server.onClose { | ||
done.complete() | ||
} | ||
done.join() | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...dio-server/src/main/kotlin/io/modelcontextprotocol/sample/server/AnnotatedToolsExample.kt
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.modelcontextprotocol.sample.server | ||
|
||
import io.ktor.client.* | ||
import io.modelcontextprotocol.kotlin.sdk.CallToolResult | ||
import io.modelcontextprotocol.kotlin.sdk.TextContent | ||
import io.modelcontextprotocol.kotlin.sdk.server.McpParam | ||
import io.modelcontextprotocol.kotlin.sdk.server.McpTool | ||
import io.modelcontextprotocol.kotlin.sdk.server.registerAnnotatedTools | ||
|
||
/** | ||
* Example class demonstrating the use of McpTool annotations. | ||
*/ | ||
class WeatherToolsAnnotated(private val httpClient: HttpClient) { | ||
|
||
/** | ||
* Gets weather alerts for a specified US state using the @McpTool annotation. | ||
*/ | ||
@McpTool( | ||
name = "get_alerts", | ||
description = "Get weather alerts for a US state" | ||
) | ||
suspend fun getAlerts( | ||
@McpParam( | ||
description = "Two-letter US state code (e.g. CA, NY)", | ||
type = "string" | ||
) state: String | ||
): CallToolResult { | ||
if (state.isEmpty()) { | ||
return CallToolResult( | ||
content = listOf(TextContent("The 'state' parameter is required.")) | ||
) | ||
} | ||
|
||
val alerts = httpClient.getAlerts(state) | ||
return CallToolResult(content = alerts.map { TextContent(it) }) | ||
} | ||
|
||
/** | ||
* Gets weather forecast for specified coordinates using the @McpTool annotation. | ||
*/ | ||
@McpTool( | ||
name = "get_forecast", | ||
description = "Get weather forecast for a specific latitude/longitude" | ||
) | ||
suspend fun getForecast( | ||
@McpParam(description = "The latitude coordinate") latitude: Double, | ||
@McpParam(description = "The longitude coordinate") longitude: Double | ||
): CallToolResult { | ||
val forecast = httpClient.getForecast(latitude, longitude) | ||
return CallToolResult(content = forecast.map { TextContent(it) }) | ||
} | ||
|
||
/** | ||
* Gets brief weather summary using the @McpTool annotation with default name. | ||
*/ | ||
@McpTool( | ||
description = "Get a brief weather summary for a location" | ||
) | ||
suspend fun getWeatherSummary( | ||
@McpParam(description = "City name") city: String, | ||
@McpParam(description = "Temperature unit (celsius/fahrenheit)", required = false) unit: String = "celsius" | ||
): String { | ||
return "Weather summary for $city: Sunny, 25° $unit" | ||
} | ||
} |
12 changes: 11 additions & 1 deletion
12
samples/weather-stdio-server/src/main/kotlin/io/modelcontextprotocol/sample/server/main.kt
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 |
---|---|---|
@@ -1,3 +1,13 @@ | ||
package io.modelcontextprotocol.sample.server | ||
|
||
fun main() = `run mcp server`() | ||
fun main(args: Array<String>) { | ||
val useAnnotations = args.contains("--use-annotations") | ||
|
||
if (useAnnotations) { | ||
println("Starting annotated MCP Weather server...") | ||
`run annotated mcp server`() | ||
} else { | ||
println("Starting traditional MCP Weather server...") | ||
`run mcp server`() | ||
} | ||
} |
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.