Skip to content

Commit 91ed867

Browse files
committed
fix tests
1 parent 72674e9 commit 91ed867

31 files changed

+874
-991
lines changed

.claude/settings.local.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
"Bash(find:*)",
66
"Bash(grep:*)",
77
"Bash(uv run pytest tests/test_get_unknown_or_stdio_session.py -v)",
8-
"Bash(uv run:*)"
8+
"Bash(uv run:*)",
9+
"Bash(rg:*)",
10+
"Bash(/Users/naseemalnaji/.nvm/versions/node/v23.5.0/lib/node_modules/@anthropic-ai/claude-code/vendor/ripgrep/arm64-darwin/rg \"CallToolResult\" --type py -A 3 -B 3)",
11+
"Bash(/Users/naseemalnaji/.nvm/versions/node/v23.5.0/lib/node_modules/@anthropic-ai/claude-code/vendor/ripgrep/arm64-darwin/rg \"from mcp.types import.*CallToolResult\" --type py)",
12+
"Bash(pip show:*)"
913
],
1014
"deny": []
1115
}

examples/basic_usage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Basic usage example for MCPCat with FastMCP."""
22

33
from fastmcp import FastMCP
4-
from mcpcat import track, MCPCatOptions
54

5+
from mcpcat import MCPCatOptions, track
66

77
# Create a FastMCP server
88
mcp = FastMCP("example-server")
@@ -37,4 +37,4 @@ def get_weather(city: str) -> str:
3737

3838
# Run the server
3939
if __name__ == "__main__":
40-
mcp.run()
40+
mcp.run()

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ classifiers = [
1919
]
2020

2121
dependencies = [
22+
"datafog>=4.1.1",
2223
"mcp>=1.0.0",
24+
"pydantic>=2.0.0",
2325
]
2426

2527
[project.optional-dependencies]
@@ -82,4 +84,4 @@ exclude = [
8284
]
8385

8486
[tool.ruff.isort]
85-
known-first-party = ["mcpcat"]
87+
known-first-party = ["mcpcat"]

src/mcpcat/__init__.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
__version__ = "0.1.0"
1212

1313

14-
def track(server: Any, options: Optional[MCPCatOptions] = None) -> Any:
14+
def track(server: Any, options: MCPCatOptions | None = None) -> Any:
1515
"""
1616
Enable analytics tracking for an MCP server.
1717
@@ -28,13 +28,13 @@ def track(server: Any, options: Optional[MCPCatOptions] = None) -> Any:
2828
# Use default options if not provided
2929
if options is None:
3030
options = MCPCatOptions()
31-
31+
3232
# Validate server compatibility
3333
if not is_compatible_server(server):
3434
raise TypeError(
3535
"Server must be a FastMCP instance or implement the MCP server protocol"
3636
)
37-
37+
3838
# Check if already tracked
3939
if has_mcpcat_data(server):
4040
log_info(
@@ -43,15 +43,15 @@ def track(server: Any, options: Optional[MCPCatOptions] = None) -> Any:
4343
options
4444
)
4545
return server
46-
46+
4747
# Create and store tracking data
4848
data = MCPCatData(options=options)
4949
set_mcpcat_data(server, data)
50-
50+
5151
try:
5252
# Set up tool handlers
5353
setup_tool_handlers(server, data)
54-
54+
5555
# Log initialization
5656
log_info(
5757
"MCPCat tracking initialized",
@@ -70,36 +70,36 @@ def track(server: Any, options: Optional[MCPCatOptions] = None) -> Any:
7070
if has_mcpcat_data(server):
7171
# Remove from tracking
7272
get_mcpcat_data(server) # This will remove the weak reference
73-
73+
7474
log_error(
7575
"Failed to initialize MCPCat",
7676
e,
7777
{"server": server.__class__.__name__},
7878
options
7979
)
80-
80+
8181
raise
82-
82+
8383
# Return the server (like TypeScript version)
8484
return server
8585

8686

8787
def get_unknown_or_stdio_session(server: Any) -> dict:
8888
"""Get or create an unknown/STDIO session for the server."""
8989
from .modules.session import get_or_create_unknown_session
90-
90+
9191
data = get_mcpcat_data(server)
9292
if not data:
9393
raise Exception("Server tracking data not found")
94-
94+
9595
session_id = get_or_create_unknown_session(data)
9696
return {
9797
"sessionId": session_id,
9898
"expiresIn": data.unknown_session.last_used if data.unknown_session else None
9999
}
100100

101101

102-
def _getServerTrackingData(server: Any) -> Optional[MCPCatData]:
102+
def _getServerTrackingData(server: Any) -> MCPCatData | None:
103103
"""Get server tracking data (for testing)."""
104104
return get_mcpcat_data(server)
105105

@@ -108,19 +108,19 @@ async def handleReportMissing(arguments: dict) -> None:
108108
"""Handle report missing tool call (for testing)."""
109109
from .modules.logging import log_info
110110
from .types import MCPCatOptions
111-
111+
112112
# Create minimal options for logging
113113
options = MCPCatOptions()
114-
114+
115115
log_info("Missing tool reported", arguments, options)
116116

117117

118118
__all__ = [
119119
"track",
120120
"get_unknown_or_stdio_session",
121-
"_getServerTrackingData",
121+
"_getServerTrackingData",
122122
"handleReportMissing",
123-
"MCPCatOptions",
123+
"MCPCatOptions",
124124
"UserData",
125125
"__version__",
126-
]
126+
]

src/mcpcat/modules/__init__.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
"""MCPCat modules."""
22

33
from .compatibility import is_compatible_server, is_fastmcp_server
4-
from .context_parameters import add_context_parameter_to_schema, add_context_parameter_to_tools
4+
from .context_parameters import (
5+
add_context_parameter_to_schema,
6+
add_context_parameter_to_tools,
7+
)
58
from .internal import get_mcpcat_data, has_mcpcat_data, set_mcpcat_data
6-
from .logging import log_error, log_info, log_tool_call, log_warning
7-
from .redaction import redact_sensitive_info
8-
from .session import get_unknown_or_stdio_session, capture_session_info
9+
from .logging import log_error, log_info, log_trace, log_warning
10+
from .session import capture_session_info, get_unknown_or_stdio_session
911
from .tools import handle_report_missing, setup_tool_handlers
10-
from .tracing import trace_tool_call
12+
from .tracing import record_trace
1113

1214
__all__ = [
1315
# Compatibility
1416
"is_compatible_server",
1517
"is_fastmcp_server",
1618
# Context parameters
17-
"add_context_parameter_to_schema",
19+
"add_context_parameter_to_schema",
1820
"add_context_parameter_to_tools",
1921
# Internal
2022
"get_mcpcat_data",
@@ -23,16 +25,15 @@
2325
# Logging
2426
"log_error",
2527
"log_info",
26-
"log_tool_call",
28+
"log_trace",
2729
"log_warning",
2830
# Redaction
29-
"redact_sensitive_info",
3031
# Session
3132
"get_unknown_or_stdio_session",
3233
"capture_session_info",
3334
# Tools
3435
"handle_report_missing",
3536
"setup_tool_handlers",
3637
# Tracing
37-
"trace_tool_call",
38-
]
38+
"record_trace",
39+
]

src/mcpcat/modules/compatibility.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
@runtime_checkable
77
class MCPServerProtocol(Protocol):
88
"""Protocol for MCP server compatibility."""
9-
9+
1010
def list_tools(self) -> Any:
1111
"""List available tools."""
1212
...
13-
13+
1414
def call_tool(self, name: str, arguments: dict) -> Any:
1515
"""Call a tool by name."""
1616
...
@@ -21,12 +21,12 @@ def is_fastmcp_server(server: Any) -> bool:
2121
# Check for FastMCP class name or specific attributes
2222
return (
2323
server.__class__.__name__ == "FastMCP" or
24-
(hasattr(server, "list_tools") and
24+
(hasattr(server, "list_tools") and
2525
hasattr(server, "call_tool") and
2626
hasattr(server, "_tool_manager"))
2727
)
2828

2929

3030
def is_compatible_server(server: Any) -> bool:
3131
"""Check if the server is compatible with MCPCat."""
32-
return is_fastmcp_server(server) or isinstance(server, MCPServerProtocol)
32+
return is_fastmcp_server(server) or isinstance(server, MCPServerProtocol)
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
"""Context parameter injection for MCP tools."""
22

3-
from typing import Any, Dict, List
3+
from typing import Any
44

55

6-
def add_context_parameter_to_tools(tools: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
6+
def add_context_parameter_to_tools(tools: list[dict[str, Any]]) -> list[dict[str, Any]]:
77
"""Add context parameter to tool schemas."""
88
modified_tools = []
9-
9+
1010
for tool in tools:
1111
# Create a copy to avoid modifying original
1212
modified_tool = tool.copy()
13-
13+
1414
if "inputSchema" in modified_tool:
1515
modified_tool["inputSchema"] = add_context_parameter_to_schema(
1616
modified_tool["inputSchema"]
1717
)
18-
18+
1919
modified_tools.append(modified_tool)
20-
20+
2121
return modified_tools
2222

2323

24-
def add_context_parameter_to_schema(schema: Dict[str, Any]) -> Dict[str, Any]:
24+
def add_context_parameter_to_schema(schema: dict[str, Any]) -> dict[str, Any]:
2525
"""Add context parameter to a JSON schema."""
2626
# Create a copy to avoid modifying original
2727
modified_schema = schema.copy()
28-
28+
2929
# Ensure properties exists
3030
if "properties" not in modified_schema:
3131
modified_schema["properties"] = {}
3232
else:
3333
# Deep copy properties
3434
modified_schema["properties"] = modified_schema["properties"].copy()
35-
35+
3636
# Add context parameter
3737
modified_schema["properties"]["context"] = {
3838
"type": "string",
3939
"description": "Describe why you are calling this tool and how it fits into your overall task"
4040
}
41-
41+
4242
# Add to required fields
4343
if "required" not in modified_schema:
4444
modified_schema["required"] = []
4545
else:
4646
# Copy required list
4747
modified_schema["required"] = list(modified_schema["required"])
48-
48+
4949
if "context" not in modified_schema["required"]:
5050
modified_schema["required"].append("context")
51-
52-
return modified_schema
51+
52+
return modified_schema

src/mcpcat/modules/internal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Internal data storage for MCPCat."""
22

33
import weakref
4-
from typing import Any, Optional
4+
from typing import Any
55

66
from ..types import MCPCatData
77

@@ -14,11 +14,11 @@ def set_mcpcat_data(server: Any, data: MCPCatData) -> None:
1414
_server_data_map[server] = data
1515

1616

17-
def get_mcpcat_data(server: Any) -> Optional[MCPCatData]:
17+
def get_mcpcat_data(server: Any) -> MCPCatData | None:
1818
"""Retrieve MCPCat data for a server instance."""
1919
return _server_data_map.get(server)
2020

2121

2222
def has_mcpcat_data(server: Any) -> bool:
2323
"""Check if a server instance has MCPCat data."""
24-
return server in _server_data_map
24+
return server in _server_data_map

0 commit comments

Comments
 (0)