docs: update client and server samples (#598)

Updated the client and server examples for further use in the following
guides: https://modelcontextprotocol.io/docs/develop/build-server and
https://modelcontextprotocol.io/docs/develop/build-client

## How Has This Been Tested?
claude desktop, cli

## Breaking Changes
NaN

## Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [x] Example, Documentation update

## Checklist
- [x] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [x] My code follows the repository's style guidelines
- [x] New and existing tests pass locally
- [ ] I have added appropriate error handling
- [ ] I have added or updated documentation as needed
This commit is contained in:
Pavel Gorgulov 2026-03-13 10:23:51 +01:00 • committed by GitHub
commit 921b1fd30e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 311 additions and 228 deletions

View file

@ -1,8 +1,8 @@
[versions] [versions]
anthropic = "2.9.0" anthropic = "2.15.0"
kotlin = "2.2.21" kotlin = "2.2.21"
ktor = "3.2.3" ktor = "3.2.3"
mcp-kotlin = "0.8.1" mcp-kotlin = "0.9.0"
shadow = "9.2.2" shadow = "9.2.2"
slf4j = "2.0.17" slf4j = "2.0.17"

View file

@ -2,10 +2,11 @@ package io.modelcontextprotocol.sample.client
import com.anthropic.client.okhttp.AnthropicOkHttpClient import com.anthropic.client.okhttp.AnthropicOkHttpClient
import com.anthropic.core.JsonValue import com.anthropic.core.JsonValue
import com.anthropic.models.messages.ContentBlockParam
import com.anthropic.models.messages.MessageCreateParams import com.anthropic.models.messages.MessageCreateParams
import com.anthropic.models.messages.MessageParam import com.anthropic.models.messages.MessageParam
import com.anthropic.models.messages.Model
import com.anthropic.models.messages.Tool import com.anthropic.models.messages.Tool
import com.anthropic.models.messages.ToolResultBlockParam
import com.anthropic.models.messages.ToolUnion import com.anthropic.models.messages.ToolUnion
import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
@ -14,24 +15,28 @@ import io.modelcontextprotocol.kotlin.sdk.client.StdioClientTransport
import io.modelcontextprotocol.kotlin.sdk.types.EmptyJsonObject import io.modelcontextprotocol.kotlin.sdk.types.EmptyJsonObject
import io.modelcontextprotocol.kotlin.sdk.types.Implementation import io.modelcontextprotocol.kotlin.sdk.types.Implementation
import io.modelcontextprotocol.kotlin.sdk.types.TextContent import io.modelcontextprotocol.kotlin.sdk.types.TextContent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.yield import kotlinx.coroutines.withContext
import kotlinx.io.asSink import kotlinx.io.asSink
import kotlinx.io.asSource import kotlinx.io.asSource
import kotlinx.io.buffered import kotlinx.io.buffered
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
import kotlin.jvm.optionals.getOrNull import java.util.concurrent.TimeUnit
class MCPClient : AutoCloseable { private const val MODEL = "claude-sonnet-4-20250514"
// Configures using the `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` environment variables private const val MAX_TOKENS = 1024L
private val anthropic = AnthropicOkHttpClient.fromEnv()
class MCPClient(apiKey: String) : AutoCloseable {
private val anthropic = AnthropicOkHttpClient.builder()
.apiKey(apiKey)
.build()
// Initialize MCP client // Initialize MCP client
private val mcp: Client = Client(clientInfo = Implementation(name = "mcp-client-cli", version = "1.0.0")) private val mcp: Client = Client(clientInfo = Implementation(name = "mcp-client-cli", version = "1.0.0"))
private val messageParamsBuilder: MessageCreateParams.Builder = MessageCreateParams.builder() // Server process reference for cleanup
.model(Model.CLAUDE_4_SONNET_20250514) private var serverProcess: Process? = null
.maxTokens(1024)
// List of tools offered by the server // List of tools offered by the server
private lateinit var tools: List<ToolUnion> private lateinit var tools: List<ToolUnion>
@ -44,52 +49,52 @@ class MCPClient : AutoCloseable {
// Connect to the server using the path to the server // Connect to the server using the path to the server
suspend fun connectToServer(serverScriptPath: String) { suspend fun connectToServer(serverScriptPath: String) {
try { // Build the command based on the file extension of the server script
// Build the command based on the file extension of the server script val command = buildList {
val command = buildList { when (serverScriptPath.substringAfterLast(".")) {
when (serverScriptPath.substringAfterLast(".")) { "js" -> add("node")
"js" -> add("node") "py" -> add(if (System.getProperty("os.name").lowercase().contains("win")) "python" else "python3")
"py" -> add(if (System.getProperty("os.name").lowercase().contains("win")) "python" else "python3") "jar" -> addAll(listOf("java", "-jar"))
"jar" -> addAll(listOf("java", "-jar")) else -> throw IllegalArgumentException("Server script must be a .js, .py or .jar file")
else -> throw IllegalArgumentException("Server script must be a .js, .py or .jar file")
}
add(serverScriptPath)
} }
add(serverScriptPath)
// Start the server process
val process = ProcessBuilder(command).start()
// Setup I/O transport using the process streams
val transport = StdioClientTransport(
input = process.inputStream.asSource().buffered(),
output = process.outputStream.asSink().buffered(),
)
// Connect the MCP client to the server using the transport
mcp.connect(transport)
// Request the list of available tools from the server
val toolsResult = mcp.listTools()
tools = toolsResult.tools.map { tool ->
ToolUnion.ofTool(
Tool.builder()
.name(tool.name)
.description(tool.description ?: "")
.inputSchema(
Tool.InputSchema.builder()
.type(JsonValue.from(tool.inputSchema.type))
.properties(tool.inputSchema.properties?.toJsonValue() ?: EmptyJsonObject.toJsonValue())
.putAdditionalProperty("required", JsonValue.from(tool.inputSchema.required))
.build(),
)
.build(),
)
}
println("Connected to server with tools: ${tools.joinToString(", ") { it.tool().get().name() }}")
} catch (e: Exception) {
println("Failed to connect to MCP server: $e")
throw e
} }
// Start the server process
val process = withContext(Dispatchers.IO) {
ProcessBuilder(command)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start()
}
serverProcess = process
// Setup I/O transport using the process streams
val transport = StdioClientTransport(
input = process.inputStream.asSource().buffered(),
output = process.outputStream.asSink().buffered(),
)
// Connect the MCP client to the server using the transport
mcp.connect(transport)
// Request the list of available tools from the server
val toolsResult = mcp.listTools()
tools = toolsResult.tools.map { tool ->
ToolUnion.ofTool(
Tool.builder()
.name(tool.name)
.description(tool.description ?: "")
.inputSchema(
Tool.InputSchema.builder()
.type(JsonValue.from(tool.inputSchema.type))
.properties(tool.inputSchema.properties?.toJsonValue() ?: EmptyJsonObject.toJsonValue())
.putAdditionalProperty("required", JsonValue.from(tool.inputSchema.required))
.build(),
)
.build(),
)
}
println("Connected to server with tools: ${tools.joinToString(", ") { it.tool().get().name() }}")
} }
// Process a user query and return a string response // Process a user query and return a string response
@ -104,23 +109,29 @@ class MCPClient : AutoCloseable {
// Send the query to the Anthropic model and get the response // Send the query to the Anthropic model and get the response
val response = anthropic.messages().create( val response = anthropic.messages().create(
messageParamsBuilder MessageCreateParams.builder()
.model(MODEL)
.maxTokens(MAX_TOKENS)
.messages(messages) .messages(messages)
.tools(tools) .tools(tools)
.build(), .build(),
) )
val finalText = mutableListOf<String>() val finalText = mutableListOf<String>()
val toolResults = mutableListOf<ContentBlockParam>()
response.content().forEach { content -> response.content().forEach { content ->
when { when {
// Append text outputs from the response // Append text outputs from the response
content.isText() -> finalText.add(content.text().getOrNull()?.text() ?: "") content.isText() -> finalText.add(content.text().get().text())
// If the response indicates a tool use, process it further // If the response indicates a tool use, process it further
content.isToolUse() -> { content.isToolUse() -> {
val toolName = content.toolUse().get().name() val toolUse = content.toolUse().get()
val toolName = toolUse.name()
val toolUseId = toolUse.id()
val toolArgs = val toolArgs =
content.toolUse().get()._input().convert(object : TypeReference<Map<String, JsonValue>>() {}) toolUse._input().convert(object : TypeReference<Map<String, JsonValue>>() {})
// Call the tool with provided arguments // Call the tool with provided arguments
val result = mcp.callTool( val result = mcp.callTool(
@ -129,37 +140,55 @@ class MCPClient : AutoCloseable {
) )
finalText.add("[Calling tool $toolName with args $toolArgs]") finalText.add("[Calling tool $toolName with args $toolArgs]")
// Add the tool result message to the conversation // Build tool_result content block with tool_use_id
messages.add( val toolResultContent = result.content
MessageParam.builder() .filterIsInstance<TextContent>()
.role(MessageParam.Role.USER) .joinToString("\n") { it.text }
.content(
"""
"type": "tool_result",
"tool_name": $toolName,
"result": ${
result.content.joinToString("\n") {
(it as TextContent).text
}
}
""".trimIndent(),
)
.build(),
)
// Retrieve an updated response after tool execution val toolResultBlock = ToolResultBlockParam.builder()
val aiResponse = anthropic.messages().create( .toolUseId(toolUseId)
messageParamsBuilder .content(toolResultContent)
.messages(messages) .apply { if (result.isError == true) isError(true) }
.build(), .build()
)
// Append the updated response to final text toolResults.add(ContentBlockParam.ofToolResult(toolResultBlock))
finalText.add(aiResponse.content().first().text().getOrNull()?.text() ?: "")
} }
} }
} }
// If there were tool calls, send tool results back and get final response
if (toolResults.isNotEmpty()) {
// Add the full assistant response (includes tool_use blocks)
messages.add(
MessageParam.builder()
.role(MessageParam.Role.ASSISTANT)
.contentOfBlockParams(response.content().map { it.toParam() })
.build(),
)
// Add user message with tool results
messages.add(
MessageParam.builder()
.role(MessageParam.Role.USER)
.contentOfBlockParams(toolResults)
.build(),
)
// Retrieve an updated response after tool execution (without tools)
val aiResponse = anthropic.messages().create(
MessageCreateParams.builder()
.model(MODEL)
.maxTokens(MAX_TOKENS)
.messages(messages)
.build(),
)
// Append the updated response to final text
aiResponse.content()
.filter { it.isText() }
.forEach { finalText.add(it.text().get().text()) }
}
return finalText.joinToString("\n", prefix = "", postfix = "") return finalText.joinToString("\n", prefix = "", postfix = "")
} }
@ -171,17 +200,27 @@ class MCPClient : AutoCloseable {
while (true) { while (true) {
print("\nQuery: ") print("\nQuery: ")
val message = readlnOrNull() ?: break val message = readlnOrNull() ?: break
if (message.lowercase() == "quit") break if (message.trim().lowercase() == "quit") break
val response = processQuery(message)
println("\n$response") try {
yield() val response = processQuery(message)
println("\n$response")
} catch (e: Exception) {
println("\nError: ${e.message}")
}
} }
} }
override fun close() { override fun close() {
runBlocking { runBlocking {
mcp.close() mcp.close()
anthropic.close()
} }
serverProcess?.let { process ->
process.destroy()
if (!process.waitFor(5, TimeUnit.SECONDS)) {
process.destroyForcibly()
}
}
anthropic.close()
} }
} }

View file

@ -3,13 +3,14 @@ package io.modelcontextprotocol.sample.client
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
fun main(args: Array<String>) = runBlocking { fun main(args: Array<String>) = runBlocking {
require(args.isNotEmpty()) { require(args.isNotEmpty()) { "Usage: java -jar <path> <path_to_server_script>" }
"Usage: java -jar <your_path>/build/libs/kotlin-mcp-client-0.1.0-all.jar <path_to_server_script>"
} val apiKey = System.getenv("ANTHROPIC_API_KEY")
val serverPath = args.first() require(!apiKey.isNullOrBlank()) { "ANTHROPIC_API_KEY environment variable is not set" }
val client = MCPClient()
val client = MCPClient(apiKey)
client.use { client.use {
client.connectToServer(serverPath) client.connectToServer(args.first())
client.chatLoop() client.chatLoop()
} }
} }

View file

@ -57,8 +57,8 @@ Add the following to your Claude Desktop configuration:
| Name | Description | | Name | Description |
|----------------|------------------------------------------------------------------------------------------| |----------------|------------------------------------------------------------------------------------------|
| `get_forecast` | Returns weather forecast for a given `latitude` / `longitude` using the weather.gov API. |
| `get_alerts` | Returns active weather alerts for a two-letter US `state` code (e.g. `CA`, `NY`). | | `get_alerts` | Returns active weather alerts for a two-letter US `state` code (e.g. `CA`, `NY`). |
| `get_forecast` | Returns weather forecast for a given `latitude` / `longitude` using the weather.gov API. |
## Additional Resources ## Additional Resources

View file

@ -14,14 +14,12 @@ version = "0.1.0"
dependencies { dependencies {
implementation(dependencies.platform(libs.ktor.bom)) implementation(dependencies.platform(libs.ktor.bom))
implementation(libs.mcp.kotlin.server)
implementation(libs.ktor.client.content.negotiation) implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json) implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.mcp.kotlin.server)
implementation(libs.ktor.server.cio)
implementation(libs.ktor.client.cio) implementation(libs.ktor.client.cio)
implementation(libs.slf4j.simple) implementation(libs.slf4j.simple)
runtimeOnly(libs.kotlin.logging) runtimeOnly(libs.kotlin.logging)
runtimeOnly(libs.kotlinx.collections.immutable)
testImplementation(kotlin("test")) testImplementation(kotlin("test"))

View file

@ -1,22 +1,19 @@
[versions] [versions]
collections-immutable = "0.4.0"
coroutines = "1.10.2" coroutines = "1.10.2"
kotlin = "2.2.21" kotlin = "2.2.21"
ktor = "3.2.3" ktor = "3.2.3"
logging = "7.0.13" logging = "7.0.13"
mcp-kotlin = "0.8.1" mcp-kotlin = "0.9.0"
shadow = "9.2.2" shadow = "9.2.2"
slf4j = "2.0.17" slf4j = "2.0.17"
[libraries] [libraries]
kotlin-logging = { group = "io.github.oshai", name = "kotlin-logging-jvm", version.ref = "logging" } kotlin-logging = { group = "io.github.oshai", name = "kotlin-logging-jvm", version.ref = "logging" }
kotlinx-collections-immutable = { group = "org.jetbrains.kotlinx", name = "kotlinx-collections-immutable-jvm", version.ref = "collections-immutable" }
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" } kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" }
ktor-bom = { group = "io.ktor", name = "ktor-bom", version.ref = "ktor" } ktor-bom = { group = "io.ktor", name = "ktor-bom", version.ref = "ktor" }
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio" } ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio" }
ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation" } ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation" }
ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json-jvm" } ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json-jvm" }
ktor-server-cio = { group = "io.ktor", name = "ktor-server-cio", version.ref = "ktor" }
mcp-kotlin-client = { group = "io.modelcontextprotocol", name = "kotlin-sdk-client", version.ref = "mcp-kotlin" } mcp-kotlin-client = { group = "io.modelcontextprotocol", name = "kotlin-sdk-client", version.ref = "mcp-kotlin" }
mcp-kotlin-server = { group = "io.modelcontextprotocol", name = "kotlin-sdk-server", version.ref = "mcp-kotlin" } mcp-kotlin-server = { group = "io.modelcontextprotocol", name = "kotlin-sdk-server", version.ref = "mcp-kotlin" }
slf4j-simple = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4j" } slf4j-simple = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4j" }

View file

@ -1,6 +1,7 @@
package io.modelcontextprotocol.sample.server package io.modelcontextprotocol.sample.server
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.defaultRequest import io.ktor.client.plugins.defaultRequest
import io.ktor.http.ContentType import io.ktor.http.ContentType
@ -15,6 +16,7 @@ import io.modelcontextprotocol.kotlin.sdk.types.CallToolResult
import io.modelcontextprotocol.kotlin.sdk.types.Implementation import io.modelcontextprotocol.kotlin.sdk.types.Implementation
import io.modelcontextprotocol.kotlin.sdk.types.ServerCapabilities import io.modelcontextprotocol.kotlin.sdk.types.ServerCapabilities
import io.modelcontextprotocol.kotlin.sdk.types.TextContent import io.modelcontextprotocol.kotlin.sdk.types.TextContent
import io.modelcontextprotocol.kotlin.sdk.types.ToolAnnotations
import io.modelcontextprotocol.kotlin.sdk.types.ToolSchema import io.modelcontextprotocol.kotlin.sdk.types.ToolSchema
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -32,47 +34,59 @@ import kotlinx.serialization.json.putJsonObject
* weather alerts by state and weather forecasts by latitude/longitude. * weather alerts by state and weather forecasts by latitude/longitude.
*/ */
fun runMcpServer() { fun runMcpServer() {
// Base URL for the Weather API createHttpClient().use { httpClient ->
val baseUrl = "https://api.weather.gov" val server = Server(
Implementation(
name = "weather",
version = "1.0.0",
),
ServerOptions(
capabilities = ServerCapabilities(tools = ServerCapabilities.Tools(listChanged = true)),
),
)
// Create an HTTP client with a default request configuration and JSON content negotiation server.registerTools(httpClient)
val httpClient = HttpClient {
defaultRequest { val transport = StdioServerTransport(
url(baseUrl) System.`in`.asInput(),
headers { System.out.asSink().buffered(),
append("Accept", "application/geo+json") )
append("User-Agent", "WeatherApiClient/1.0")
runBlocking {
val session = server.createSession(transport)
val done = Job()
session.onClose {
done.complete()
} }
contentType(ContentType.Application.Json) done.join()
}
// Install content negotiation plugin for JSON serialization/deserialization
install(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
prettyPrint = true
},
)
} }
} }
}
// Create the MCP Server instance with a basic implementation private fun createHttpClient(): HttpClient = HttpClient(CIO) {
val server = Server( defaultRequest {
Implementation( url("https://api.weather.gov")
name = "weather", // Tool name is "weather" headers {
version = "1.0.0", // Version of the implementation append("Accept", "application/geo+json")
), append("User-Agent", "WeatherApiClient/1.0")
ServerOptions( }
capabilities = ServerCapabilities(tools = ServerCapabilities.Tools(listChanged = true)), contentType(ContentType.Application.Json)
), }
) install(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
prettyPrint = true
},
)
}
}
private fun Server.registerTools(httpClient: HttpClient) {
// Register a tool to fetch weather alerts by state // Register a tool to fetch weather alerts by state
server.addTool( addTool(
name = "get_alerts", name = "get_alerts",
description = """ description = "Get weather alerts for a US state. Input is a two-letter US state code (e.g. CA, NY)",
Get weather alerts for a US state. Input is Two-letter US state code (e.g. CA, NY)
""".trimIndent(),
inputSchema = ToolSchema( inputSchema = ToolSchema(
properties = buildJsonObject { properties = buildJsonObject {
putJsonObject("state") { putJsonObject("state") {
@ -82,33 +96,62 @@ fun runMcpServer() {
}, },
required = listOf("state"), required = listOf("state"),
), ),
toolAnnotations = ToolAnnotations(readOnlyHint = true, openWorldHint = true),
) { request -> ) { request ->
val state = request.arguments?.get("state")?.jsonPrimitive?.content ?: return@addTool CallToolResult( val state = request.arguments?.get("state")?.jsonPrimitive?.content
content = listOf(TextContent("The 'state' parameter is required.")), ?: return@addTool CallToolResult(
content = listOf(TextContent("The 'state' parameter is required.")),
)
if (state.length != 2) {
return@addTool CallToolResult(
content = listOf(TextContent("Invalid state code: '$state'. Must be a two-letter US state code.")),
isError = true,
)
}
val stateCode = state.uppercase()
httpClient.getAlerts(stateCode).fold(
onSuccess = { alerts ->
if (alerts.isEmpty()) {
CallToolResult(content = listOf(TextContent("No active alerts for $stateCode")))
} else {
val alertsText = "Active alerts for $stateCode:\n\n${alerts.joinToString("\n---\n")}"
CallToolResult(content = listOf(TextContent(alertsText)))
}
},
onFailure = { e ->
CallToolResult(
content = listOf(TextContent("Failed to retrieve alerts data: ${e.message}")),
isError = true,
)
},
) )
val alerts = httpClient.getAlerts(state)
CallToolResult(content = alerts.map { TextContent(it) })
} }
// Register a tool to fetch weather forecast by latitude and longitude // Register a tool to fetch weather forecast by latitude and longitude
server.addTool( addTool(
name = "get_forecast", name = "get_forecast",
description = """ description = "Get weather forecast for a location. Note: only US locations are supported by the NWS API.",
Get weather forecast for a specific latitude/longitude
""".trimIndent(),
inputSchema = ToolSchema( inputSchema = ToolSchema(
properties = buildJsonObject { properties = buildJsonObject {
putJsonObject("latitude") { putJsonObject("latitude") {
put("type", "number") put("type", "number")
put("description", "Latitude of the location")
put("minimum", -90)
put("maximum", 90)
} }
putJsonObject("longitude") { putJsonObject("longitude") {
put("type", "number") put("type", "number")
put("description", "Longitude of the location")
put("minimum", -180)
put("maximum", 180)
} }
}, },
required = listOf("latitude", "longitude"), required = listOf("latitude", "longitude"),
), ),
toolAnnotations = ToolAnnotations(readOnlyHint = true, openWorldHint = true),
) { request -> ) { request ->
val latitude = request.arguments?.get("latitude")?.jsonPrimitive?.doubleOrNull val latitude = request.arguments?.get("latitude")?.jsonPrimitive?.doubleOrNull
val longitude = request.arguments?.get("longitude")?.jsonPrimitive?.doubleOrNull val longitude = request.arguments?.get("longitude")?.jsonPrimitive?.doubleOrNull
@ -118,23 +161,26 @@ fun runMcpServer() {
) )
} }
val forecast = httpClient.getForecast(latitude, longitude) httpClient.getForecast(latitude, longitude).fold(
onSuccess = { periods ->
CallToolResult(content = forecast.map { TextContent(it) }) if (periods.isEmpty()) {
} CallToolResult(content = listOf(TextContent("No forecast periods available")))
} else {
// Create a transport using standard IO for server communication val forecastText = periods.joinToString("\n---\n")
val transport = StdioServerTransport( CallToolResult(content = listOf(TextContent(forecastText)))
System.`in`.asInput(), }
System.out.asSink().buffered(), },
) onFailure = { _ ->
CallToolResult(
runBlocking { content = listOf(
val session = server.createSession(transport) TextContent(
val done = Job() "Failed to retrieve grid point data for coordinates: $latitude, $longitude. " +
session.onClose { "This location may not be supported by the NWS API (only US locations are supported).",
done.complete() ),
} ),
done.join() isError = true,
)
},
)
} }
} }

View file

@ -4,88 +4,90 @@ import io.ktor.client.HttpClient
import io.ktor.client.call.body import io.ktor.client.call.body
import io.ktor.client.request.get import io.ktor.client.request.get
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject import java.util.Locale
// Extension function to fetch forecast information for given latitude and longitude
suspend fun HttpClient.getForecast(latitude: Double, longitude: Double): List<String> {
// Build the URI using provided latitude and longitude
val uri = "/points/$latitude,$longitude"
// Request the points data from the API
val points = this.get(uri).body<Points>()
// Request the forecast using the URL provided in the points response
val forecast = this.get(points.properties.forecast).body<Forecast>()
// Map each forecast period to a formatted string
return forecast.properties.periods.map { period ->
"""
${period.name}:
Temperature: ${period.temperature} ${period.temperatureUnit}
Wind: ${period.windSpeed} ${period.windDirection}
Forecast: ${period.detailedForecast}
""".trimIndent()
}
}
// Extension function to fetch weather alerts for a given state // Extension function to fetch weather alerts for a given state
suspend fun HttpClient.getAlerts(state: String): List<String> { suspend fun HttpClient.getAlerts(state: String): Result<List<String>> = runCatching {
// Build the URI using the given state code
val uri = "/alerts/active/area/$state" val uri = "/alerts/active/area/$state"
// Request the alerts data from the API val alerts = this.get(uri).body<AlertsResponse>()
val alerts = this.get(uri).body<Alert>()
// Map each alert feature to a formatted string alerts.features.map { feature ->
return alerts.features.map { feature ->
""" """
Event: ${feature.properties.event} Event: ${feature.properties.event}
Area: ${feature.properties.areaDesc} Area: ${feature.properties.areaDesc}
Severity: ${feature.properties.severity} Severity: ${feature.properties.severity}
Description: ${feature.properties.description} Status: ${feature.properties.status}
Instruction: ${feature.properties.instruction} Headline: ${feature.properties.headline}
""".trimIndent() """.trimIndent()
} }
} }
// Data class representing the points response from the API // Extension function to fetch forecast information for given latitude and longitude
@Serializable suspend fun HttpClient.getForecast(latitude: Double, longitude: Double): Result<List<String>> = runCatching {
data class Points( val lat = String.format(Locale.US, "%.4f", latitude)
val properties: Properties val lon = String.format(Locale.US, "%.4f", longitude)
) { val uri = "/points/$lat,$lon"
@Serializable val points = this.get(uri).body<PointsResponse>()
data class Properties(val forecast: String)
val forecastUrl = points.properties.forecast
?: error("No forecast URL available for coordinates: $latitude, $longitude")
val forecast = this.get(forecastUrl).body<ForecastResponse>()
forecast.properties.periods.map { period ->
"""
${period.name}:
Temperature: ${period.temperature}°${period.temperatureUnit}
Wind: ${period.windSpeed} ${period.windDirection}
${period.shortForecast}
""".trimIndent()
}
} }
// Data class representing the forecast response from the API
@Serializable @Serializable
data class Forecast( data class PointsResponse(
val properties: Properties val properties: PointsProperties,
) { )
@Serializable
data class Properties(val periods: List<Period>)
@Serializable
data class Period(
val number: Int, val name: String, val startTime: String, val endTime: String,
val isDaytime: Boolean, val temperature: Int, val temperatureUnit: String,
val temperatureTrend: String, val probabilityOfPrecipitation: JsonObject,
val windSpeed: String, val windDirection: String,
val shortForecast: String, val detailedForecast: String,
)
}
// Data class representing the alerts response from the API
@Serializable @Serializable
data class Alert( data class PointsProperties(
val features: List<Feature> val forecast: String? = null,
) { )
@Serializable
data class Feature(
val properties: Properties
)
@Serializable @Serializable
data class Properties( data class ForecastResponse(
val event: String, val areaDesc: String, val severity: String, val properties: ForecastProperties,
val description: String, val instruction: String?, )
)
} @Serializable
data class ForecastProperties(
val periods: List<ForecastPeriod> = emptyList(),
)
@Serializable
data class ForecastPeriod(
val name: String? = null,
val temperature: Int? = null,
val temperatureUnit: String? = null,
val windSpeed: String? = null,
val windDirection: String? = null,
val shortForecast: String? = null,
)
@Serializable
data class AlertsResponse(
val features: List<AlertFeature> = emptyList(),
)
@Serializable
data class AlertFeature(
val properties: AlertProperties,
)
@Serializable
data class AlertProperties(
val event: String? = null,
val areaDesc: String? = null,
val severity: String? = null,
val status: String? = null,
val headline: String? = null,
)

View file

@ -13,8 +13,8 @@ fun main(): Unit = runBlocking {
val process = ProcessBuilder( val process = ProcessBuilder(
"java", "java",
"-jar", "-jar",
"${System.getProperty("user.dir")}/build/libs/weather-stdio-server-0.1.0-all.jar", "${System.getProperty("user.dir")}/samples/weather-stdio-server/build/libs/weather-stdio-server-0.1.0-all.jar",
).redirectErrorStream(true) ).redirectError(ProcessBuilder.Redirect.INHERIT)
.start() .start()
val transport = StdioClientTransport( val transport = StdioClientTransport(
@ -44,7 +44,7 @@ fun main(): Unit = runBlocking {
val alertResult = val alertResult =
client.callTool( client.callTool(
name = "get_alert", name = "get_alerts",
arguments = mapOf("state" to "TX"), arguments = mapOf("state" to "TX"),
).content.map { if (it is TextContent) it.text else it.toString() } ).content.map { if (it is TextContent) it.text else it.toString() }