-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_analysis_mcp.py
More file actions
85 lines (67 loc) · 2.89 KB
/
Copy pathcode_analysis_mcp.py
File metadata and controls
85 lines (67 loc) · 2.89 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
# from pylsp import LspClient
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import TextContent, Tool, GetPromptResult, PromptMessage, Prompt
from typing import Any
from collections.abc import Sequence
import json
from tools import tool_list
server = Server('code-analysis-mcp',
instructions='''This MCP server provides a set of tools for code analysis, enabling fast and precise symbol lookup. When using these tools, you should first call start_analyzer to initialize the analyzer, passing the root directory of the code as a parameter. Once the analyzer is started, other interfaces can be invoked.
The find_definition tool provides functionality to locate the position where a variable or function is defined. It is typically used during code analysis when you need to inspect the implementation of a called function. This tool requires a variable name or function name as a parameter.
The find_references tool enables locating the positions where a variable or function is referenced or used. It is commonly used to determine where a specific function is called during code analysis. Like find_definition, this tool also requires a variable name or function name as a parameter.''',
)
tool_table = {}
@server.list_tools()
async def list_tools() -> list[Tool]:
tool_register_list: list[Tool] = []
for tool in tool_list:
tool_register_list.append(Tool(
name=tool.__name__,
description=tool.__doc__,
inputSchema=tool.model_json_schema()
))
return tool_register_list
@server.call_tool()
async def call_tool(name: str, arg: Any) -> Sequence[TextContent]:
if name not in tool_table:
raise ValueError(f'unknown tool: {name}')
result = await tool_table[name].exec(arg)
if None == result:
result = True
return [
TextContent(
text=json.dumps(result),
type='text'
)
]
# @server.get_prompt()
# async def get_prompt(name: str, arg: dict[str, str] | None) -> GetPromptResult:
# return GetPromptResult(description='desc', messages=[
# PromptMessage(
# role="user",
# content=TextContent(
# text=f'input name: {name}, input arg: {arg}',
# type='text'
# )
# )
# ])
# @server.list_prompts()
# async def list_prompts() -> list[Prompt]:
# return [
# Prompt(
# name='analysis',
# description='first desc',
# )
# ]
async def main():
global tool_table
for tool in tool_list:
tool_table.update({tool.__name__: tool})
async with stdio_server() as (read_stream, write_stream):
options = server.create_initialization_options()
# options.capabilities.performance = 'very fast'
await server.run(read_stream, write_stream, options)
if __name__ == '__main__':
import asyncio
asyncio.run(main())