@sriinnu/kosha-discovery 1.1.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/README.md +66 -26
  2. package/dist/aliases.d.ts +6 -1
  3. package/dist/aliases.d.ts.map +1 -1
  4. package/dist/aliases.js +38 -12
  5. package/dist/aliases.js.map +1 -1
  6. package/dist/cache.d.ts.map +1 -1
  7. package/dist/cache.js +25 -2
  8. package/dist/cache.js.map +1 -1
  9. package/dist/claude-generation.d.ts +59 -0
  10. package/dist/claude-generation.d.ts.map +1 -0
  11. package/dist/claude-generation.js +117 -0
  12. package/dist/claude-generation.js.map +1 -0
  13. package/dist/cli-cmd-doctor.d.ts +17 -0
  14. package/dist/cli-cmd-doctor.d.ts.map +1 -0
  15. package/dist/cli-cmd-doctor.js +199 -0
  16. package/dist/cli-cmd-doctor.js.map +1 -0
  17. package/dist/cli-cmd-model.js +2 -2
  18. package/dist/cli-cmd-model.js.map +1 -1
  19. package/dist/cli-cmd-spend.d.ts +10 -0
  20. package/dist/cli-cmd-spend.d.ts.map +1 -0
  21. package/dist/cli-cmd-spend.js +130 -0
  22. package/dist/cli-cmd-spend.js.map +1 -0
  23. package/dist/cli-commands.d.ts +4 -1
  24. package/dist/cli-commands.d.ts.map +1 -1
  25. package/dist/cli-commands.js +10 -2
  26. package/dist/cli-commands.js.map +1 -1
  27. package/dist/cli-format.d.ts.map +1 -1
  28. package/dist/cli-format.js +2 -1
  29. package/dist/cli-format.js.map +1 -1
  30. package/dist/cli-help.d.ts.map +1 -1
  31. package/dist/cli-help.js +4 -2
  32. package/dist/cli-help.js.map +1 -1
  33. package/dist/cli.js +10 -0
  34. package/dist/cli.js.map +1 -1
  35. package/dist/context-strategy.d.ts +48 -0
  36. package/dist/context-strategy.d.ts.map +1 -0
  37. package/dist/context-strategy.js +274 -0
  38. package/dist/context-strategy.js.map +1 -0
  39. package/dist/cost.d.ts +152 -0
  40. package/dist/cost.d.ts.map +1 -0
  41. package/dist/cost.js +377 -0
  42. package/dist/cost.js.map +1 -0
  43. package/dist/credentials/resolver.d.ts +17 -0
  44. package/dist/credentials/resolver.d.ts.map +1 -1
  45. package/dist/credentials/resolver.js +92 -34
  46. package/dist/credentials/resolver.js.map +1 -1
  47. package/dist/discovery/anthropic.d.ts +19 -1
  48. package/dist/discovery/anthropic.d.ts.map +1 -1
  49. package/dist/discovery/anthropic.js +112 -8
  50. package/dist/discovery/anthropic.js.map +1 -1
  51. package/dist/discovery/base.d.ts +7 -0
  52. package/dist/discovery/base.d.ts.map +1 -1
  53. package/dist/discovery/base.js +32 -4
  54. package/dist/discovery/base.js.map +1 -1
  55. package/dist/discovery/bedrock.d.ts.map +1 -1
  56. package/dist/discovery/cerebras.d.ts.map +1 -1
  57. package/dist/discovery/cohere.d.ts.map +1 -1
  58. package/dist/discovery/deepinfra.d.ts.map +1 -1
  59. package/dist/discovery/deepseek.d.ts.map +1 -1
  60. package/dist/discovery/fireworks.d.ts.map +1 -1
  61. package/dist/discovery/glm.d.ts.map +1 -1
  62. package/dist/discovery/google.d.ts +3 -1
  63. package/dist/discovery/google.d.ts.map +1 -1
  64. package/dist/discovery/google.js +13 -6
  65. package/dist/discovery/google.js.map +1 -1
  66. package/dist/discovery/groq.d.ts.map +1 -1
  67. package/dist/discovery/index.d.ts +3 -0
  68. package/dist/discovery/index.d.ts.map +1 -1
  69. package/dist/discovery/index.js +9 -0
  70. package/dist/discovery/index.js.map +1 -1
  71. package/dist/discovery/litellm-seed.js +12 -8
  72. package/dist/discovery/litellm-seed.js.map +1 -1
  73. package/dist/discovery/llama-cpp.d.ts.map +1 -1
  74. package/dist/discovery/lmstudio.d.ts +26 -0
  75. package/dist/discovery/lmstudio.d.ts.map +1 -0
  76. package/dist/discovery/lmstudio.js +93 -0
  77. package/dist/discovery/lmstudio.js.map +1 -0
  78. package/dist/discovery/minimax.d.ts.map +1 -1
  79. package/dist/discovery/mistral.d.ts.map +1 -1
  80. package/dist/discovery/modelsdev-seed.js +16 -12
  81. package/dist/discovery/modelsdev-seed.js.map +1 -1
  82. package/dist/discovery/moonshot.d.ts.map +1 -1
  83. package/dist/discovery/nvidia.d.ts.map +1 -1
  84. package/dist/discovery/ollama.d.ts.map +1 -1
  85. package/dist/discovery/openai-compatible.d.ts.map +1 -1
  86. package/dist/discovery/openai.d.ts.map +1 -1
  87. package/dist/discovery/openrouter.d.ts +29 -5
  88. package/dist/discovery/openrouter.d.ts.map +1 -1
  89. package/dist/discovery/openrouter.js +56 -18
  90. package/dist/discovery/openrouter.js.map +1 -1
  91. package/dist/discovery/perplexity.d.ts.map +1 -1
  92. package/dist/discovery/promo-overrides.js.map +1 -1
  93. package/dist/discovery/static-direct.d.ts +9 -1
  94. package/dist/discovery/static-direct.d.ts.map +1 -1
  95. package/dist/discovery/static-direct.js +68 -7
  96. package/dist/discovery/static-direct.js.map +1 -1
  97. package/dist/discovery/together.d.ts.map +1 -1
  98. package/dist/discovery/vercel.d.ts +45 -0
  99. package/dist/discovery/vercel.d.ts.map +1 -0
  100. package/dist/discovery/vercel.js +232 -0
  101. package/dist/discovery/vercel.js.map +1 -0
  102. package/dist/discovery/vertex.d.ts.map +1 -1
  103. package/dist/discovery/vllm.d.ts +26 -0
  104. package/dist/discovery/vllm.d.ts.map +1 -0
  105. package/dist/discovery/vllm.js +93 -0
  106. package/dist/discovery/vllm.js.map +1 -0
  107. package/dist/discovery/zai.d.ts.map +1 -1
  108. package/dist/discovery-contract.d.ts +8 -1
  109. package/dist/discovery-contract.d.ts.map +1 -1
  110. package/dist/discovery-contract.js +28 -1
  111. package/dist/discovery-contract.js.map +1 -1
  112. package/dist/discovery-routes.d.ts +9 -0
  113. package/dist/discovery-routes.d.ts.map +1 -1
  114. package/dist/discovery-routes.js +106 -18
  115. package/dist/discovery-routes.js.map +1 -1
  116. package/dist/enrichment/litellm-catalog.d.ts +1 -0
  117. package/dist/enrichment/litellm-catalog.d.ts.map +1 -1
  118. package/dist/enrichment/litellm-catalog.js.map +1 -1
  119. package/dist/enrichment/litellm.d.ts +1 -1
  120. package/dist/enrichment/litellm.d.ts.map +1 -1
  121. package/dist/enrichment/litellm.js +9 -3
  122. package/dist/enrichment/litellm.js.map +1 -1
  123. package/dist/entry.d.ts +14 -0
  124. package/dist/entry.d.ts.map +1 -0
  125. package/dist/entry.js +26 -0
  126. package/dist/entry.js.map +1 -0
  127. package/dist/index.d.ts +10 -0
  128. package/dist/index.d.ts.map +1 -1
  129. package/dist/index.js +5 -0
  130. package/dist/index.js.map +1 -1
  131. package/dist/mcp-server.d.ts +207 -2
  132. package/dist/mcp-server.d.ts.map +1 -1
  133. package/dist/mcp-server.js +254 -54
  134. package/dist/mcp-server.js.map +1 -1
  135. package/dist/model-features.d.ts.map +1 -1
  136. package/dist/model-features.js +11 -2
  137. package/dist/model-features.js.map +1 -1
  138. package/dist/normalize.d.ts +7 -7
  139. package/dist/normalize.js +8 -8
  140. package/dist/provider-catalog.d.ts +17 -2
  141. package/dist/provider-catalog.d.ts.map +1 -1
  142. package/dist/provider-catalog.js +112 -0
  143. package/dist/provider-catalog.js.map +1 -1
  144. package/dist/proxy.d.ts +49 -7
  145. package/dist/proxy.d.ts.map +1 -1
  146. package/dist/proxy.js +765 -80
  147. package/dist/proxy.js.map +1 -1
  148. package/dist/registry-discovery.d.ts.map +1 -1
  149. package/dist/registry-discovery.js +4 -4
  150. package/dist/registry-discovery.js.map +1 -1
  151. package/dist/registry-query.d.ts +2 -2
  152. package/dist/registry-query.d.ts.map +1 -1
  153. package/dist/registry-query.js +60 -9
  154. package/dist/registry-query.js.map +1 -1
  155. package/dist/registry-routing.d.ts +65 -0
  156. package/dist/registry-routing.d.ts.map +1 -0
  157. package/dist/registry-routing.js +165 -0
  158. package/dist/registry-routing.js.map +1 -0
  159. package/dist/registry-runtime.d.ts +2 -0
  160. package/dist/registry-runtime.d.ts.map +1 -1
  161. package/dist/registry-runtime.js +167 -15
  162. package/dist/registry-runtime.js.map +1 -1
  163. package/dist/registry-selection.d.ts.map +1 -1
  164. package/dist/registry-selection.js +27 -3
  165. package/dist/registry-selection.js.map +1 -1
  166. package/dist/registry.d.ts +69 -0
  167. package/dist/registry.d.ts.map +1 -1
  168. package/dist/registry.js +135 -3
  169. package/dist/registry.js.map +1 -1
  170. package/dist/resilience.d.ts +15 -0
  171. package/dist/resilience.d.ts.map +1 -1
  172. package/dist/resilience.js +23 -1
  173. package/dist/resilience.js.map +1 -1
  174. package/dist/security.js +6 -6
  175. package/dist/security.js.map +1 -1
  176. package/dist/server.d.ts +44 -2
  177. package/dist/server.d.ts.map +1 -1
  178. package/dist/server.js +329 -34
  179. package/dist/server.js.map +1 -1
  180. package/dist/tally.d.ts +80 -0
  181. package/dist/tally.d.ts.map +1 -0
  182. package/dist/tally.js +176 -0
  183. package/dist/tally.js.map +1 -0
  184. package/dist/types.d.ts +64 -3
  185. package/dist/types.d.ts.map +1 -1
  186. package/dist/wire-anthropic.d.ts +264 -0
  187. package/dist/wire-anthropic.d.ts.map +1 -0
  188. package/dist/wire-anthropic.js +960 -0
  189. package/dist/wire-anthropic.js.map +1 -0
  190. package/package.json +106 -97
  191. package/logo.png +0 -0
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EACN,wBAAwB,EACxB,cAAc,EACd,YAAY,EACZ,uBAAuB,EACvB,2BAA2B,GAC3B,MAAM,yBAAyB,CAAC;AAgBjC,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAC9F,OAAO,EAAE,oBAAoB,EAAE,MAAM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AAC;AAS9E,OAAO,EACN,kBAAkB,EAClB,qBAAqB,EACrB,8BAA8B,EAC9B,8BAA8B,EAC9B,qBAAqB,EACrB,qBAAqB,GACrB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACN,uBAAuB,EACvB,aAAa,EACb,gCAAgC,EAChC,0BAA0B,EAC1B,0BAA0B,EAC1B,mCAAmC,EACnC,2BAA2B,GAC3B,MAAM,qBAAqB,CAAC;AAa7B,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAC9F,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC3G,OAAO,EACN,qBAAqB,EACrB,eAAe,EACf,uBAAuB,EACvB,mBAAmB,EACnB,gBAAgB,GAChB,MAAM,uBAAuB,CAAC;AAkC/B;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,MAAyC;IAC1E,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,GAAG,MAAM,MAAM,CAAC,eAAe,CAAC,CAAC;IAClE,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IACrD,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;IACtC,MAAM,QAAQ,CAAC,QAAQ,EAAE,CAAC;IAC1B,OAAO,QAAQ,CAAC;AACjB,CAAC"}
@@ -1,18 +1,223 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * kosha-discovery — MCP server (stdio transport, protocol 2024-11-05).
3
+ * kosha-discovery — MCP server (stdio transport).
4
4
  *
5
5
  * Exposes the local kosha registry as MCP tools so AI agents can query
6
6
  * model pricing, routing, and health without HTTP.
7
7
  *
8
+ * Protocol: JSON-RPC 2.0 over newline-delimited stdio. The server speaks
9
+ * every MCP revision in {@link MCP_PROTOCOL_VERSIONS} and echoes the
10
+ * client's requested revision back when it is one of them (otherwise it
11
+ * answers with the newest it supports, per the MCP negotiation rule).
12
+ *
8
13
  * Tools:
9
14
  * kosha_query_models — list / filter models
10
15
  * kosha_cheapest_model — cheapest model meeting requirements
16
+ * kosha_ranked_routes — strategy-ranked routes (cheapest / fastest / reliable / balanced)
11
17
  * kosha_model_detail — full detail for one model
12
18
  * kosha_model_routes — all provider routes for a model
13
19
  * kosha_resolve_alias — alias → canonical ID
14
20
  * kosha_provider_health — provider auth + error status
21
+ * kosha_context_strategy — context-management advice for a long conversation
22
+ *
23
+ * The JSON-RPC handler ({@link handleJsonRpc}) is exported and pure over an
24
+ * injected registry loader so it can be unit-tested; the stdio loop only
25
+ * starts when this file is the process entry point.
15
26
  * @module
16
27
  */
17
- export {};
28
+ import { ModelRegistry } from "./registry.js";
29
+ /** Package version, read from package.json so `serverInfo.version` never drifts from the release. */
30
+ export declare const KOSHA_MCP_VERSION: string;
31
+ /** MCP protocol revisions this server implements, newest first. */
32
+ export declare const MCP_PROTOCOL_VERSIONS: readonly string[];
33
+ /**
34
+ * Pick the protocol revision to answer an `initialize` with: the client's
35
+ * own when we support it, otherwise our newest (the client then decides
36
+ * whether to proceed).
37
+ */
38
+ export declare function negotiateProtocolVersion(requested: unknown): string;
39
+ export interface JsonRpcMessage {
40
+ jsonrpc: "2.0";
41
+ id?: string | number | null;
42
+ method?: string;
43
+ params?: unknown;
44
+ result?: unknown;
45
+ error?: {
46
+ code: number;
47
+ message: string;
48
+ };
49
+ }
50
+ export declare const TOOLS: readonly [{
51
+ readonly name: "kosha_query_models";
52
+ readonly description: "List AI models from the local kosha registry. Optionally filter by provider, mode, or capability tag. Returns id, provider, name, mode, context window, capabilities, and pricing.";
53
+ readonly inputSchema: {
54
+ readonly type: "object";
55
+ readonly properties: {
56
+ readonly provider: {
57
+ readonly type: "string";
58
+ readonly description: "Provider ID (e.g. anthropic, openai, groq, openrouter)";
59
+ };
60
+ readonly mode: {
61
+ readonly type: "string";
62
+ readonly enum: readonly ["chat", "embedding", "image", "video", "audio", "moderation", "rerank"];
63
+ readonly description: "Primary model mode";
64
+ };
65
+ readonly capability: {
66
+ readonly type: "string";
67
+ readonly description: "Capability tag (e.g. vision, tool_use, code, reasoning)";
68
+ };
69
+ readonly limit: {
70
+ readonly type: "number";
71
+ readonly description: "Max results to return (default 20)";
72
+ };
73
+ };
74
+ };
75
+ }, {
76
+ readonly name: "kosha_cheapest_model";
77
+ readonly description: "Find the cheapest AI model that meets your requirements. Returns ranked matches with per-million token pricing. Use this before routing a request to pick the most cost-effective option.";
78
+ readonly inputSchema: {
79
+ readonly type: "object";
80
+ readonly properties: {
81
+ readonly capability: {
82
+ readonly type: "string";
83
+ readonly description: "Required capability (e.g. tool_use, vision, code, reasoning)";
84
+ };
85
+ readonly min_context_k: {
86
+ readonly type: "number";
87
+ readonly description: "Minimum context window in thousands of tokens (e.g. 128 means 128k)";
88
+ };
89
+ readonly provider: {
90
+ readonly type: "string";
91
+ readonly description: "Pin to a specific provider";
92
+ };
93
+ readonly limit: {
94
+ readonly type: "number";
95
+ readonly description: "Number of ranked results (default 5)";
96
+ };
97
+ };
98
+ };
99
+ }, {
100
+ readonly name: "kosha_ranked_routes";
101
+ readonly description: "Rank chat-model routes by strategy: cheapest (price only), fastest (observed p95 latency), reliable (circuit-breaker state plus timeout / auth-error history), or balanced (weighted blend of all three). Providers whose breaker is open always sort last, so the result doubles as a failover order. Accepts the same filters as kosha_cheapest_model.";
102
+ readonly inputSchema: {
103
+ readonly type: "object";
104
+ readonly properties: {
105
+ readonly strategy: {
106
+ readonly type: "string";
107
+ readonly enum: readonly ["cheapest", "fastest", "reliable", "balanced"];
108
+ readonly description: "Ranking strategy";
109
+ };
110
+ readonly capability: {
111
+ readonly type: "string";
112
+ readonly description: "Required capability tag (e.g. tool_use, vision)";
113
+ };
114
+ readonly min_context_k: {
115
+ readonly type: "number";
116
+ readonly description: "Minimum context window in thousands of tokens";
117
+ };
118
+ readonly provider: {
119
+ readonly type: "string";
120
+ readonly description: "Pin to a specific provider";
121
+ };
122
+ readonly limit: {
123
+ readonly type: "number";
124
+ readonly description: "Number of ranked results (default 5)";
125
+ };
126
+ };
127
+ readonly required: readonly ["strategy"];
128
+ };
129
+ }, {
130
+ readonly name: "kosha_model_detail";
131
+ readonly description: "Get full details for a specific model — pricing, capabilities, context window, tool dialect, structured output modes, status, and deprecation info.";
132
+ readonly inputSchema: {
133
+ readonly type: "object";
134
+ readonly properties: {
135
+ readonly model: {
136
+ readonly type: "string";
137
+ readonly description: "Model ID or alias (e.g. sonnet, claude-sonnet-5, gpt-4o, deepseek-v3)";
138
+ };
139
+ };
140
+ readonly required: readonly ["model"];
141
+ };
142
+ }, {
143
+ readonly name: "kosha_model_routes";
144
+ readonly description: "List all serving-layer routes for a model (direct provider, OpenRouter, Bedrock, Vertex, etc.) with pricing per route. Useful for finding the cheapest or most available path to a specific model.";
145
+ readonly inputSchema: {
146
+ readonly type: "object";
147
+ readonly properties: {
148
+ readonly model: {
149
+ readonly type: "string";
150
+ readonly description: "Model ID or alias";
151
+ };
152
+ };
153
+ readonly required: readonly ["model"];
154
+ };
155
+ }, {
156
+ readonly name: "kosha_resolve_alias";
157
+ readonly description: "Resolve a model alias or short name to its canonical ID and provider.";
158
+ readonly inputSchema: {
159
+ readonly type: "object";
160
+ readonly properties: {
161
+ readonly alias: {
162
+ readonly type: "string";
163
+ readonly description: "Short name or alias (e.g. sonnet, opus, haiku, gpt-4o-mini, gemini-flash)";
164
+ };
165
+ };
166
+ readonly required: readonly ["alias"];
167
+ };
168
+ }, {
169
+ readonly name: "kosha_provider_health";
170
+ readonly description: "Get authentication and health status for all discovered providers. Shows which providers are active, which need credentials, and any recent discovery errors.";
171
+ readonly inputSchema: {
172
+ readonly type: "object";
173
+ readonly properties: {
174
+ readonly provider: {
175
+ readonly type: "string";
176
+ readonly description: "Filter to a specific provider ID";
177
+ };
178
+ };
179
+ };
180
+ }, {
181
+ readonly name: "kosha_context_strategy";
182
+ readonly description: "Advise on context management for a long-running conversation. Given a model and current token usage, returns ranked options (continue, enable prompt cache, compact + continue, switch to long-context tier, switch model, batch offload) with rough cost-per-turn math. Useful before deciding whether to summarize, swap models, or just keep going.";
183
+ readonly inputSchema: {
184
+ readonly type: "object";
185
+ readonly properties: {
186
+ readonly model: {
187
+ readonly type: "string";
188
+ readonly description: "Model ID or alias the caller is currently using";
189
+ };
190
+ readonly current_tokens: {
191
+ readonly type: "number";
192
+ readonly description: "Approximate token count of the conversation so far";
193
+ };
194
+ readonly expected_output_tokens: {
195
+ readonly type: "number";
196
+ readonly description: "Tokens the next reply is expected to produce (default 1024)";
197
+ };
198
+ readonly expected_remaining_turns: {
199
+ readonly type: "number";
200
+ readonly description: "How many more turns the caller plans (default 5)";
201
+ };
202
+ };
203
+ readonly required: readonly ["model", "current_tokens"];
204
+ };
205
+ }];
206
+ /** Dependencies the JSON-RPC handler needs; injected so tests can supply a fixture registry. */
207
+ export interface McpDeps {
208
+ registry: () => Promise<ModelRegistry>;
209
+ }
210
+ export declare function callTool(name: string, args: Record<string, unknown>, deps?: McpDeps): Promise<unknown>;
211
+ /**
212
+ * Handle one JSON-RPC request and return the response, or `null` for
213
+ * notifications (which carry no id and expect no reply).
214
+ *
215
+ * Tool *execution* failures come back as a successful `tools/call` result
216
+ * with `isError: true`, per the MCP spec, so the model can read the message
217
+ * and recover; only protocol-level problems (unknown method, unknown tool,
218
+ * malformed params) surface as JSON-RPC errors.
219
+ */
220
+ export declare function handleJsonRpc(msg: JsonRpcMessage, deps?: McpDeps): Promise<JsonRpcMessage | null>;
221
+ /** Start the newline-delimited JSON-RPC loop over stdio. */
222
+ export declare function runStdioServer(deps?: McpDeps): void;
18
223
  //# sourceMappingURL=mcp-server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mcp-server.d.ts","sourceRoot":"","sources":["../src/mcp-server.ts"],"names":[],"mappings":";AACA;;;;;;;;;;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,IAAI,GAAE,OAAqB,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CA4CpH;AAMD,4DAA4D;AAC5D,wBAAgB,cAAc,CAAC,IAAI,GAAE,OAAqB,GAAG,IAAI,CA4ChE"}
@@ -1,24 +1,71 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * kosha-discovery — MCP server (stdio transport, protocol 2024-11-05).
3
+ * kosha-discovery — MCP server (stdio transport).
4
4
  *
5
5
  * Exposes the local kosha registry as MCP tools so AI agents can query
6
6
  * model pricing, routing, and health without HTTP.
7
7
  *
8
+ * Protocol: JSON-RPC 2.0 over newline-delimited stdio. The server speaks
9
+ * every MCP revision in {@link MCP_PROTOCOL_VERSIONS} and echoes the
10
+ * client's requested revision back when it is one of them (otherwise it
11
+ * answers with the newest it supports, per the MCP negotiation rule).
12
+ *
8
13
  * Tools:
9
14
  * kosha_query_models — list / filter models
10
15
  * kosha_cheapest_model — cheapest model meeting requirements
16
+ * kosha_ranked_routes — strategy-ranked routes (cheapest / fastest / reliable / balanced)
11
17
  * kosha_model_detail — full detail for one model
12
18
  * kosha_model_routes — all provider routes for a model
13
19
  * kosha_resolve_alias — alias → canonical ID
14
20
  * kosha_provider_health — provider auth + error status
21
+ * kosha_context_strategy — context-management advice for a long conversation
22
+ *
23
+ * The JSON-RPC handler ({@link handleJsonRpc}) is exported and pure over an
24
+ * injected registry loader so it can be unit-tested; the stdio loop only
25
+ * starts when this file is the process entry point.
15
26
  * @module
16
27
  */
28
+ import { createRequire } from "node:module";
29
+ import { computeContextStrategy } from "./context-strategy.js";
30
+ import { isMainModule } from "./entry.js";
17
31
  import { ModelRegistry } from "./registry.js";
32
+ import { parseRouteStrategy, ROUTE_STRATEGIES } from "./registry-routing.js";
33
+ // ---------------------------------------------------------------------------
34
+ // Server identity + protocol negotiation
35
+ // ---------------------------------------------------------------------------
36
+ /** Package version, read from package.json so `serverInfo.version` never drifts from the release. */
37
+ export const KOSHA_MCP_VERSION = (() => {
38
+ try {
39
+ const require = createRequire(import.meta.url);
40
+ return require("../package.json").version;
41
+ }
42
+ catch {
43
+ return "0.0.0";
44
+ }
45
+ })();
46
+ /** MCP protocol revisions this server implements, newest first. */
47
+ export const MCP_PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
48
+ /**
49
+ * Pick the protocol revision to answer an `initialize` with: the client's
50
+ * own when we support it, otherwise our newest (the client then decides
51
+ * whether to proceed).
52
+ */
53
+ export function negotiateProtocolVersion(requested) {
54
+ if (typeof requested === "string" && MCP_PROTOCOL_VERSIONS.includes(requested))
55
+ return requested;
56
+ return MCP_PROTOCOL_VERSIONS[0];
57
+ }
58
+ /** Standard JSON-RPC 2.0 error codes used by this server. */
59
+ const JSON_RPC = {
60
+ PARSE_ERROR: -32700,
61
+ METHOD_NOT_FOUND: -32601,
62
+ INVALID_PARAMS: -32602,
63
+ INTERNAL_ERROR: -32603,
64
+ };
18
65
  // ---------------------------------------------------------------------------
19
66
  // Tool definitions (MCP schema)
20
67
  // ---------------------------------------------------------------------------
21
- const TOOLS = [
68
+ export const TOOLS = [
22
69
  {
23
70
  name: "kosha_query_models",
24
71
  description: "List AI models from the local kosha registry. Optionally filter by provider, mode, or capability tag. Returns id, provider, name, mode, context window, capabilities, and pricing.",
@@ -28,7 +75,7 @@ const TOOLS = [
28
75
  provider: { type: "string", description: "Provider ID (e.g. anthropic, openai, groq, openrouter)" },
29
76
  mode: {
30
77
  type: "string",
31
- enum: ["chat", "embedding", "image", "audio"],
78
+ enum: ["chat", "embedding", "image", "video", "audio", "moderation", "rerank"],
32
79
  description: "Primary model mode",
33
80
  },
34
81
  capability: {
@@ -58,6 +105,25 @@ const TOOLS = [
58
105
  },
59
106
  },
60
107
  },
108
+ {
109
+ name: "kosha_ranked_routes",
110
+ description: "Rank chat-model routes by strategy: cheapest (price only), fastest (observed p95 latency), reliable (circuit-breaker state plus timeout / auth-error history), or balanced (weighted blend of all three). Providers whose breaker is open always sort last, so the result doubles as a failover order. Accepts the same filters as kosha_cheapest_model.",
111
+ inputSchema: {
112
+ type: "object",
113
+ properties: {
114
+ strategy: {
115
+ type: "string",
116
+ enum: ["cheapest", "fastest", "reliable", "balanced"],
117
+ description: "Ranking strategy",
118
+ },
119
+ capability: { type: "string", description: "Required capability tag (e.g. tool_use, vision)" },
120
+ min_context_k: { type: "number", description: "Minimum context window in thousands of tokens" },
121
+ provider: { type: "string", description: "Pin to a specific provider" },
122
+ limit: { type: "number", description: "Number of ranked results (default 5)" },
123
+ },
124
+ required: ["strategy"],
125
+ },
126
+ },
61
127
  {
62
128
  name: "kosha_model_detail",
63
129
  description: "Get full details for a specific model — pricing, capabilities, context window, tool dialect, structured output modes, status, and deprecation info.",
@@ -66,7 +132,7 @@ const TOOLS = [
66
132
  properties: {
67
133
  model: {
68
134
  type: "string",
69
- description: "Model ID or alias (e.g. sonnet, claude-sonnet-4-6, gpt-4o, deepseek-v3)",
135
+ description: "Model ID or alias (e.g. sonnet, claude-sonnet-5, gpt-4o, deepseek-v3)",
70
136
  },
71
137
  },
72
138
  required: ["model"],
@@ -107,25 +173,51 @@ const TOOLS = [
107
173
  },
108
174
  },
109
175
  },
176
+ {
177
+ name: "kosha_context_strategy",
178
+ description: "Advise on context management for a long-running conversation. Given a model and current token usage, returns ranked options (continue, enable prompt cache, compact + continue, switch to long-context tier, switch model, batch offload) with rough cost-per-turn math. Useful before deciding whether to summarize, swap models, or just keep going.",
179
+ inputSchema: {
180
+ type: "object",
181
+ properties: {
182
+ model: { type: "string", description: "Model ID or alias the caller is currently using" },
183
+ current_tokens: { type: "number", description: "Approximate token count of the conversation so far" },
184
+ expected_output_tokens: {
185
+ type: "number",
186
+ description: "Tokens the next reply is expected to produce (default 1024)",
187
+ },
188
+ expected_remaining_turns: {
189
+ type: "number",
190
+ description: "How many more turns the caller plans (default 5)",
191
+ },
192
+ },
193
+ required: ["model", "current_tokens"],
194
+ },
195
+ },
110
196
  ];
111
- // ---------------------------------------------------------------------------
112
- // Registry — load in background on startup so first tool call is fast
113
- // ---------------------------------------------------------------------------
114
197
  let registryInstance = null;
115
- const registryReady = (async () => {
198
+ let registryReady = null;
199
+ async function loadRegistry() {
116
200
  const reg = new ModelRegistry();
117
201
  await reg.discover();
118
202
  registryInstance = reg;
119
203
  return reg;
120
- })();
204
+ }
121
205
  async function getRegistry() {
122
- return registryInstance ?? registryReady;
206
+ if (registryInstance)
207
+ return registryInstance;
208
+ registryReady ??= loadRegistry();
209
+ return registryReady;
210
+ }
211
+ /** Production dependencies: the discover-on-first-use registry. */
212
+ const defaultDeps = { registry: getRegistry };
213
+ /** Thrown by a tool handler for caller mistakes (unknown tool, bad argument). */
214
+ class ToolInputError extends Error {
123
215
  }
124
216
  // ---------------------------------------------------------------------------
125
217
  // Tool handlers
126
218
  // ---------------------------------------------------------------------------
127
- async function callTool(name, args) {
128
- const reg = await getRegistry();
219
+ export async function callTool(name, args, deps = defaultDeps) {
220
+ const reg = await deps.registry();
129
221
  switch (name) {
130
222
  case "kosha_query_models": {
131
223
  const models = reg.models({
@@ -168,6 +260,43 @@ async function callTool(name, args) {
168
260
  missingCredentials: result.missingCredentials.map((c) => c.providerId),
169
261
  };
170
262
  }
263
+ case "kosha_ranked_routes": {
264
+ const strategy = parseRouteStrategy(typeof args.strategy === "string" ? args.strategy : undefined);
265
+ if (!strategy) {
266
+ throw new ToolInputError(`unknown strategy '${String(args.strategy)}' — expected one of ${ROUTE_STRATEGIES.join(", ")}`);
267
+ }
268
+ const limit = typeof args.limit === "number" && args.limit > 0 ? Math.floor(args.limit) : 5;
269
+ const minContext = typeof args.min_context_k === "number" ? args.min_context_k * 1_000 : undefined;
270
+ const ranked = reg.rankedRoutes({
271
+ mode: "chat",
272
+ capability: args.capability,
273
+ provider: args.provider,
274
+ // Over-fetch so a min-context filter applied after ranking
275
+ // still has enough candidates to fill `limit`.
276
+ limit: Math.max(limit * 4, 20),
277
+ }, strategy);
278
+ const filtered = minContext ? ranked.filter((r) => r.model.contextWindow >= minContext) : ranked;
279
+ return {
280
+ strategy,
281
+ routes: filtered.slice(0, limit).map((r) => ({
282
+ id: r.model.id,
283
+ provider: r.providerId,
284
+ name: r.model.name,
285
+ contextWindow: r.model.contextWindow,
286
+ pricing: r.model.pricing ?? null,
287
+ price: r.price,
288
+ compositeScore: r.compositeScore,
289
+ health: {
290
+ available: r.health.available,
291
+ breakerState: r.health.breakerState,
292
+ reliabilityScore: r.health.reliabilityScore,
293
+ p95LatencyMs: r.health.p95LatencyMs,
294
+ samples: r.health.samples,
295
+ lastErrorType: r.health.lastErrorType,
296
+ },
297
+ })),
298
+ };
299
+ }
171
300
  case "kosha_model_detail": {
172
301
  const model = reg.model(args.model);
173
302
  if (!model)
@@ -221,73 +350,144 @@ async function callTool(name, args) {
221
350
  lastError: errorByProvider.get(p.id) ?? null,
222
351
  }));
223
352
  }
353
+ case "kosha_context_strategy": {
354
+ const modelId = args.model;
355
+ if (!modelId)
356
+ return { error: "model is required" };
357
+ const currentTokens = typeof args.current_tokens === "number" ? args.current_tokens : undefined;
358
+ if (currentTokens === undefined || currentTokens < 0) {
359
+ return { error: "current_tokens must be a non-negative number" };
360
+ }
361
+ const model = reg.model(modelId);
362
+ if (!model)
363
+ return { error: `model '${modelId}' not found` };
364
+ const candidates = reg
365
+ .cheapestModels({ mode: "chat", limit: 20 })
366
+ .matches.map((m) => m.model);
367
+ return computeContextStrategy({
368
+ model,
369
+ currentTokens,
370
+ expectedRemainingTurns: typeof args.expected_remaining_turns === "number" ? args.expected_remaining_turns : undefined,
371
+ expectedOutputTokens: typeof args.expected_output_tokens === "number" ? args.expected_output_tokens : undefined,
372
+ candidateAlternatives: candidates,
373
+ });
374
+ }
224
375
  default:
225
- throw new Error(`unknown tool: ${name}`);
376
+ throw new ToolInputError(`unknown tool: ${name}`);
226
377
  }
227
378
  }
228
379
  // ---------------------------------------------------------------------------
229
- // Stdio JSON-RPC loop
380
+ // JSON-RPC dispatch (pure) + stdio loop (entry point only)
230
381
  // ---------------------------------------------------------------------------
231
- function send(msg) {
232
- process.stdout.write(JSON.stringify(msg) + "\n");
382
+ function ok(id, result) {
383
+ return { jsonrpc: "2.0", id: id ?? null, result };
233
384
  }
234
- function respond(id, result) {
235
- send({ jsonrpc: "2.0", id: id ?? null, result });
385
+ function fail(id, code, message) {
386
+ return { jsonrpc: "2.0", id: id ?? null, error: { code, message } };
236
387
  }
237
- function respondError(id, code, message) {
238
- send({ jsonrpc: "2.0", id: id ?? null, error: { code, message } });
239
- }
240
- async function handleMessage(msg) {
388
+ /**
389
+ * Handle one JSON-RPC request and return the response, or `null` for
390
+ * notifications (which carry no id and expect no reply).
391
+ *
392
+ * Tool *execution* failures come back as a successful `tools/call` result
393
+ * with `isError: true`, per the MCP spec, so the model can read the message
394
+ * and recover; only protocol-level problems (unknown method, unknown tool,
395
+ * malformed params) surface as JSON-RPC errors.
396
+ */
397
+ export async function handleJsonRpc(msg, deps = defaultDeps) {
241
398
  const { id, method, params } = msg;
242
399
  // Notifications carry no id — no response expected.
243
400
  if (!method || method.startsWith("notifications/"))
244
- return;
401
+ return null;
245
402
  switch (method) {
246
- case "initialize":
247
- respond(id, {
248
- protocolVersion: "2024-11-05",
403
+ case "initialize": {
404
+ const requested = params?.protocolVersion;
405
+ return ok(id, {
406
+ protocolVersion: negotiateProtocolVersion(requested),
249
407
  capabilities: { tools: {} },
250
- serverInfo: { name: "kosha", version: "1.0.0" },
408
+ serverInfo: { name: "kosha", version: KOSHA_MCP_VERSION },
251
409
  });
252
- return;
410
+ }
411
+ case "ping":
412
+ return ok(id, {});
253
413
  case "tools/list":
254
- respond(id, { tools: TOOLS });
255
- return;
414
+ return ok(id, { tools: TOOLS });
256
415
  case "tools/call": {
257
- const { name, arguments: args = {} } = params;
416
+ const call = (params ?? {});
417
+ if (typeof call.name !== "string") {
418
+ return fail(id, JSON_RPC.INVALID_PARAMS, "tools/call requires a string `name`");
419
+ }
420
+ const args = call.arguments && typeof call.arguments === "object" && !Array.isArray(call.arguments)
421
+ ? call.arguments
422
+ : {};
258
423
  try {
259
- const result = await callTool(name, args);
260
- respond(id, { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] });
424
+ const result = await callTool(call.name, args, deps);
425
+ return ok(id, { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] });
261
426
  }
262
427
  catch (err) {
263
428
  const message = err instanceof Error ? err.message : String(err);
264
- respondError(id, -32603, message);
429
+ if (err instanceof ToolInputError)
430
+ return fail(id, JSON_RPC.INVALID_PARAMS, message);
431
+ return ok(id, { content: [{ type: "text", text: message }], isError: true });
265
432
  }
266
- return;
267
433
  }
268
434
  default:
269
- respondError(id, -32601, `method not found: ${method}`);
435
+ return fail(id, JSON_RPC.METHOD_NOT_FOUND, `method not found: ${method}`);
270
436
  }
271
437
  }
272
- let buffer = "";
273
- process.stdin.setEncoding("utf-8");
274
- process.stdin.on("data", (chunk) => {
275
- buffer += chunk;
276
- const lines = buffer.split("\n");
277
- buffer = lines.pop() ?? "";
278
- for (const line of lines) {
279
- const trimmed = line.trim();
280
- if (!trimmed)
281
- continue;
282
- try {
283
- handleMessage(JSON.parse(trimmed)).catch((err) => {
438
+ function send(msg) {
439
+ process.stdout.write(`${JSON.stringify(msg)}\n`);
440
+ }
441
+ /** Start the newline-delimited JSON-RPC loop over stdio. */
442
+ export function runStdioServer(deps = defaultDeps) {
443
+ let buffer = "";
444
+ /** In-flight requests, so an EOF on stdin waits for their answers before exiting. */
445
+ const pending = new Set();
446
+ process.stdin.setEncoding("utf-8");
447
+ process.stdin.on("data", (chunk) => {
448
+ buffer += chunk;
449
+ const lines = buffer.split("\n");
450
+ buffer = lines.pop() ?? "";
451
+ for (const line of lines) {
452
+ const trimmed = line.trim();
453
+ if (!trimmed)
454
+ continue;
455
+ let parsed;
456
+ try {
457
+ parsed = JSON.parse(trimmed);
458
+ }
459
+ catch {
460
+ // Malformed JSON. Per JSON-RPC 2.0 we must still answer with a
461
+ // -32700 Parse error so the client doesn't block forever waiting
462
+ // on a response. The id is unknowable from unparseable input, so
463
+ // per spec we respond with id=null (we deliberately avoid scanning
464
+ // the raw text for an id, which would mean running a regex over
465
+ // uncontrolled stdin).
466
+ send(fail(null, JSON_RPC.PARSE_ERROR, "Parse error"));
467
+ continue;
468
+ }
469
+ const job = handleJsonRpc(parsed, deps)
470
+ .then((response) => {
471
+ if (response)
472
+ send(response);
473
+ })
474
+ .catch((err) => {
284
475
  process.stderr.write(`[kosha-mcp] ${err}\n`);
476
+ })
477
+ .finally(() => {
478
+ pending.delete(job);
285
479
  });
480
+ pending.add(job);
286
481
  }
287
- catch {
288
- // malformed JSON — ignore
289
- }
290
- }
291
- });
292
- process.stdin.on("end", () => process.exit(0));
482
+ });
483
+ // One-shot / piped clients half-close stdin right after writing their
484
+ // request; exiting immediately would drop the answer still being computed.
485
+ process.stdin.on("end", () => {
486
+ Promise.allSettled(pending).then(() => process.exit(0));
487
+ });
488
+ }
489
+ // Only attach to stdio when executed directly (`kosha-mcp` bin or
490
+ // `node dist/mcp-server.js`); see entry.ts for why realpath matters.
491
+ if (isMainModule(import.meta.url))
492
+ runStdioServer();
293
493
  //# sourceMappingURL=mcp-server.js.map