fix: restore mcp flexibility and improve cli tooling
This commit is contained in:
@@ -7,11 +7,13 @@
|
||||
clippy::empty_line_after_outer_attr
|
||||
)]
|
||||
|
||||
use owlen_core::config::{ensure_provider_config, Config as OwlenConfig};
|
||||
use owlen_core::mcp::protocol::{
|
||||
methods, ErrorCode, InitializeParams, InitializeResult, RequestId, RpcError, RpcErrorResponse,
|
||||
RpcNotification, RpcRequest, RpcResponse, ServerCapabilities, ServerInfo, PROTOCOL_VERSION,
|
||||
};
|
||||
use owlen_core::mcp::{McpToolCall, McpToolDescriptor, McpToolResponse};
|
||||
use owlen_core::provider::ProviderConfig;
|
||||
use owlen_core::types::{ChatParameters, ChatRequest, Message};
|
||||
use owlen_core::Provider;
|
||||
use owlen_ollama::OllamaProvider;
|
||||
@@ -106,12 +108,44 @@ fn resources_list_descriptor() -> McpToolDescriptor {
|
||||
}
|
||||
}
|
||||
|
||||
fn provider_from_config() -> Result<OllamaProvider, RpcError> {
|
||||
let mut config = OwlenConfig::load(None).unwrap_or_default();
|
||||
let provider_name =
|
||||
env::var("OWLEN_PROVIDER").unwrap_or_else(|_| config.general.default_provider.clone());
|
||||
if config.provider(&provider_name).is_none() {
|
||||
ensure_provider_config(&mut config, &provider_name);
|
||||
}
|
||||
let provider_cfg: ProviderConfig =
|
||||
config.provider(&provider_name).cloned().ok_or_else(|| {
|
||||
RpcError::internal_error(format!(
|
||||
"Provider '{provider_name}' not found in configuration"
|
||||
))
|
||||
})?;
|
||||
|
||||
if provider_cfg.provider_type != "ollama" && provider_cfg.provider_type != "ollama-cloud" {
|
||||
return Err(RpcError::internal_error(format!(
|
||||
"Unsupported provider type '{}' for MCP LLM server",
|
||||
provider_cfg.provider_type
|
||||
)));
|
||||
}
|
||||
|
||||
OllamaProvider::from_config(&provider_cfg, Some(&config.general)).map_err(|e| {
|
||||
RpcError::internal_error(format!("Failed to init OllamaProvider from config: {}", e))
|
||||
})
|
||||
}
|
||||
|
||||
fn create_provider() -> Result<OllamaProvider, RpcError> {
|
||||
if let Ok(url) = env::var("OLLAMA_URL") {
|
||||
return OllamaProvider::new(&url).map_err(|e| {
|
||||
RpcError::internal_error(format!("Failed to init OllamaProvider: {}", e))
|
||||
});
|
||||
}
|
||||
|
||||
provider_from_config()
|
||||
}
|
||||
|
||||
async fn handle_generate_text(args: GenerateTextArgs) -> Result<String, RpcError> {
|
||||
// Create provider with Ollama URL from environment or default to localhost
|
||||
let ollama_url =
|
||||
env::var("OLLAMA_URL").unwrap_or_else(|_| "http://localhost:11434".to_string());
|
||||
let provider = OllamaProvider::new(&ollama_url)
|
||||
.map_err(|e| RpcError::internal_error(format!("Failed to init OllamaProvider: {}", e)))?;
|
||||
let provider = create_provider()?;
|
||||
|
||||
let parameters = ChatParameters {
|
||||
temperature: args.temperature,
|
||||
@@ -191,12 +225,7 @@ async fn handle_request(req: &RpcRequest) -> Result<Value, RpcError> {
|
||||
}
|
||||
// New method to list available Ollama models via the provider.
|
||||
methods::MODELS_LIST => {
|
||||
// Reuse the provider instance for model listing.
|
||||
let ollama_url =
|
||||
env::var("OLLAMA_URL").unwrap_or_else(|_| "http://localhost:11434".to_string());
|
||||
let provider = OllamaProvider::new(&ollama_url).map_err(|e| {
|
||||
RpcError::internal_error(format!("Failed to init OllamaProvider: {}", e))
|
||||
})?;
|
||||
let provider = create_provider()?;
|
||||
let models = provider
|
||||
.list_models()
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user