4
4
from __future__ import annotations
5
5
6
6
import asyncio
7
+ import os
7
8
from typing import TypedDict
8
9
9
10
import click
15
16
# Import logging configuration first to intercept all logging
16
17
from gitingest .utils .logging_config import get_logger
17
18
19
+ # Optional MCP imports
20
+ try :
21
+ from gitingest .mcp_server import start_mcp_server
22
+ from mcp_server .main import start_mcp_server_tcp
23
+
24
+ MCP_AVAILABLE = True
25
+ except ImportError :
26
+ MCP_AVAILABLE = False
27
+
18
28
# Initialize logger for this module
19
29
logger = get_logger (__name__ )
20
30
@@ -29,7 +39,10 @@ class _CLIArgs(TypedDict):
29
39
include_submodules : bool
30
40
token : str | None
31
41
output : str | None
32
- mcp_server : bool
42
+ mcp : bool
43
+ transport : str
44
+ host : str
45
+ port : int
33
46
34
47
35
48
@click .command ()
@@ -78,11 +91,31 @@ class _CLIArgs(TypedDict):
78
91
help = "Output file path (default: digest.txt in current directory). Use '-' for stdout." ,
79
92
)
80
93
@click .option (
81
- "--mcp-server " ,
94
+ "--mcp" ,
82
95
is_flag = True ,
83
96
default = False ,
84
97
help = "Start the MCP (Model Context Protocol) server for LLM integration" ,
85
98
)
99
+ @click .option (
100
+ "--transport" ,
101
+ type = click .Choice (["stdio" , "tcp" ]),
102
+ default = "stdio" ,
103
+ show_default = True ,
104
+ help = "Transport protocol for MCP communication (only used with --mcp)" ,
105
+ )
106
+ @click .option (
107
+ "--host" ,
108
+ default = "127.0.0.1" ,
109
+ show_default = True ,
110
+ help = "Host to bind TCP server (only used with --mcp --transport tcp)" ,
111
+ )
112
+ @click .option (
113
+ "--port" ,
114
+ type = int ,
115
+ default = 8001 ,
116
+ show_default = True ,
117
+ help = "Port for TCP server (only used with --mcp --transport tcp)" ,
118
+ )
86
119
def main (** cli_kwargs : Unpack [_CLIArgs ]) -> None :
87
120
"""Run the CLI entry point to analyze a repo / directory and dump its contents.
88
121
@@ -107,7 +140,8 @@ def main(**cli_kwargs: Unpack[_CLIArgs]) -> None:
107
140
$ gitingest https://github.com/user/repo --output -
108
141
109
142
MCP server mode:
110
- $ gitingest --mcp-server
143
+ $ gitingest --mcp
144
+ $ gitingest --mcp --transport tcp --host 0.0.0.0 --port 8001
111
145
112
146
With filtering:
113
147
$ gitingest -i "*.py" -e "*.log"
@@ -135,7 +169,10 @@ async def _async_main(
135
169
include_submodules : bool = False ,
136
170
token : str | None = None ,
137
171
output : str | None = None ,
138
- mcp_server : bool = False ,
172
+ mcp : bool = False ,
173
+ transport : str = "stdio" ,
174
+ host : str = "127.0.0.1" ,
175
+ port : int = 8001 ,
139
176
) -> None :
140
177
"""Analyze a directory or repository and create a text dump of its contents.
141
178
@@ -165,8 +202,14 @@ async def _async_main(
165
202
output : str | None
166
203
The path where the output file will be written (default: ``digest.txt`` in current directory).
167
204
Use ``"-"`` to write to ``stdout``.
168
- mcp_server : bool
205
+ mcp : bool
169
206
If ``True``, starts the MCP (Model Context Protocol) server instead of normal operation (default: ``False``).
207
+ transport : str
208
+ Transport protocol for MCP communication: "stdio" or "tcp" (default: "stdio").
209
+ host : str
210
+ Host to bind TCP server (only used with transport="tcp", default: "127.0.0.1").
211
+ port : int
212
+ Port for TCP server (only used with transport="tcp", default: 8001).
170
213
171
214
Raises
172
215
------
@@ -177,17 +220,21 @@ async def _async_main(
177
220
178
221
"""
179
222
# Check if MCP server mode is requested
180
- if mcp_server :
181
- # Dynamic import to avoid circular imports and optional dependency
182
- try :
183
- from gitingest .mcp_server import ( # noqa: PLC0415 # pylint: disable=import-outside-toplevel
184
- start_mcp_server ,
185
- )
186
-
223
+ if mcp :
224
+ if not MCP_AVAILABLE :
225
+ msg = "MCP server dependencies not installed"
226
+ raise click .ClickException (msg )
227
+
228
+ if transport == "tcp" :
229
+ # Use TCP transport with FastMCP and metrics support
230
+ # Enable metrics for TCP mode if not already set
231
+ if os .getenv ("GITINGEST_METRICS_ENABLED" ) is None :
232
+ os .environ ["GITINGEST_METRICS_ENABLED" ] = "true"
233
+
234
+ await start_mcp_server_tcp (host , port )
235
+ else :
236
+ # Use stdio transport (default) - metrics not available in stdio mode
187
237
await start_mcp_server ()
188
- except ImportError as e :
189
- msg = f"MCP server dependencies not installed: { e } "
190
- raise click .ClickException (msg ) from e
191
238
return
192
239
193
240
try :
0 commit comments