Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
175 lines (144 loc) · 5.7 KB
/
Copy pathmain.py
File metadata and controls
175 lines (144 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
"""
YouTrack MCP Server - A Model Context Protocol server for JetBrains YouTrack.
Uses MCPServer for stdio, SSE, and streamable HTTP transports.
"""
import functools
import inspect
import json
import logging
import os
import stat
import sys
from mcp.server.mcpserver import MCPServer
from youtrack_mcp.version import __version__ as APP_VERSION
from youtrack_mcp.config import config
from youtrack_mcp.tools.loader import load_all_tools
# Set up logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler(sys.stderr)],
)
logger = logging.getLogger(__name__)
# MCP SDK names the remote HTTP transport "streamable-http".
# Accept "http" as an alias so the npm --http flag keeps working.
_HTTP_TRANSPORTS = {"http", "streamable-http"}
_HTTP_RUN_TRANSPORTS = {"sse", "streamable-http"}
def _flatten_result(result):
"""Collapse JSON-string returns and MCP resource envelopes into plain objects.
Tools historically return JSON *strings*, which MCPServer wraps as
{"result": "<escaped json>"} — clients end up with double/triple-encoded
output. Parsing here (and unwrapping {"contents":[{"text": ...}]} resource
envelopes) lets MCPServer serialize the object exactly once.
"""
if isinstance(result, str):
try:
parsed = json.loads(result)
except (ValueError, TypeError):
return {"text": result}
if isinstance(parsed, (dict, list)):
return _flatten_result(parsed)
return {"result": parsed}
if isinstance(result, dict):
contents = result.get("contents")
if (
set(result.keys()) == {"contents"}
and isinstance(contents, list)
and len(contents) == 1
and isinstance(contents[0], dict)
and "text" in contents[0]
):
return _flatten_result(contents[0]["text"])
return result
if isinstance(result, list):
return {"items": result, "count": len(result)}
return {"result": result}
def _clean_tool(func):
"""Wrap a tool so it returns a parsed dict instead of a JSON string."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
return _flatten_result(func(*args, **kwargs))
sig = inspect.signature(func)
wrapper.__signature__ = sig.replace(return_annotation=dict)
return wrapper
def create_server() -> MCPServer:
"""Create and configure the MCPServer with all tools registered."""
mcp = MCPServer(
config.MCP_SERVER_NAME,
instructions=config.MCP_SERVER_DESCRIPTION,
version=APP_VERSION,
)
# Load and register all tools
tools = load_all_tools()
for name, func in tools.items():
mcp.add_tool(_clean_tool(func), name=name)
logger.info(f"Registered {len(tools)} tools with MCPServer")
return mcp
def _normalize_transport(transport: str) -> str:
if transport in _HTTP_TRANSPORTS:
return "streamable-http"
return transport
def _stdin_is_mcp_client() -> bool:
"""True when stdin is a pipe/socket from an MCP host (docker -i, Claude, Cursor)."""
try:
mode = os.fstat(sys.stdin.fileno()).st_mode
except OSError:
return False
return stat.S_ISFIFO(mode) or stat.S_ISSOCK(mode)
def _should_fallback_to_http() -> bool:
"""True when stdio would just EOF (Compose, docker without -i), not a TTY or pipe."""
if _stdin_is_mcp_client():
return False
try:
return not sys.stdin.isatty()
except (OSError, ValueError):
return True
def main():
"""Run the MCP server."""
import argparse
parser = argparse.ArgumentParser(description="YouTrack MCP Server")
parser.add_argument(
"--transport",
choices=["stdio", "sse", "streamable-http", "http"],
default=None,
help="Transport mode (default: from TRANSPORT env var, fallback stdio)",
)
parser.add_argument("--host", default="0.0.0.0", help="Host for SSE / streamable HTTP")
parser.add_argument("--port", type=int, default=None, help="Port for SSE / streamable HTTP")
parser.add_argument("--version", action="store_true", help="Show version and exit")
parser.add_argument("--log-level", default="INFO",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"])
args = parser.parse_args()
if args.version:
print(f"YouTrack MCP Server v{APP_VERSION}")
sys.exit(0)
logging.getLogger().setLevel(getattr(logging, args.log_level))
# Determine transport: CLI arg > env var > default stdio
transport = _normalize_transport(args.transport or os.getenv("TRANSPORT", "stdio"))
port = args.port or int(os.getenv("PORT", "8000"))
# Docker Compose (no stdin) looks like a crash: stdio starts, then EOF exits.
# Switch to streamable HTTP so a long-running service actually stays up.
# Explicit --transport stdio skips this (Dockerfile ENV TRANSPORT=stdio does not).
if (
transport == "stdio"
and args.transport != "stdio"
and _should_fallback_to_http()
):
logger.warning(
"No MCP client on stdin (typical of docker compose without -i). "
"Switching to streamable-http on 0.0.0.0:%s. "
"Pass --transport stdio to keep stdio, or set TRANSPORT=streamable-http "
"and publish port %s.",
port,
port,
)
transport = "streamable-http"
logger.info(f"Starting YouTrack MCP Server v{APP_VERSION} [{transport}]")
mcp = create_server()
if transport in _HTTP_RUN_TRANSPORTS:
mcp.run(transport=transport, host=args.host, port=port)
else:
mcp.run(transport=transport)
if __name__ == "__main__":
main()