Coverage for app / routers / mcp.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1from fastapi import APIRouter 

2from pydantic import BaseModel 

3from typing import Any 

4 

5from app.mcp.loader import probe_mcp_server 

6from app.mcp.registry import BUILTIN_MCP_SERVERS, MCPServerTemplate 

7 

8router = APIRouter(prefix="/mcp", tags=["MCP Servers"]) 

9 

10 

11class MCPConfigFieldResponse(BaseModel): 

12 key: str 

13 label: str 

14 type: str 

15 description: str 

16 required: bool 

17 default: Any 

18 placeholder: str 

19 

20 

21class MCPServerTemplateResponse(BaseModel): 

22 key: str 

23 name: str 

24 description: str 

25 connection_type: str 

26 icon_hint: str 

27 category: str 

28 config_fields: list[MCPConfigFieldResponse] 

29 tools_preview: list[str] 

30 docs_url: str 

31 

32 

33class MCPServerTemplatesResponse(BaseModel): 

34 templates: list[MCPServerTemplateResponse] 

35 

36 

37def _serialise(t: MCPServerTemplate) -> MCPServerTemplateResponse: 

38 return MCPServerTemplateResponse( 

39 key=t.key, 

40 name=t.name, 

41 description=t.description, 

42 connection_type=t.connection_type, 

43 icon_hint=t.icon_hint, 

44 category=t.category, 

45 config_fields=[ 

46 MCPConfigFieldResponse( 

47 key=f.key, 

48 label=f.label, 

49 type=f.type, 

50 description=f.description, 

51 required=f.required, 

52 default=f.default, 

53 placeholder=f.placeholder, 

54 ) 

55 for f in t.config_fields 

56 ], 

57 tools_preview=t.tools_preview, 

58 docs_url=t.docs_url, 

59 ) 

60 

61 

62@router.get("/servers/templates", response_model=MCPServerTemplatesResponse) 

63def list_mcp_server_templates(): 

64 """Return all built-in MCP server templates available for users to configure.""" 

65 return MCPServerTemplatesResponse( 

66 templates=[_serialise(t) for t in BUILTIN_MCP_SERVERS.values()] 

67 ) 

68 

69 

70# --------------------------------------------------------------------------- 

71# Probe — connection test + tool discovery 

72# --------------------------------------------------------------------------- 

73 

74 

75class MCPProbeRequest(BaseModel): 

76 """One MCP server descriptor to connect to and enumerate.""" 

77 

78 name: str = "probe" 

79 transport: str # "http" | "sse" | "stdio" 

80 config: dict[str, Any] = {} 

81 

82 

83class MCPProbeToolInfo(BaseModel): 

84 name: str 

85 description: str = "" 

86 

87 

88class MCPProbeResponse(BaseModel): 

89 ok: bool 

90 tools: list[MCPProbeToolInfo] = [] 

91 error: str = "" 

92 

93 

94@router.post("/servers/probe", response_model=MCPProbeResponse) 

95async def probe_server(body: MCPProbeRequest): 

96 """Connect to an MCP server descriptor and return its exposed tools. 

97 

98 Never raises for a bad/unreachable server — the failure reason comes 

99 back in ``error`` so svc-core can surface it to the user verbatim. 

100 The connection is bounded by ``mcp_connect_timeout_s`` and the same 

101 transport policy (stdio allowlist, URL validation) as agent execution. 

102 """ 

103 result = await probe_mcp_server(body.model_dump()) 

104 return MCPProbeResponse( 

105 ok=result.ok, 

106 tools=[MCPProbeToolInfo(**t) for t in result.tools], 

107 error=result.error, 

108 )