Skip to content

Commit 9ff7fbf

Browse files
authored
Merge pull request #154 from joreilly/api_updates
add asset emissions tool to mcp server
2 parents e19dacc + 54feb32 commit 9ff7fbf

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

composeApp/src/commonMain/kotlin/dev/johnoreilly/climatetrace/data/ClimateTraceRepository.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class ClimateTraceRepository(
1515
return countries
1616
}
1717

18-
suspend fun fetchCountryEmissionsInfo(countryCodeList: List<String>, year: String) = api.fetchCountryEmissionsInfo(countryCodeList, year)
1918
suspend fun fetchCountryEmissionsInfo(countryCode: String, year: String) = api.fetchCountryEmissionsInfo(listOf(countryCode), year)
19+
suspend fun fetchCountryEmissionsInfo(countryCodeList: List<String>, year: String) = api.fetchCountryEmissionsInfo(countryCodeList, year)
20+
2021
suspend fun fetchCountryAssetEmissionsInfo(countryCode: String) = api.fetchCountryAssetEmissionsInfo(countryCode)
22+
suspend fun fetchCountryAssetEmissionsInfo(countryCodeList: List<String>) = api.fetchCountryAssetEmissionsInfo(countryCodeList)
2123
}

composeApp/src/commonMain/kotlin/dev/johnoreilly/climatetrace/remote/ClimateTraceApi.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ class ClimateTraceApi(
7373
// TODO need to implement paging on top of this
7474
suspend fun fetchAssets() = client.get("$baseUrl/assets").body<AssetsResult>()
7575

76-
suspend fun fetchCountryAssets(countryCode: String) = client.get("$baseUrl/assets?countries=$countryCode").body<AssetsResult>()
77-
//suspend fun fetchCountryEmissionsInfo(countryCode: String, year: String) = client.get("$baseUrl/country/emissions?since=$year&to=$year&countries=$countryCode").body<List<CountryEmissionsInfo>>()
7876

7977
suspend fun fetchCountryEmissionsInfo(countryCodeList: List<String>, year: String): List<CountryEmissionsInfo> {
8078
return client.get("$baseUrl/country/emissions") {
@@ -86,6 +84,13 @@ class ClimateTraceApi(
8684
}.body<List<CountryEmissionsInfo>>()
8785
}
8886

87+
suspend fun fetchCountryAssetEmissionsInfo(countryCodeList: List<String>): Map<String, List<CountryAssetEmissionsInfo>> {
88+
return client.get("$baseUrl/assets/emissions") {
89+
url {
90+
parameters.append("countries", countryCodeList.joinToString(","))
91+
}
92+
}.body<Map<String, List<CountryAssetEmissionsInfo>>>()
93+
}
8994

9095
suspend fun fetchCountryAssetEmissionsInfo(countryCode: String) = client.get("$baseUrl/assets/emissions?countries=$countryCode").body<Map<String, List<CountryAssetEmissionsInfo>>>()[countryCode] ?: emptyList()
9196
}

mcp-server/src/main/kotlin/server.kt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,44 @@ fun configureServer(): Server {
4646
)
4747
}
4848

49+
50+
server.addTool(
51+
name = "get-country-asset-emissions",
52+
description = "Get sector emission information for the given countries",
53+
inputSchema = Tool.Input(
54+
properties = buildJsonObject {
55+
putJsonObject("countryCodeList") {
56+
put("type", JsonPrimitive("array"))
57+
putJsonObject("items") {
58+
put("type", JsonPrimitive("string"))
59+
}
60+
}
61+
},
62+
required = listOf("countryCodeList")
63+
)
64+
) { request ->
65+
val countryCodeList = request.arguments["countryCodeList"]
66+
if (countryCodeList == null) {
67+
return@addTool CallToolResult(
68+
content = listOf(TextContent("The 'countryCodeList' parameters are required."))
69+
)
70+
}
71+
72+
val countryAssetEmissionInfo = climateTraceRepository.fetchCountryAssetEmissionsInfo(
73+
countryCodeList = countryCodeList
74+
.jsonArray
75+
.map { it.jsonPrimitive.content }
76+
)
77+
CallToolResult(
78+
content = countryAssetEmissionInfo.map { TextContent("${it.key}, ${it.value}") }
79+
)
80+
}
81+
82+
83+
4984
server.addTool(
5085
name = "get-emissions",
51-
description = "List emission info for a particular country",
86+
description = "Get total emission information for the given countries",
5287
inputSchema = Tool.Input(
5388
properties = buildJsonObject {
5489
putJsonObject("countryCodeList") {

0 commit comments

Comments
 (0)