暂存mcpserver

This commit is contained in:
2025-08-03 13:23:48 +08:00
parent c8389bbf80
commit f8dd936568
11 changed files with 205 additions and 219 deletions

86
.gitignore vendored
View File

@@ -1,66 +1,28 @@
# Visual Studio
# 忽略包含敏感信息的环境变量文件
.env
# 忽略 Python 缓存文件
__pycache__/
*.pyc
*.pyo
*.pyd
# 忽略 VSCode 设置
.vscode/
# MCP Server generated files
Source/MCP/*.log
# Visual Studio/C# 编译和临时文件
[Bb]in/
[Oo]bj/
.vs/
*.suo
*.user
*.ilk
*.obj
*.pdb
*.tlb
*.tlh
*.tmp
*.bak
*.log
*.vspscc
*.vssscc
[Bb]in/
[Oo]bj/
[Dd]ebug/
[Rr]elease/
*.csproj.user
*.resharper
*.dotSettings
*.pidb
*.booproj
*.kproj
*.lock.json
*.nuget.props
*.nuget.targets
*.nupkg
*.appx
*.appxbundle
*.cer
*.pfx
*.snk
*.vsix
*.vsmacros
*.webtest
*.testrunconfig
*.trx
*.fxcop
*.fxcopcmd
*.scc
*.vsscc
*.vspscc
*.vssscc
*.builds
*.deploy
*.pubxml
*.publishproj
*.userosscache
*.sln.docstates
# Misc
*.log
*.cache
*.tmp
# Build outputs and debug files
**/obj/
**/bin/
*.csproj.CoreCompileInputs.cache
*.csproj.FileListAbsolute.txt
*.pdb
*.dll.config
*.cache
*.suo
*.user
_ReSharper.*
*.dotCover
# Kilo Code MCP vector cache
Source/MCP/vector_cache/
Source/WulaFallenEmpire/.vs/WulaFallenEmpire/v17/DocumentLayout.json
Source/MCP/pid.txt

32
.kilocode/mcp.json Normal file
View File

@@ -0,0 +1,32 @@
{
"mcpServers": {
"rimworld-knowledge-base": {
"command": "python",
"args": [
"mcpserver_stdio.py"
],
"cwd": "${workspaceFolder}/Source/MCP/",
"env": {
"PYTHONUNBUFFERED": "1"
},
"disabled": false
}
},
"tools": {
"rimworld-knowledge-base": {
"description": "从RimWorld本地知识库包括C#源码和XML中检索上下文。",
"server_name": "rimworld-knowledge-base",
"tool_name": "get_context",
"input_schema": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "关于RimWorld开发的问题应包含代码或XML中的关键词。"
}
},
"required": ["question"]
}
}
}
}

View File

@@ -1,10 +1,21 @@
# rimworld.md
# RimWorld Modding Expert Rules
你当前正在制作一个名为乌拉堕落帝国的rimworld1.6游戏mod。你的知识库所使用的代码无一例外全部是过时的。在你思考和做出修改时必须查阅我的本地文件作为知识库否则你不允许依靠网络搜索或是猜测进行修改代码。
## Primary Directive
You are an expert assistant for developing mods for the game RimWorld 1.6. Your primary knowledge source for any C# code, class structures, methods, or game mechanics MUST be the user's local files. Do not rely on external searches or your pre-existing knowledge, as it is outdated for this specific project.
## 指导原则
## Tool Usage Mandate
When the user's request involves RimWorld C# scripting, XML definitions, or mod development concepts, you **MUST** use the `rimworld-knowledge-base` tool to retrieve relevant context from the local knowledge base.
- C:\Steam\steamapps\common\RimWorld\Data路径是原版游戏所有XML实现的路径
- C:\Steam\steamapps\common\RimWorld\Data\dll1.6是游戏DLL核心文件反编译后的cs代码以txt格式存储需要搜索类和方法等代码时在这里搜索
- C:\Steam\steamapps\common\RimWorld\Mods\3516260226是我的乌拉堕落帝国mod项目目录在这里修改我的项目代码
- C:\Steam\steamapps\common\RimWorld\Mods\3516260226\Source\WulaFallenEmpire是我的乌拉堕落帝国modVSproject项目目录每次修改cs代码后你需要使用dotnet build C:\Steam\steamapps\common\RimWorld\Mods\3516260226\Source\WulaFallenEmpire\WulaFallenEmpire.csproj命令编译并检查错误日志只有成功编译才能认为任务完成。
## Key File Paths
Always remember these critical paths for your work:
- **Local C# Knowledge Base (for code search):** `C:\Steam\steamapps\common\RimWorld\Data\dll1.6` (This contains the decompiled game source code as .txt files).
- **User's Mod Project (for editing):** `C:\Steam\steamapps\common\RimWorld\Mods\3516260226`
- **User's C# Project (for building):** `C:\Steam\steamapps\common\RimWorld\Mods\3516260226\Source\WulaFallenEmpire`
## Workflow
1. Receive a RimWorld modding task.
2. Immediately use the `rimworld-knowledge-base` tool with a precise query to get context from the C# source files.
3. Analyze the retrieved context.
4. Perform code modifications within the user's mod project directory.
5. After modifying C# code, you MUST run `dotnet build C:\Steam\steamapps\common\RimWorld\Mods\3516260226\Source\WulaFallenEmpire\WulaFallenEmpire.csproj` to check for errors. A successful build is required for task completion.

View File

@@ -0,0 +1,131 @@
# -*- coding: utf-8 -*-
import os
import sys
import logging
import json
# 1. --- 导入库 ---
# mcp 库已通过 'pip install -e' 安装,无需修改 sys.path
from mcp.server.fastmcp import FastMCP
# 2. --- 日志和知识库配置 ---
MCP_DIR = os.path.dirname(os.path.abspath(__file__))
LOG_FILE_PATH = os.path.join(MCP_DIR, 'mcpserver.log')
logging.basicConfig(filename=LOG_FILE_PATH, level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
encoding='utf-8')
# 定义知识库路径
KNOWLEDGE_BASE_PATHS = [
r"C:\Steam\steamapps\common\RimWorld\Data"
]
# 4. --- 核心功能函数 ---
def find_files_with_keyword(roots, keyword, extensions=['.xml', '.cs', '.txt']):
"""在指定目录中查找包含关键字的文件名和内容。"""
found_files = []
keyword_lower = keyword.lower()
for root_path in roots:
if not os.path.isdir(root_path):
logging.warning(f"知识库路径不存在或不是一个目录: {root_path}")
continue
for dirpath, _, filenames in os.walk(root_path):
for filename in filenames:
if any(filename.lower().endswith(ext) for ext in extensions):
file_path = os.path.join(dirpath, filename)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 使用不区分大小写的子字符串搜索
if keyword_lower in content.lower():
found_files.append(file_path)
except Exception as e:
logging.error(f"读取文件时出错 {file_path}: {e}")
return found_files
def find_keyword_in_question(question: str) -> str:
"""从问题中提取最有可能的单个关键词 (通常是类型名或defName)。"""
# 排除常见但非特定的术语
excluded_keywords = {"XML", "C#", "DEF", "CS"}
# 使用更精确的规则来识别关键词
# 规则1: 包含下划线 (很可能是 defName)
# 规则2: 混合大小写 (很可能是 C# 类型名)
# 规则3: 全大写但不在排除列表中
parts = question.replace('"', ' ').replace("'", ' ').replace('`', ' ').split()
potential_keywords = []
for part in parts:
part = part.strip(',.?;:')
if not part:
continue
# 检查是否在排除列表中
if part.upper() in excluded_keywords:
continue
# 规则1: 包含下划线
if '_' in part:
potential_keywords.append((part, 3)) # 最高优先级
# 规则2: 驼峰命名或混合大小写
elif any(c.islower() for c in part) and any(c.isupper() for c in part):
potential_keywords.append((part, 2)) # 次高优先级
# 规则3: 多个大写字母(例如 CompPsychicScaling但要排除纯大写缩写词
elif sum(1 for c in part if c.isupper()) > 1 and not part.isupper():
potential_keywords.append((part, 2))
# 备用规则:如果之前的规则都没匹配上,就找一个看起来像专有名词的
elif part[0].isupper() and len(part) > 4: # 长度大于4以避免像 'A' 'I' 这样的词
potential_keywords.append((part, 1)) # 较低优先级
# 如果找到了关键词,按优先级排序并返回最高优先级的那个
if potential_keywords:
potential_keywords.sort(key=lambda x: x[1], reverse=True)
logging.info(f"找到的潜在关键词: {potential_keywords}")
return potential_keywords[0][0]
# 如果没有找到,返回空字符串
logging.warning(f"'{question}' 中未找到合适的关键词。")
return ""
# 5. --- 创建和配置 MCP 服务器 ---
# 使用 FastMCP 创建服务器实例
mcp = FastMCP(
"rimworld-knowledge-base",
"1.0.0-fastmcp",
)
@mcp.tool()
def get_context(question: str) -> str:
"""
根据问题中的关键词在RimWorld知识库中搜索相关的XML或C#文件。
返回找到的文件路径列表。
"""
logging.info(f"收到问题: {question}")
keyword = find_keyword_in_question(question)
if not keyword:
logging.warning("无法从问题中提取关键词。")
return "无法从问题中提取关键词,请提供更具体的信息。"
logging.info(f"提取到关键词: {keyword}")
try:
found_files = find_files_with_keyword(KNOWLEDGE_BASE_PATHS, keyword)
if not found_files:
logging.info(f"未找到与 '{keyword}' 相关的文件。")
return f"未在知识库中找到与 '{keyword}' 相关的文件定义。"
logging.info(f"找到了 {len(found_files)} 个相关文件。")
# 将文件列表格式化为字符串返回
context = f"根据关键词 '{keyword}',在知识库中找到了以下 {len(found_files)} 个相关文件:\n\n" + "\n".join(found_files)
return context
except Exception as e:
logging.error(f"处理请求时发生意外错误: {e}", exc_info=True)
return f"处理您的请求时发生错误: {e}"
# 6. --- 启动服务器 ---
# FastMCP 实例可以直接运行
if __name__ == "__main__":
logging.info("RimWorld 本地知识库 (FastMCP版, v1.2 关键词修正) 正在启动...")
# 使用 'stdio' 传输协议
mcp.run(transport="stdio")

View File

@@ -1,92 +0,0 @@
{
"Version": 1,
"WorkspaceRootPath": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\WulaFallenEmpireTest\\Source\\WulaFallenEmpire\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|e:\\steamlibrary\\steamapps\\common\\rimworld\\mods\\wulafallenempiretest\\source\\wulafallenempire\\eventsystem\\dialog_customdisplay.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|solutionrelative:eventsystem\\dialog_customdisplay.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|e:\\steamlibrary\\steamapps\\common\\rimworld\\mods\\wulafallenempiretest\\source\\wulafallenempire\\eventsystem\\debugactions.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|solutionrelative:eventsystem\\debugactions.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|e:\\steamlibrary\\steamapps\\common\\rimworld\\mods\\wulafallenempiretest\\source\\wulafallenempire\\eventsystem\\customuidef.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|solutionrelative:eventsystem\\customuidef.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|e:\\steamlibrary\\steamapps\\common\\rimworld\\mods\\wulafallenempiretest\\source\\wulafallenempire\\eventsystem\\condition.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|solutionrelative:eventsystem\\condition.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
}
],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 200,
"SelectedChildIndex": 0,
"Children": [
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "Dialog_CustomDisplay.cs",
"DocumentMoniker": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\WulaFallenEmpireTest\\Source\\WulaFallenEmpire\\EventSystem\\Dialog_CustomDisplay.cs",
"RelativeDocumentMoniker": "EventSystem\\Dialog_CustomDisplay.cs",
"ToolTip": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\WulaFallenEmpireTest\\Source\\WulaFallenEmpire\\EventSystem\\Dialog_CustomDisplay.cs",
"RelativeToolTip": "EventSystem\\Dialog_CustomDisplay.cs",
"ViewState": "AgIAAAAAAAAAAAAAAAAAAAUAAAAaAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-07-27T10:59:13.84Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 1,
"Title": "DebugActions.cs",
"DocumentMoniker": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\WulaFallenEmpireTest\\Source\\WulaFallenEmpire\\EventSystem\\DebugActions.cs",
"RelativeDocumentMoniker": "EventSystem\\DebugActions.cs",
"ToolTip": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\WulaFallenEmpireTest\\Source\\WulaFallenEmpire\\EventSystem\\DebugActions.cs",
"RelativeToolTip": "EventSystem\\DebugActions.cs",
"ViewState": "AgIAAAAAAAAAAAAAAADwvwAAAAAAAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-07-27T10:59:11.817Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 2,
"Title": "CustomUIDef.cs",
"DocumentMoniker": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\WulaFallenEmpireTest\\Source\\WulaFallenEmpire\\EventSystem\\CustomUIDef.cs",
"RelativeDocumentMoniker": "EventSystem\\CustomUIDef.cs",
"ToolTip": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\WulaFallenEmpireTest\\Source\\WulaFallenEmpire\\EventSystem\\CustomUIDef.cs",
"RelativeToolTip": "EventSystem\\CustomUIDef.cs",
"ViewState": "AgIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-07-27T10:59:01.929Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 3,
"Title": "Condition.cs",
"DocumentMoniker": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\WulaFallenEmpireTest\\Source\\WulaFallenEmpire\\EventSystem\\Condition.cs",
"RelativeDocumentMoniker": "EventSystem\\Condition.cs",
"ToolTip": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\WulaFallenEmpireTest\\Source\\WulaFallenEmpire\\EventSystem\\Condition.cs",
"RelativeToolTip": "EventSystem\\Condition.cs",
"ViewState": "AgIAAAAAAAAAAAAAAAAAAAkAAAAAAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-07-27T10:50:26.799Z",
"EditorCaption": ""
},
{
"$type": "Bookmark",
"Name": "ST:0:0:{1c4feeaa-4718-4aa9-859d-94ce25d182ba}"
}
]
}
]
}
]
}

View File

@@ -1,58 +0,0 @@
{
"Version": 1,
"WorkspaceRootPath": "C:\\Steam\\steamapps\\common\\RimWorld\\Mods\\3516260226\\Source\\WulaFallenEmpire\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|c:\\steam\\steamapps\\common\\rimworld\\mods\\3516260226\\source\\wulafallenempire\\verb\\projectile_configurablehellspherecannon.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|solutionrelative:verb\\projectile_configurablehellspherecannon.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|c:\\steam\\steamapps\\common\\rimworld\\mods\\3516260226\\source\\wulafallenempire\\building_wula_darkenergy_engine.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|solutionrelative:building_wula_darkenergy_engine.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
}
],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 200,
"SelectedChildIndex": 2,
"Children": [
{
"$type": "Bookmark",
"Name": "ST:0:0:{1c4feeaa-4718-4aa9-859d-94ce25d182ba}"
},
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "Projectile_ConfigurableHellsphereCannon.cs",
"DocumentMoniker": "C:\\Steam\\steamapps\\common\\RimWorld\\Mods\\3516260226\\Source\\WulaFallenEmpire\\Verb\\Projectile_ConfigurableHellsphereCannon.cs",
"RelativeDocumentMoniker": "Verb\\Projectile_ConfigurableHellsphereCannon.cs",
"ToolTip": "C:\\Steam\\steamapps\\common\\RimWorld\\Mods\\3516260226\\Source\\WulaFallenEmpire\\Verb\\Projectile_ConfigurableHellsphereCannon.cs",
"RelativeToolTip": "Verb\\Projectile_ConfigurableHellsphereCannon.cs",
"ViewState": "AQIAAC0AAAAAAAAAAAAUwFwAAAABAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-08-01T05:40:06.726Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 1,
"Title": "Building_Wula_DarkEnergy_Engine.cs",
"DocumentMoniker": "C:\\Steam\\steamapps\\common\\RimWorld\\Mods\\3516260226\\Source\\WulaFallenEmpire\\Building_Wula_DarkEnergy_Engine.cs",
"RelativeDocumentMoniker": "Building_Wula_DarkEnergy_Engine.cs",
"ToolTip": "C:\\Steam\\steamapps\\common\\RimWorld\\Mods\\3516260226\\Source\\WulaFallenEmpire\\Building_Wula_DarkEnergy_Engine.cs",
"RelativeToolTip": "Building_Wula_DarkEnergy_Engine.cs",
"ViewState": "AQIAAAAAAAAAAAAAAADwvwUAAAAWAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-08-01T05:27:49.469Z",
"EditorCaption": ""
}
]
}
]
}
]
}