feat: Phase 3 — MCP header fallback, LIKE-based consent window, and auth token fix for dsh integration
- X-Oikos-Session-Id header → context fallback for _session_id - sessionIDFromArgsOrContext() reads from args, then header - AssentWindowActive falls back to session-ID-only LIKE lookup - windowActiveLike() for LIKE-pattern autonomy_settings queries - mcpBearerToken: package-level resolved token replaces os.Getenv() in decide_approval - Bump 0.36.0 → 0.37.0
This commit is contained in:
@@ -40,7 +40,7 @@ func OpsTools(pool *db.Pool, agentID uuid.UUID, sec ports.Secrets, execSvc *app.
|
||||
command, _ := args["command"].(string)
|
||||
purpose, _ := args["purpose"].(string)
|
||||
declaredRisk, _ := args["declared_risk"].(string)
|
||||
sessionID, _ := args["_session_id"].(string)
|
||||
sessionID := sessionIDFromArgsOrContext(ctx, args)
|
||||
if targetSlug == "" || command == "" {
|
||||
return textResult("error: target and command are required"), nil
|
||||
}
|
||||
@@ -71,7 +71,7 @@ func OpsTools(pool *db.Pool, agentID uuid.UUID, sec ports.Secrets, execSvc *app.
|
||||
command, _ := args["command"].(string)
|
||||
purpose, _ := args["purpose"].(string)
|
||||
declaredRisk, _ := args["declared_risk"].(string)
|
||||
sessionID, _ := args["_session_id"].(string)
|
||||
sessionID := sessionIDFromArgsOrContext(ctx, args)
|
||||
|
||||
if lxcSlug == "" || container == "" || command == "" {
|
||||
return textResult("error: lxc_slug, container, and command are required"), nil
|
||||
@@ -662,7 +662,7 @@ func OpsTools(pool *db.Pool, agentID uuid.UUID, sec ports.Secrets, execSvc *app.
|
||||
if targetSlug == "" || service == "" {
|
||||
return textResult("error: target and service are required"), nil
|
||||
}
|
||||
sessionID, _ := args["_session_id"].(string)
|
||||
sessionID := sessionIDFromArgsOrContext(ctx, args)
|
||||
var targetID uuid.UUID
|
||||
if err := pool.QueryRow(ctx, "SELECT id FROM entities WHERE slug = $1", targetSlug).Scan(&targetID); err != nil {
|
||||
return textResult(fmt.Sprintf("target not found: %s", targetSlug)), nil
|
||||
@@ -717,7 +717,7 @@ func OpsTools(pool *db.Pool, agentID uuid.UUID, sec ports.Secrets, execSvc *app.
|
||||
if backup {
|
||||
cmd = fmt.Sprintf("pct exec %s -- cp -n %s %s.bak 2>/dev/null || true; %s", pveID, destPath, destPath, cmd)
|
||||
}
|
||||
sessionID, _ := args["_session_id"].(string)
|
||||
sessionID := sessionIDFromArgsOrContext(ctx, args)
|
||||
var hostEntityID uuid.UUID
|
||||
if err := pool.QueryRow(ctx, "SELECT id FROM entities WHERE slug = $1", "host:"+hostSlug).Scan(&hostEntityID); err != nil {
|
||||
// hostSlug may already carry the host: prefix
|
||||
@@ -800,8 +800,8 @@ func OpsTools(pool *db.Pool, agentID uuid.UUID, sec ports.Secrets, execSvc *app.
|
||||
return textResult(fmt.Sprintf("error: %v", hreqErr)), nil
|
||||
}
|
||||
hreq.Header.Set("Content-Type", "application/json")
|
||||
if token := os.Getenv("OIKOS_MCP_BEARER_TOKEN"); token != "" {
|
||||
hreq.Header.Set("Authorization", "Bearer "+token)
|
||||
if mcpBearerToken != "" {
|
||||
hreq.Header.Set("Authorization", "Bearer "+mcpBearerToken)
|
||||
}
|
||||
resp, reqErr := client.Do(hreq)
|
||||
if reqErr != nil {
|
||||
|
||||
@@ -53,6 +53,7 @@ func objSchema(props ...prop) *jsonschema.Schema {
|
||||
// NewHandler creates an http.Handler that serves the Oikos MCP server.
|
||||
// agentID is the Nomos agent entity UUID; tool calls are logged to agent_activity.
|
||||
func NewHandler(pool *db.Pool, token string, agentID uuid.UUID, sec ports.Secrets, entities *app.EntityService, relService *app.RelationshipService, execSvc *app.ExecutionService) http.Handler {
|
||||
mcpBearerToken = token
|
||||
s := newServer(pool, agentID, sec, entities, relService, execSvc)
|
||||
handler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server {
|
||||
if token != "" {
|
||||
@@ -62,7 +63,35 @@ func NewHandler(pool *db.Pool, token string, agentID uuid.UUID, sec ports.Secret
|
||||
}
|
||||
return s
|
||||
}, nil)
|
||||
return handler
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Extract X-Oikos-Session-Id from headers and store in context.
|
||||
if sid := r.Header.Get("X-Oikos-Session-Id"); sid != "" {
|
||||
r = r.WithContext(context.WithValue(r.Context(), ctxSessionIDKey{}, sid))
|
||||
}
|
||||
handler.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// ctxSessionIDKey is a Go context key for X-Oikos-Session-Id header value.
|
||||
type ctxSessionIDKey struct{}
|
||||
|
||||
// mcpBearerToken is the resolved MCP bearer token, set at startup from the
|
||||
// config (after Infisical overlay). Used by decide_approval and similar
|
||||
// handlers that call back into the oikos HTTP API.
|
||||
var mcpBearerToken string
|
||||
|
||||
// sessionIDFromArgsOrContext returns _session_id from tool call args,
|
||||
// falling back to the X-Oikos-Session-Id header injected into the request
|
||||
// context. This lets dsh agents send the session ID as a header without
|
||||
// injecting it into every tool call's arguments.
|
||||
func sessionIDFromArgsOrContext(ctx context.Context, args map[string]any) string {
|
||||
if sid, _ := args["_session_id"].(string); sid != "" {
|
||||
return sid
|
||||
}
|
||||
if sid, ok := ctx.Value(ctxSessionIDKey{}).(string); ok {
|
||||
return sid
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// toolHandler is the function signature registered via AddTool.
|
||||
|
||||
Reference in New Issue
Block a user