granite-cli 0.2.0

CLI for discovering, configuring, and launching AI workflows powered by IBM Granite models.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! `VisionMCPCapability`: exposes a vision-language model as an MCP server
//! that a launched coding agent can call as a tool (compare/analyze images).
//!
//! Like `AgentModelCapability`, this depends on a configured `Model` (which
//! in turn resolves its `Provider`) rather than taking an endpoint/api_key
//! of its own -- connection details are resolved from that dependency chain
//! at `bind()` time, the same way every other model-backed capability works.
//!
//! `bind()` always serves Streamable HTTP: a background server is started
//! in-process (via [`crate::utils::subserver::SubServer`], the same
//! mechanism the usage-tracking proxy uses) for the lifetime of this one
//! launch, and torn down in `on_shutdown`.

mod backend;
mod tools;

pub use backend::OpenAiCompatibleVlm;
pub use tools::VlmToolRegistry;

use crate::capabilities::base::{
    Binding, BindingRequest, BindingType, Capability, CapabilityMetadata, Dependency,
    HasCapabilityMetadata, LaunchContext, McpBinding, McpBindingRequest, McpTransportKind,
};
use crate::capabilities::requirement::ModelRequirement;
use crate::models::{ConfiguredModel, ModelFunction, ModelType};
use crate::providers::ApiType;
use crate::registry::ConfigConstructable;
use crate::utils::subserver::SubServer;
use async_trait::async_trait;
use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
use rmcp::transport::streamable_http_server::{StreamableHttpServerConfig, StreamableHttpService};
use serde::{Deserialize, Serialize};
use serde_valid::Validate;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::Mutex;

/*-- VisionMCPCapabilityConfig ----------------------------------------------------*/

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema, Validate)]
pub struct VisionMCPCapabilityConfig {
    /// Key into the configured models map (the user-chosen instance ID) for
    /// the vision-language model this capability serves.
    #[validate(min_length = 1)]
    pub model_id: String,
    /// Request timeout in seconds for calls to the model's provider.
    #[serde(default = "default_timeout_seconds")]
    pub timeout_seconds: u64,
    /// Maximum image size (bytes) accepted from any image source (file,
    /// inline base64, or fetched URL).
    #[serde(default = "default_max_image_bytes")]
    pub max_image_bytes: u64,
    /// Extra headers sent with every request to the model's provider, on
    /// top of whatever auth the provider itself supplies.
    #[serde(default)]
    pub extra_headers: HashMap<String, String>,
}

fn default_timeout_seconds() -> u64 {
    120
}

fn default_max_image_bytes() -> u64 {
    50 * 1024 * 1024
}

impl Default for VisionMCPCapabilityConfig {
    fn default() -> Self {
        Self {
            model_id: String::new(),
            timeout_seconds: default_timeout_seconds(),
            max_image_bytes: default_max_image_bytes(),
            extra_headers: HashMap::new(),
        }
    }
}

/*-- VisionMCPCapability -----------------------------------------------------------*/

pub struct VisionMCPCapability {
    instance_id: String,
    config: VisionMCPCapabilityConfig,
    configured_model: ConfiguredModel,
    /// The in-process Streamable HTTP server started by `bind()`. `None`
    /// before `bind()` runs.
    http_server: Mutex<Option<SubServer>>,
}

impl ConfigConstructable for VisionMCPCapability {
    type Config = VisionMCPCapabilityConfig;

    /// Constructs the capability by resolving its model through
    /// `ConfiguredModel`, exactly like `AgentModelCapability::new` -- so
    /// `model.provider()` works at bind time and, when a usage-tracking
    /// session is active, the model is transparently tracked.
    fn new(
        instance_id: &str,
        cfg: &serde_json::Value,
        global_config: &crate::config::Config,
    ) -> Self {
        let config: VisionMCPCapabilityConfig =
            serde_json::from_value(cfg.clone()).unwrap_or_default();
        let configured_model = ConfiguredModel::resolve(&config.model_id, global_config);
        Self {
            instance_id: instance_id.to_string(),
            config,
            configured_model,
            http_server: Mutex::new(None),
        }
    }
}

impl crate::registry::Named for VisionMCPCapability {
    fn instance_id(&self) -> &str {
        &self.instance_id
    }
}

#[async_trait]
impl Capability for VisionMCPCapability {
    fn name(&self) -> &str {
        "Vision MCP Server"
    }

    fn description(&self) -> &str {
        "Exposes a vision-language model as an MCP server (compare/analyze images) for a launched coding agent to call."
    }

    fn binding_types(&self) -> HashSet<BindingType> {
        HashSet::from([BindingType::Mcp])
    }

    async fn bind(&self, request: BindingRequest) -> anyhow::Result<Binding> {
        let McpBindingRequest {
            supported_transports,
        } = match request {
            BindingRequest::Mcp(request) => request,
            other => anyhow::bail!(
                "VisionMCPCapability does not handle {:?} binding requests",
                other.binding_type()
            ),
        };
        anyhow::ensure!(
            supported_transports.contains(&McpTransportKind::Http),
            "no MCP transport in common with the requesting launcher (it offered {supported_transports:?}; \
             VisionMCPCapability only serves Streamable HTTP)"
        );

        let model_id = &self.config.model_id;
        // The vision backend speaks the OpenAI-compatible chat/vision
        // dialect, the one every granite-cli provider can serve -- same
        // rationale as `pi`/`opencode`'s AgentModel binding. The model must
        // support ImageUnderstanding, but the endpoint is looked up via
        // Chat, since that's the endpoint that actually serves vision
        // requests.
        let (provider, endpoint, model_name) = self.configured_model.resolve_provider_endpoint(
            model_id,
            ApiType::OpenAI,
            ModelFunction::ImageUnderstanding,
            ModelFunction::Chat,
        )?;

        let vlm = OpenAiCompatibleVlm::new(
            vlm_base_url(provider.base_url(), endpoint.path()),
            model_name,
            provider.api_key().map(|k| k.0.clone()).unwrap_or_default(),
            self.config.timeout_seconds,
            self.config.max_image_bytes,
            self.config.extra_headers.clone(),
        )?;

        let tool_registry = VlmToolRegistry::new(Arc::new(vlm));
        let service = StreamableHttpService::new(
            move || Ok(tool_registry.clone()),
            Arc::new(LocalSessionManager::default()),
            StreamableHttpServerConfig::default(),
        );
        let router = axum::Router::new().route_service("/mcp", service);
        let server = SubServer::spawn(router, &format!("vision-mcp ({})", self.instance_id))?;
        let url = format!("http://{}/mcp", server.local_addr);
        *self.http_server.lock().await = Some(server);

        Ok(Binding::Mcp(McpBinding::Http {
            url,
            headers: HashMap::new(),
            timeout: None,
        }))
    }

    async fn on_shutdown(&self, _context: &LaunchContext) -> anyhow::Result<()> {
        if let Some(server) = self.http_server.lock().await.take() {
            server.shutdown().await;
        }
        Ok(())
    }
}

impl HasCapabilityMetadata for VisionMCPCapability {
    fn metadata() -> CapabilityMetadata {
        CapabilityMetadata {
            name: "Vision MCP Server".to_string(),
            description: "Exposes a vision-language model as an MCP server (compare/analyze images) for a launched coding agent to call.".to_string(),
            dependencies: vec![Dependency::Model {
                config_key: "model_id".to_string(),
                requirement: ModelRequirement {
                    model_type: Some(ModelType::Vision),
                    supported_functions: vec![ModelFunction::Chat, ModelFunction::ImageUnderstanding],
                    ..Default::default()
                },
                resolved_id: None,
                required: true,
            }],
            tags: vec!["vision".to_string(), "mcp".to_string()],
            supported_binding_types: HashSet::from([BindingType::Mcp]),
        }
    }
}

/// The vision backend's REST client appends its own operation path (e.g.
/// `chat/completions`) to whatever root it's given, so the trailing
/// operation is dropped from the endpoint path here -- same convention as
/// `pi`/`opencode`'s own `*_base_url` helpers.
fn vlm_base_url(base_url: &str, endpoint_path: &str) -> String {
    let root = base_url.trim_end_matches('/');
    let prefix = endpoint_path
        .strip_suffix("/chat/completions")
        .unwrap_or("");
    format!("{root}{prefix}")
}

/*-- tests -------------------------------------------------------------------------*/

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{Config, ModelConfig};
    use crate::models::{Model, ModelVariant};
    use crate::providers::{ApiEndpoint, HealthStatus, ModelFormat, Provider, ProviderError};
    use crate::registry::Secret;
    use std::collections::HashMap as StdHashMap;

    #[derive(Clone, Default)]
    struct FakeProvider {
        instance_id: String,
        base_url: String,
        api_key: Option<Secret>,
        verify_ssl: bool,
        api_types: Vec<ApiType>,
        endpoints: StdHashMap<ModelFunction, Vec<ApiEndpoint>>,
        alias: Option<String>,
    }

    impl ConfigConstructable for FakeProvider {
        type Config = crate::registry::NoConfig;
        fn new(_: &str, _: &serde_json::Value, _: &crate::config::Config) -> Self {
            unimplemented!("not used in tests")
        }
    }

    impl crate::registry::Named for FakeProvider {
        fn instance_id(&self) -> &str {
            &self.instance_id
        }
    }

    #[async_trait]
    impl Provider for FakeProvider {
        fn name(&self) -> &str {
            "Fake Provider"
        }
        fn function_endpoints(&self) -> StdHashMap<ModelFunction, Vec<ApiEndpoint>> {
            self.endpoints.clone()
        }
        fn supported_api_types(&self) -> Vec<ApiType> {
            self.api_types.clone()
        }
        fn base_url(&self) -> &str {
            &self.base_url
        }
        fn api_key(&self) -> Option<&Secret> {
            self.api_key.as_ref()
        }
        fn verify_ssl(&self) -> bool {
            self.verify_ssl
        }
        fn custom_headers(&self) -> Option<StdHashMap<String, Secret>> {
            None
        }
        fn supported_formats(&self) -> Vec<ModelFormat> {
            vec![]
        }
        fn model_alias(
            &self,
            _model_id: String,
            _variant: Option<&ModelVariant>,
        ) -> Option<String> {
            self.alias.clone()
        }
        async fn health_check(&self) -> Result<HealthStatus, ProviderError> {
            unimplemented!("not used in tests")
        }
    }

    fn ok_provider() -> FakeProvider {
        let mut endpoints = StdHashMap::new();
        endpoints.insert(ModelFunction::Chat, vec![ApiEndpoint::OpenAIChat]);
        FakeProvider {
            instance_id: "my-ollama".to_string(),
            base_url: "http://localhost:11434".to_string(),
            api_key: None,
            verify_ssl: true,
            api_types: vec![ApiType::OpenAI],
            endpoints,
            alias: None,
        }
    }

    struct TestVisionModel {
        supported_functions: Vec<ModelFunction>,
        provider: FakeProvider,
    }

    impl ConfigConstructable for TestVisionModel {
        type Config = crate::registry::NoConfig;
        fn new(_: &str, _: &serde_json::Value, _: &crate::config::Config) -> Self {
            unimplemented!("not used in tests")
        }
    }

    impl crate::registry::Named for TestVisionModel {
        fn instance_id(&self) -> &str {
            "granite-vision-test"
        }
    }

    impl Model for TestVisionModel {
        fn family(&self) -> &str {
            "Test"
        }
        fn version(&self) -> &str {
            "1.0"
        }
        fn size(&self) -> u64 {
            1
        }
        fn context_length(&self) -> u64 {
            4096
        }
        fn model_type(&self) -> &ModelType {
            &ModelType::Vision
        }
        fn huggingface_repo(&self) -> &str {
            "test/test-vision"
        }
        fn native_dtype(&self) -> &str {
            "bfloat16"
        }
        fn architecture(&self) -> &crate::models::ModelArchitecture {
            unimplemented!("not used in tests")
        }
        fn variants(&self) -> &[ModelVariant] {
            &[]
        }
        fn description(&self) -> Option<&str> {
            None
        }
        fn tags(&self) -> &[String] {
            &[]
        }
        fn supported_functions(&self) -> &[ModelFunction] {
            &self.supported_functions
        }
        fn provider(&self) -> anyhow::Result<Box<dyn Provider>> {
            Ok(Box::new(self.provider.clone()))
        }
    }

    /// Builds a `VisionMCPCapability` with a real registry model id (so
    /// construction succeeds) and then swaps in a test double model/provider,
    /// mirroring `agent_model.rs`'s test pattern.
    fn capability_with_test_model(
        functions: Vec<ModelFunction>,
        provider: FakeProvider,
    ) -> VisionMCPCapability {
        let mut config = Config::default();
        config.models.insert(
            "granite-3.1-8b-instruct".to_string(),
            ModelConfig {
                model_id: "granite-3.1-8b-instruct".to_string(),
                model_type: "granite-3.1-8b-instruct".to_string(),
                config: serde_json::json!({}),
                provider_id: "ollama".to_string(),
                variant: None,
            },
        );
        let cap = VisionMCPCapability::new(
            "vision",
            &serde_json::json!({ "model_id": "granite-3.1-8b-instruct" }),
            &config,
        );
        VisionMCPCapability {
            instance_id: cap.instance_id,
            config: cap.config,
            configured_model: ConfiguredModel::for_test(
                Arc::new(TestVisionModel {
                    supported_functions: functions,
                    provider,
                }),
                None,
            ),
            http_server: Mutex::new(None),
        }
    }

    fn request(transports: impl IntoIterator<Item = McpTransportKind>) -> BindingRequest {
        BindingRequest::Mcp(McpBindingRequest {
            supported_transports: transports.into_iter().collect(),
        })
    }

    fn ctx() -> LaunchContext {
        LaunchContext {
            launcher_id: "test".to_string(),
            working_dir: std::env::temp_dir(),
            base_env: HashMap::new(),
            dry_run: false,
        }
    }

    #[test]
    fn binding_types_reports_mcp() {
        let cap =
            capability_with_test_model(vec![ModelFunction::ImageUnderstanding], ok_provider());
        assert_eq!(cap.binding_types(), HashSet::from([BindingType::Mcp]));
    }

    #[tokio::test]
    async fn bind_starts_a_real_http_server_from_the_resolved_provider() {
        let cap =
            capability_with_test_model(vec![ModelFunction::ImageUnderstanding], ok_provider());
        let binding = cap.bind(request([McpTransportKind::Http])).await.unwrap();
        let Binding::Mcp(McpBinding::Http { url, .. }) = binding else {
            panic!("expected an Http McpBinding");
        };
        assert!(url.starts_with("http://127.0.0.1:"));
        assert!(url.ends_with("/mcp"));

        // A real MCP initialize handshake round-trips over Streamable HTTP.
        let client = reqwest::Client::new();
        let resp = client
            .post(&url)
            .header("Content-Type", "application/json")
            .header("Accept", "application/json, text/event-stream")
            .body(
                serde_json::json!({
                    "jsonrpc": "2.0",
                    "id": 1,
                    "method": "initialize",
                    "params": {
                        "protocolVersion": "2024-11-05",
                        "capabilities": {},
                        "clientInfo": {"name": "smoke-test", "version": "0.0.0"},
                    },
                })
                .to_string(),
            )
            .send()
            .await
            .unwrap();
        assert!(resp.status().is_success(), "status: {}", resp.status());
        let body = resp.text().await.unwrap();
        assert!(
            body.contains("protocolVersion"),
            "expected an initialize result, got: {body}"
        );

        cap.on_shutdown(&ctx()).await.unwrap();

        // The port is free again after shutdown.
        let addr = url.trim_start_matches("http://").trim_end_matches("/mcp");
        std::net::TcpListener::bind(addr).unwrap();
    }

    #[tokio::test]
    async fn bind_errors_when_launcher_offers_only_stdio() {
        let cap =
            capability_with_test_model(vec![ModelFunction::ImageUnderstanding], ok_provider());
        let err = cap
            .bind(request([McpTransportKind::Stdio]))
            .await
            .unwrap_err();
        assert!(err.to_string().contains("no MCP transport in common"));
    }

    #[tokio::test]
    async fn bind_fails_when_model_lacks_image_understanding() {
        let cap = capability_with_test_model(vec![ModelFunction::Chat], ok_provider());
        let err = cap
            .bind(request([McpTransportKind::Http]))
            .await
            .unwrap_err();
        assert!(
            err.to_string()
                .contains("does not support Image Understanding")
        );
    }

    #[tokio::test]
    async fn bind_fails_when_provider_lacks_openai_api_type() {
        let cap = capability_with_test_model(
            vec![ModelFunction::ImageUnderstanding],
            FakeProvider {
                api_types: vec![ApiType::Ollama],
                ..ok_provider()
            },
        );
        let err = cap
            .bind(request([McpTransportKind::Http]))
            .await
            .unwrap_err();
        assert!(err.to_string().contains("does not support OpenAI"));
    }

    #[tokio::test]
    async fn bind_rejects_non_mcp_requests() {
        let cap =
            capability_with_test_model(vec![ModelFunction::ImageUnderstanding], ok_provider());
        let err = cap
            .bind(BindingRequest::AgentModel(
                crate::capabilities::base::AgentModelBindingRequest {
                    api_type: ApiType::OpenAI,
                },
            ))
            .await
            .unwrap_err();
        assert!(err.to_string().contains("does not handle"));
    }

    #[tokio::test]
    async fn on_shutdown_without_a_started_server_is_a_no_op() {
        let cap =
            capability_with_test_model(vec![ModelFunction::ImageUnderstanding], ok_provider());
        cap.on_shutdown(&ctx()).await.unwrap();
    }

    #[test]
    fn vlm_base_url_drops_trailing_operation_and_keeps_version_prefix() {
        assert_eq!(
            vlm_base_url("http://localhost:11434", "/v1/chat/completions"),
            "http://localhost:11434/v1"
        );
    }

    #[test]
    fn vlm_base_url_trims_trailing_slash_from_provider_url() {
        assert_eq!(
            vlm_base_url("http://localhost:1234/", "/v1/chat/completions"),
            "http://localhost:1234/v1"
        );
    }
}