feat(secrets-mcp): MCP 请求日志、探测 404 与资源元数据
- 新增 logging 中间件:记录 client_ip、ua、JSON-RPC、tool 等 - tools 各入口/出口结构化日志 - 探测型 404(/.well-known、GET /mcp)降为 debug - /.well-known/oauth-protected-resource 最小元数据 - secrets-mcp 0.1.11 Made-with: Cursor
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::Result;
|
||||
use rmcp::{
|
||||
@@ -257,7 +258,17 @@ impl SecretsService {
|
||||
Parameters(input): Parameters<SearchInput>,
|
||||
ctx: RequestContext<RoleServer>,
|
||||
) -> Result<CallToolResult, rmcp::ErrorData> {
|
||||
let t = Instant::now();
|
||||
let user_id = Self::user_id_from_ctx(&ctx)?;
|
||||
tracing::info!(
|
||||
tool = "secrets_search",
|
||||
?user_id,
|
||||
namespace = input.namespace.as_deref(),
|
||||
kind = input.kind.as_deref(),
|
||||
name = input.name.as_deref(),
|
||||
query = input.query.as_deref(),
|
||||
"tool call start",
|
||||
);
|
||||
let tags = input.tags.unwrap_or_default();
|
||||
let result = svc_search(
|
||||
&self.pool,
|
||||
@@ -274,7 +285,10 @@ impl SecretsService {
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
.map_err(|e| {
|
||||
tracing::warn!(tool = "secrets_search", ?user_id, error = %e, "tool call failed");
|
||||
rmcp::ErrorData::internal_error(e.to_string(), None)
|
||||
})?;
|
||||
|
||||
let summary = input.summary.unwrap_or(false);
|
||||
let entries: Vec<serde_json::Value> = result
|
||||
@@ -312,6 +326,14 @@ impl SecretsService {
|
||||
})
|
||||
.collect();
|
||||
|
||||
let count = entries.len();
|
||||
tracing::info!(
|
||||
tool = "secrets_search",
|
||||
?user_id,
|
||||
result_count = count,
|
||||
elapsed_ms = t.elapsed().as_millis(),
|
||||
"tool call ok",
|
||||
);
|
||||
let json = serde_json::to_string_pretty(&entries).unwrap_or_else(|_| "[]".to_string());
|
||||
Ok(CallToolResult::success(vec![Content::text(json)]))
|
||||
}
|
||||
@@ -326,7 +348,17 @@ impl SecretsService {
|
||||
Parameters(input): Parameters<GetSecretInput>,
|
||||
ctx: RequestContext<RoleServer>,
|
||||
) -> Result<CallToolResult, rmcp::ErrorData> {
|
||||
let t = Instant::now();
|
||||
let (user_id, user_key) = Self::require_user_and_key(&ctx)?;
|
||||
tracing::info!(
|
||||
tool = "secrets_get",
|
||||
?user_id,
|
||||
namespace = %input.namespace,
|
||||
kind = %input.kind,
|
||||
name = %input.name,
|
||||
field = input.field.as_deref(),
|
||||
"tool call start",
|
||||
);
|
||||
|
||||
if let Some(field_name) = &input.field {
|
||||
let value = get_secret_field(
|
||||
@@ -339,8 +371,17 @@ impl SecretsService {
|
||||
Some(user_id),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
.map_err(|e| {
|
||||
tracing::warn!(tool = "secrets_get", ?user_id, error = %e, "tool call failed");
|
||||
rmcp::ErrorData::internal_error(e.to_string(), None)
|
||||
})?;
|
||||
|
||||
tracing::info!(
|
||||
tool = "secrets_get",
|
||||
?user_id,
|
||||
elapsed_ms = t.elapsed().as_millis(),
|
||||
"tool call ok",
|
||||
);
|
||||
let result = serde_json::json!({ field_name: value });
|
||||
let json = serde_json::to_string_pretty(&result).unwrap_or_default();
|
||||
Ok(CallToolResult::success(vec![Content::text(json)]))
|
||||
@@ -354,8 +395,19 @@ impl SecretsService {
|
||||
Some(user_id),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
.map_err(|e| {
|
||||
tracing::warn!(tool = "secrets_get", ?user_id, error = %e, "tool call failed");
|
||||
rmcp::ErrorData::internal_error(e.to_string(), None)
|
||||
})?;
|
||||
|
||||
let count = secrets.len();
|
||||
tracing::info!(
|
||||
tool = "secrets_get",
|
||||
?user_id,
|
||||
field_count = count,
|
||||
elapsed_ms = t.elapsed().as_millis(),
|
||||
"tool call ok",
|
||||
);
|
||||
let json = serde_json::to_string_pretty(&secrets).unwrap_or_default();
|
||||
Ok(CallToolResult::success(vec![Content::text(json)]))
|
||||
}
|
||||
@@ -371,7 +423,16 @@ impl SecretsService {
|
||||
Parameters(input): Parameters<AddInput>,
|
||||
ctx: RequestContext<RoleServer>,
|
||||
) -> Result<CallToolResult, rmcp::ErrorData> {
|
||||
let t = Instant::now();
|
||||
let (user_id, user_key) = Self::require_user_and_key(&ctx)?;
|
||||
tracing::info!(
|
||||
tool = "secrets_add",
|
||||
?user_id,
|
||||
namespace = %input.namespace,
|
||||
kind = %input.kind,
|
||||
name = %input.name,
|
||||
"tool call start",
|
||||
);
|
||||
|
||||
let tags = input.tags.unwrap_or_default();
|
||||
let meta = input.meta.unwrap_or_default();
|
||||
@@ -391,8 +452,20 @@ impl SecretsService {
|
||||
&user_key,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
.map_err(|e| {
|
||||
tracing::warn!(tool = "secrets_add", ?user_id, error = %e, "tool call failed");
|
||||
rmcp::ErrorData::internal_error(e.to_string(), None)
|
||||
})?;
|
||||
|
||||
tracing::info!(
|
||||
tool = "secrets_add",
|
||||
?user_id,
|
||||
namespace = %input.namespace,
|
||||
kind = %input.kind,
|
||||
name = %input.name,
|
||||
elapsed_ms = t.elapsed().as_millis(),
|
||||
"tool call ok",
|
||||
);
|
||||
let json = serde_json::to_string_pretty(&result).unwrap_or_default();
|
||||
Ok(CallToolResult::success(vec![Content::text(json)]))
|
||||
}
|
||||
@@ -406,7 +479,16 @@ impl SecretsService {
|
||||
Parameters(input): Parameters<UpdateInput>,
|
||||
ctx: RequestContext<RoleServer>,
|
||||
) -> Result<CallToolResult, rmcp::ErrorData> {
|
||||
let t = Instant::now();
|
||||
let (user_id, user_key) = Self::require_user_and_key(&ctx)?;
|
||||
tracing::info!(
|
||||
tool = "secrets_update",
|
||||
?user_id,
|
||||
namespace = %input.namespace,
|
||||
kind = %input.kind,
|
||||
name = %input.name,
|
||||
"tool call start",
|
||||
);
|
||||
|
||||
let add_tags = input.add_tags.unwrap_or_default();
|
||||
let remove_tags = input.remove_tags.unwrap_or_default();
|
||||
@@ -432,8 +514,20 @@ impl SecretsService {
|
||||
&user_key,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
.map_err(|e| {
|
||||
tracing::warn!(tool = "secrets_update", ?user_id, error = %e, "tool call failed");
|
||||
rmcp::ErrorData::internal_error(e.to_string(), None)
|
||||
})?;
|
||||
|
||||
tracing::info!(
|
||||
tool = "secrets_update",
|
||||
?user_id,
|
||||
namespace = %input.namespace,
|
||||
kind = %input.kind,
|
||||
name = %input.name,
|
||||
elapsed_ms = t.elapsed().as_millis(),
|
||||
"tool call ok",
|
||||
);
|
||||
let json = serde_json::to_string_pretty(&result).unwrap_or_default();
|
||||
Ok(CallToolResult::success(vec![Content::text(json)]))
|
||||
}
|
||||
@@ -447,7 +541,17 @@ impl SecretsService {
|
||||
Parameters(input): Parameters<DeleteInput>,
|
||||
ctx: RequestContext<RoleServer>,
|
||||
) -> Result<CallToolResult, rmcp::ErrorData> {
|
||||
let t = Instant::now();
|
||||
let user_id = Self::user_id_from_ctx(&ctx)?;
|
||||
tracing::info!(
|
||||
tool = "secrets_delete",
|
||||
?user_id,
|
||||
namespace = %input.namespace,
|
||||
kind = input.kind.as_deref(),
|
||||
name = input.name.as_deref(),
|
||||
dry_run = input.dry_run.unwrap_or(false),
|
||||
"tool call start",
|
||||
);
|
||||
|
||||
let result = svc_delete(
|
||||
&self.pool,
|
||||
@@ -460,8 +564,18 @@ impl SecretsService {
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
.map_err(|e| {
|
||||
tracing::warn!(tool = "secrets_delete", ?user_id, error = %e, "tool call failed");
|
||||
rmcp::ErrorData::internal_error(e.to_string(), None)
|
||||
})?;
|
||||
|
||||
tracing::info!(
|
||||
tool = "secrets_delete",
|
||||
?user_id,
|
||||
namespace = %input.namespace,
|
||||
elapsed_ms = t.elapsed().as_millis(),
|
||||
"tool call ok",
|
||||
);
|
||||
let json = serde_json::to_string_pretty(&result).unwrap_or_default();
|
||||
Ok(CallToolResult::success(vec![Content::text(json)]))
|
||||
}
|
||||
@@ -475,7 +589,17 @@ impl SecretsService {
|
||||
Parameters(input): Parameters<HistoryInput>,
|
||||
ctx: RequestContext<RoleServer>,
|
||||
) -> Result<CallToolResult, rmcp::ErrorData> {
|
||||
let t = Instant::now();
|
||||
let user_id = Self::user_id_from_ctx(&ctx)?;
|
||||
tracing::info!(
|
||||
tool = "secrets_history",
|
||||
?user_id,
|
||||
namespace = %input.namespace,
|
||||
kind = %input.kind,
|
||||
name = %input.name,
|
||||
"tool call start",
|
||||
);
|
||||
|
||||
let result = svc_history(
|
||||
&self.pool,
|
||||
&input.namespace,
|
||||
@@ -485,8 +609,17 @@ impl SecretsService {
|
||||
user_id,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
.map_err(|e| {
|
||||
tracing::warn!(tool = "secrets_history", ?user_id, error = %e, "tool call failed");
|
||||
rmcp::ErrorData::internal_error(e.to_string(), None)
|
||||
})?;
|
||||
|
||||
tracing::info!(
|
||||
tool = "secrets_history",
|
||||
?user_id,
|
||||
elapsed_ms = t.elapsed().as_millis(),
|
||||
"tool call ok",
|
||||
);
|
||||
let json = serde_json::to_string_pretty(&result).unwrap_or_default();
|
||||
Ok(CallToolResult::success(vec![Content::text(json)]))
|
||||
}
|
||||
@@ -500,7 +633,17 @@ impl SecretsService {
|
||||
Parameters(input): Parameters<RollbackInput>,
|
||||
ctx: RequestContext<RoleServer>,
|
||||
) -> Result<CallToolResult, rmcp::ErrorData> {
|
||||
let t = Instant::now();
|
||||
let (user_id, user_key) = Self::require_user_and_key(&ctx)?;
|
||||
tracing::info!(
|
||||
tool = "secrets_rollback",
|
||||
?user_id,
|
||||
namespace = %input.namespace,
|
||||
kind = %input.kind,
|
||||
name = %input.name,
|
||||
to_version = input.to_version,
|
||||
"tool call start",
|
||||
);
|
||||
|
||||
let result = svc_rollback(
|
||||
&self.pool,
|
||||
@@ -512,8 +655,17 @@ impl SecretsService {
|
||||
Some(user_id),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
.map_err(|e| {
|
||||
tracing::warn!(tool = "secrets_rollback", ?user_id, error = %e, "tool call failed");
|
||||
rmcp::ErrorData::internal_error(e.to_string(), None)
|
||||
})?;
|
||||
|
||||
tracing::info!(
|
||||
tool = "secrets_rollback",
|
||||
?user_id,
|
||||
elapsed_ms = t.elapsed().as_millis(),
|
||||
"tool call ok",
|
||||
);
|
||||
let json = serde_json::to_string_pretty(&result).unwrap_or_default();
|
||||
Ok(CallToolResult::success(vec![Content::text(json)]))
|
||||
}
|
||||
@@ -527,9 +679,18 @@ impl SecretsService {
|
||||
Parameters(input): Parameters<ExportInput>,
|
||||
ctx: RequestContext<RoleServer>,
|
||||
) -> Result<CallToolResult, rmcp::ErrorData> {
|
||||
let t = Instant::now();
|
||||
let (user_id, user_key) = Self::require_user_and_key(&ctx)?;
|
||||
let tags = input.tags.unwrap_or_default();
|
||||
let format = input.format.as_deref().unwrap_or("json");
|
||||
tracing::info!(
|
||||
tool = "secrets_export",
|
||||
?user_id,
|
||||
namespace = input.namespace.as_deref(),
|
||||
kind = input.kind.as_deref(),
|
||||
format,
|
||||
"tool call start",
|
||||
);
|
||||
|
||||
let data = svc_export(
|
||||
&self.pool,
|
||||
@@ -545,13 +706,23 @@ impl SecretsService {
|
||||
Some(&user_key),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
.map_err(|e| {
|
||||
tracing::warn!(tool = "secrets_export", ?user_id, error = %e, "tool call failed");
|
||||
rmcp::ErrorData::internal_error(e.to_string(), None)
|
||||
})?;
|
||||
|
||||
let serialized = format
|
||||
.parse::<secrets_core::models::ExportFormat>()
|
||||
.and_then(|fmt| fmt.serialize(&data))
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
|
||||
tracing::info!(
|
||||
tool = "secrets_export",
|
||||
?user_id,
|
||||
entry_count = data.entries.len(),
|
||||
elapsed_ms = t.elapsed().as_millis(),
|
||||
"tool call ok",
|
||||
);
|
||||
Ok(CallToolResult::success(vec![Content::text(serialized)]))
|
||||
}
|
||||
|
||||
@@ -565,9 +736,18 @@ impl SecretsService {
|
||||
Parameters(input): Parameters<EnvMapInput>,
|
||||
ctx: RequestContext<RoleServer>,
|
||||
) -> Result<CallToolResult, rmcp::ErrorData> {
|
||||
let t = Instant::now();
|
||||
let (user_id, user_key) = Self::require_user_and_key(&ctx)?;
|
||||
let tags = input.tags.unwrap_or_default();
|
||||
let only_fields = input.only_fields.unwrap_or_default();
|
||||
tracing::info!(
|
||||
tool = "secrets_env_map",
|
||||
?user_id,
|
||||
namespace = input.namespace.as_deref(),
|
||||
kind = input.kind.as_deref(),
|
||||
prefix = input.prefix.as_deref().unwrap_or(""),
|
||||
"tool call start",
|
||||
);
|
||||
|
||||
let env_map = secrets_core::service::env_map::build_env_map(
|
||||
&self.pool,
|
||||
@@ -581,8 +761,19 @@ impl SecretsService {
|
||||
Some(user_id),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| rmcp::ErrorData::internal_error(e.to_string(), None))?;
|
||||
.map_err(|e| {
|
||||
tracing::warn!(tool = "secrets_env_map", ?user_id, error = %e, "tool call failed");
|
||||
rmcp::ErrorData::internal_error(e.to_string(), None)
|
||||
})?;
|
||||
|
||||
let entry_count = env_map.len();
|
||||
tracing::info!(
|
||||
tool = "secrets_env_map",
|
||||
?user_id,
|
||||
entry_count,
|
||||
elapsed_ms = t.elapsed().as_millis(),
|
||||
"tool call ok",
|
||||
);
|
||||
let json = serde_json::to_string_pretty(&env_map).unwrap_or_default();
|
||||
Ok(CallToolResult::success(vec![Content::text(json)]))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user