Skip to content

Commit 94d8091

Browse files
committed
feat: add Context Switching pattern tools (arango_switch_context, arango_get_active_context, arango_list_contexts)
1 parent df075db commit 94d8091

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

mcp_arangodb_async/handlers.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,176 @@ def handle_list_tools_by_category(
19661966
return result
19671967

19681968

1969+
# Pattern 2: Context Switching
1970+
# Define workflow contexts
1971+
WORKFLOW_CONTEXTS = {
1972+
"baseline": {
1973+
"description": "Minimal CRUD operations for basic database interaction",
1974+
"tools": [
1975+
ARANGO_QUERY, ARANGO_LIST_COLLECTIONS, ARANGO_INSERT,
1976+
ARANGO_UPDATE, ARANGO_REMOVE, ARANGO_CREATE_COLLECTION, ARANGO_BACKUP
1977+
]
1978+
},
1979+
"data_analysis": {
1980+
"description": "Query optimization and performance analysis",
1981+
"tools": [
1982+
ARANGO_QUERY, ARANGO_LIST_COLLECTIONS, ARANGO_EXPLAIN_QUERY,
1983+
ARANGO_QUERY_BUILDER, ARANGO_QUERY_PROFILE, ARANGO_LIST_INDEXES,
1984+
ARANGO_DATABASE_STATUS
1985+
]
1986+
},
1987+
"graph_modeling": {
1988+
"description": "Graph creation, traversal, and analysis",
1989+
"tools": [
1990+
ARANGO_CREATE_GRAPH, ARANGO_LIST_GRAPHS, ARANGO_ADD_VERTEX_COLLECTION,
1991+
ARANGO_ADD_EDGE_DEFINITION, ARANGO_ADD_EDGE, ARANGO_TRAVERSE,
1992+
ARANGO_SHORTEST_PATH, ARANGO_GRAPH_STATISTICS,
1993+
ARANGO_VALIDATE_GRAPH_INTEGRITY, ARANGO_QUERY
1994+
]
1995+
},
1996+
"bulk_operations": {
1997+
"description": "Batch processing and bulk data operations",
1998+
"tools": [
1999+
ARANGO_BULK_INSERT, ARANGO_BULK_UPDATE, ARANGO_INSERT_WITH_VALIDATION,
2000+
ARANGO_VALIDATE_REFERENCES, ARANGO_LIST_COLLECTIONS, ARANGO_QUERY
2001+
]
2002+
},
2003+
"schema_validation": {
2004+
"description": "Data integrity and schema management",
2005+
"tools": [
2006+
ARANGO_CREATE_SCHEMA, ARANGO_VALIDATE_DOCUMENT,
2007+
ARANGO_INSERT_WITH_VALIDATION, ARANGO_VALIDATE_REFERENCES,
2008+
ARANGO_VALIDATE_GRAPH_INTEGRITY, ARANGO_QUERY
2009+
]
2010+
},
2011+
"full": {
2012+
"description": "All available tools (fallback for complex workflows)",
2013+
"tools": list(TOOL_REGISTRY.keys())
2014+
}
2015+
}
2016+
2017+
# Global state for active context (in production, this would be per-session)
2018+
_ACTIVE_CONTEXT = "baseline"
2019+
2020+
2021+
@handle_errors
2022+
@register_tool(
2023+
name=ARANGO_SWITCH_CONTEXT,
2024+
description="Switch to a different workflow context with a predefined set of tools. Enables Context Switching pattern for workflow-specific tool sets.",
2025+
model=SwitchContextArgs,
2026+
)
2027+
def handle_switch_context(
2028+
db: StandardDatabase, args: Dict[str, Any]
2029+
) -> Dict[str, Any]:
2030+
"""Switch to a different workflow context.
2031+
2032+
This tool enables the Context Switching pattern by allowing AI agents to
2033+
switch between predefined tool sets optimized for specific workflows.
2034+
2035+
Args:
2036+
db: ArangoDB database instance (not used, but required for handler signature)
2037+
args: Validated SwitchContextArgs containing target context
2038+
2039+
Returns:
2040+
Dictionary with context switch details
2041+
"""
2042+
global _ACTIVE_CONTEXT
2043+
2044+
new_context = args["context"]
2045+
old_context = _ACTIVE_CONTEXT
2046+
2047+
if new_context not in WORKFLOW_CONTEXTS:
2048+
return {
2049+
"error": f"Unknown context: {new_context}",
2050+
"available_contexts": list(WORKFLOW_CONTEXTS.keys())
2051+
}
2052+
2053+
old_tools = set(WORKFLOW_CONTEXTS[old_context]["tools"])
2054+
new_tools = set(WORKFLOW_CONTEXTS[new_context]["tools"])
2055+
2056+
tools_added = list(new_tools - old_tools)
2057+
tools_removed = list(old_tools - new_tools)
2058+
2059+
_ACTIVE_CONTEXT = new_context
2060+
2061+
return {
2062+
"from_context": old_context,
2063+
"to_context": new_context,
2064+
"description": WORKFLOW_CONTEXTS[new_context]["description"],
2065+
"tools_added": tools_added,
2066+
"tools_removed": tools_removed,
2067+
"total_tools": len(new_tools),
2068+
"active_tools": list(new_tools)
2069+
}
2070+
2071+
2072+
@handle_errors
2073+
@register_tool(
2074+
name=ARANGO_GET_ACTIVE_CONTEXT,
2075+
description="Get the currently active workflow context and its tool set.",
2076+
model=GetActiveContextArgs,
2077+
)
2078+
def handle_get_active_context(
2079+
db: StandardDatabase, args: Optional[Dict[str, Any]] = None
2080+
) -> Dict[str, Any]:
2081+
"""Get the currently active workflow context.
2082+
2083+
Args:
2084+
db: ArangoDB database instance (not used, but required for handler signature)
2085+
args: No arguments required
2086+
2087+
Returns:
2088+
Dictionary with active context details
2089+
"""
2090+
global _ACTIVE_CONTEXT
2091+
2092+
context_info = WORKFLOW_CONTEXTS[_ACTIVE_CONTEXT]
2093+
2094+
return {
2095+
"active_context": _ACTIVE_CONTEXT,
2096+
"description": context_info["description"],
2097+
"tools": context_info["tools"],
2098+
"tool_count": len(context_info["tools"])
2099+
}
2100+
2101+
2102+
@handle_errors
2103+
@register_tool(
2104+
name=ARANGO_LIST_CONTEXTS,
2105+
description="List all available workflow contexts with their descriptions and optional tool lists.",
2106+
model=ListContextsArgs,
2107+
)
2108+
def handle_list_contexts(
2109+
db: StandardDatabase, args: Dict[str, Any]
2110+
) -> Dict[str, Any]:
2111+
"""List all available workflow contexts.
2112+
2113+
Args:
2114+
db: ArangoDB database instance (not used, but required for handler signature)
2115+
args: Validated ListContextsArgs with include_tools flag
2116+
2117+
Returns:
2118+
Dictionary with all available contexts
2119+
"""
2120+
include_tools = args.get("include_tools", False)
2121+
2122+
contexts = {}
2123+
for context_name, context_info in WORKFLOW_CONTEXTS.items():
2124+
contexts[context_name] = {
2125+
"description": context_info["description"],
2126+
"tool_count": len(context_info["tools"])
2127+
}
2128+
2129+
if include_tools:
2130+
contexts[context_name]["tools"] = context_info["tools"]
2131+
2132+
return {
2133+
"contexts": contexts,
2134+
"total_contexts": len(contexts),
2135+
"active_context": _ACTIVE_CONTEXT
2136+
}
2137+
2138+
19692139
# Register aliases for backward compatibility
19702140
from .tool_registry import ToolRegistration
19712141

0 commit comments

Comments
 (0)