refactor(mcp): improve docs and script (#146)

This commit is contained in:
sigoden
2024-12-12 13:18:54 +08:00
committed by GitHub
parent e8ffb414c4
commit 40c13b9fa9
3 changed files with 27 additions and 12 deletions
+5
View File
@@ -184,6 +184,11 @@ documents:
Refer to [./agents/demo](https://github.com/sigoden/llm-functions/tree/main/agents/demo) for examples of how to implement a agent. Refer to [./agents/demo](https://github.com/sigoden/llm-functions/tree/main/agents/demo) for examples of how to implement a agent.
## MCP (Model Context Protocol)
- [mcp/server](./mcp/server/): Let LLM-Functions tools/agents be used through the Model Context Protocol.
- [mcp/bridge](./mcp/bridge/): Let external MCP tools be used by LLM-Functions.
## License ## License
The project is under the MIT License, Refer to the [LICENSE](https://github.com/sigoden/llm-functions/blob/main/LICENSE) file for detailed information. The project is under the MIT License, Refer to the [LICENSE](https://github.com/sigoden/llm-functions/blob/main/LICENSE) file for detailed information.
+3 -2
View File
@@ -1,6 +1,6 @@
# MCP-Bridge # MCP-Bridge
Let MCP tools be used by LLM functions. Let external MCP tools be used by LLM-Functions.
## Get Started ## Get Started
@@ -39,4 +39,5 @@ Let MCP tools be used by LLM functions.
argc mcp start argc mcp start
``` ```
> Run `argc mcp stop` to stop the bridge server, recover functions.json > Run `argc mcp stop` to stop the bridge server, recover functions.json
> Run `argc mcp logs` to check the server's logs.
Regular → Executable
+19 -10
View File
@@ -25,8 +25,7 @@ start() {
nohup node "$index_js" "$llm_functions_dir" > "$MCP_DIR/mcp-bridge.log" 2>&1 & nohup node "$index_js" "$llm_functions_dir" > "$MCP_DIR/mcp-bridge.log" 2>&1 &
wait-for-server wait-for-server
echo "Merge MCP tools into functions.json" echo "Merge MCP tools into functions.json"
merge-functions > "$MCP_DIR/functions.json" "$0" merge-functions -S
cp -f "$MCP_DIR/functions.json" "$FUNCTIONS_JSON_PATH"
build-bin build-bin
} }
@@ -40,15 +39,13 @@ stop() {
kill -9 "$pid" > /dev/null 2>&1 || true kill -9 "$pid" > /dev/null 2>&1 || true
fi fi
fi fi
mkdir -p "$MCP_DIR" "$0" recovery-functions -S
unmerge-functions > "$MCP_DIR/functions.original.json"
cp -f "$MCP_DIR/functions.original.json" "$FUNCTIONS_JSON_PATH"
} }
# @cmd Call mcp tool # @cmd Run the tool
# @arg tool![`_choice_tool`] The tool name # @arg tool![`_choice_tool`] The tool name
# @arg json The json data # @arg json The json data
call() { run@tool() {
if [[ -z "$argc_json" ]]; then if [[ -z "$argc_json" ]]; then
declaration="$(build-declarations | jq --arg tool "$argc_tool" -r '.[] | select(.name == $tool)')" declaration="$(build-declarations | jq --arg tool "$argc_tool" -r '.[] | select(.name == $tool)')"
if [[ -n "$declaration" ]]; then if [[ -n "$declaration" ]]; then
@@ -89,17 +86,29 @@ build-bin() {
} }
# @cmd Merge mcp tools into functions.json # @cmd Merge mcp tools into functions.json
# @flag -S --save Save to functions.json
merge-functions() { merge-functions() {
jq --argjson json1 "$(unmerge-functions)" --argjson json2 "$(build-declarations)" -n '($json1 + $json2)' result="$(jq --argjson json1 "$("$0" recovery-functions)" --argjson json2 "$(build-declarations)" -n '($json1 + $json2)')"
if [[ -n "$argc_save" ]]; then
printf "%s" "$result" > "$FUNCTIONS_JSON_PATH"
else
printf "%s" "$result"
fi
} }
# @cmd Unmerge mcp tools from functions.json # @cmd Unmerge mcp tools from functions.json
unmerge-functions() { # @flag -S --save Save to functions.json
recovery-functions() {
functions="[]" functions="[]"
if [[ -f "$FUNCTIONS_JSON_PATH" ]]; then if [[ -f "$FUNCTIONS_JSON_PATH" ]]; then
functions="$(cat "$FUNCTIONS_JSON_PATH")" functions="$(cat "$FUNCTIONS_JSON_PATH")"
fi fi
printf "%s" "$functions" | jq 'map(select(has("mcp") | not))' result="$(printf "%s" "$functions" | jq 'map(select(has("mcp") | not))')"
if [[ -n "$argc_save" ]]; then
printf "%s" "$result" > "$FUNCTIONS_JSON_PATH"
else
printf "%s" "$result"
fi
} }
# @cmd Build tools to bin # @cmd Build tools to bin