#
tokens: 47461/50000 3/511 files (page 17/20)
lines: on (toggle) GitHub
raw markdown copy reset
This is page 17 of 20. Use http://codebase.md/fujitsu-ai/mcp-server-for-mas-developments?lines=true&page={x} to view the full context.

# Directory Structure

```
├── .gitattributes
├── .gitignore
├── agents
│   ├── __init__.py
│   ├── AgentInterface
│   │   ├── __init__.py
│   │   ├── Python
│   │   │   ├── __init__.py
│   │   │   ├── agent.py
│   │   │   ├── color.py
│   │   │   ├── config.py
│   │   │   ├── language.py
│   │   │   ├── local_file_handler.py
│   │   │   └── network.py
│   │   └── requirements.txt
│   ├── AgentMonitoring
│   │   ├── ChatBot-Agent Dashboard Example - Grafana.json
│   │   ├── images
│   │   │   ├── Grafana.png
│   │   │   └── Prometheus.png
│   │   ├── IoT-Agent Dashboard Example - Grafana.json
│   │   ├── OpenAI compatible API - Agent Dashboard Example - Grafana.json
│   │   ├── prometheus Example.yml
│   │   └── README.md
│   ├── ChatBotAgent
│   │   ├── __init__.py
│   │   ├── config.json.example
│   │   ├── html
│   │   │   ├── favicon.ico
│   │   │   ├── index_de.html
│   │   │   ├── index.html
│   │   │   ├── Logo_light.svg
│   │   │   ├── start_http_server.ps1
│   │   │   └── start_http_server.sh
│   │   ├── Python
│   │   │   ├── __init__.py
│   │   │   └── chatbot_agent.py
│   │   ├── README.md
│   │   └── requirements.txt
│   ├── IoTAgent
│   │   ├── config_example.json
│   │   ├── Python
│   │   │   ├── iot_mqtt_agent.py
│   │   │   └── language.py
│   │   ├── README.md
│   │   └── requirements.txt
│   ├── ISMAgent
│   │   ├── config_example.json
│   │   ├── PGPT Scenario Prompts
│   │   │   ├── ISM System Prompt - Detecting Error State.txt
│   │   │   ├── ISM User Post-Prompt - Detecting Error State.txt
│   │   │   ├── ISM User Pre-Prompt - Detecting Error State.txt
│   │   │   └── README.md
│   │   ├── Python
│   │   │   ├── ism_agent.py
│   │   │   └── language.py
│   │   ├── README.md
│   │   ├── requirements.txt
│   │   └── start_ism_agent.ps1
│   ├── MCP-Client
│   │   ├── __init__.py
│   │   ├── .env.example
│   │   ├── Python
│   │   │   ├── __init__.py
│   │   │   ├── chat_handler.py
│   │   │   ├── config.py
│   │   │   ├── environment.py
│   │   │   ├── llm_client.py
│   │   │   ├── mcp_client_sse.py
│   │   │   ├── mcp_client.py
│   │   │   ├── messages
│   │   │   │   ├── __init__.py
│   │   │   │   ├── message_types
│   │   │   │   │   ├── __init__.py
│   │   │   │   │   ├── incrementing_id_message.py
│   │   │   │   │   ├── initialize_message.py
│   │   │   │   │   ├── json_rpc_message.py
│   │   │   │   │   ├── ping_message.py
│   │   │   │   │   ├── prompts_messages.py
│   │   │   │   │   ├── prompts_models.py
│   │   │   │   │   ├── resources_messages.py
│   │   │   │   │   └── tools_messages.py
│   │   │   │   ├── send_call_tool.py
│   │   │   │   ├── send_initialize_message.py
│   │   │   │   ├── send_message.py
│   │   │   │   ├── send_ping.py
│   │   │   │   ├── send_prompts.py
│   │   │   │   ├── send_resources.py
│   │   │   │   └── send_tools_list.py
│   │   │   ├── system_prompt_generator.py
│   │   │   ├── tools_handler.py
│   │   │   └── transport
│   │   │       ├── __init__.py
│   │   │       └── stdio
│   │   │           ├── __init__.py
│   │   │           ├── stdio_client.py
│   │   │           ├── stdio_server_parameters.py
│   │   │           └── stdio_server_shutdown.py
│   │   ├── README.md
│   │   ├── requirements.txt
│   │   └── server_config.json
│   ├── OpenAI_Compatible_API_Agent
│   │   ├── __init__.py
│   │   ├── docker-compose.yml
│   │   ├── Dockerfile
│   │   ├── pgpt_openai_api_mcp.json.example
│   │   ├── pgpt_openai_api_proxy.json.example
│   │   ├── Python
│   │   │   ├── __init__.py
│   │   │   ├── client_tests
│   │   │   │   ├── __init__.py
│   │   │   │   ├── openai_test_client_structured.py
│   │   │   │   ├── openai_test_client_tools.py
│   │   │   │   ├── openai_test_client.py
│   │   │   │   ├── vllm_client_multimodal.py
│   │   │   │   ├── vllm_client.py
│   │   │   │   ├── vllm_structured.py
│   │   │   │   └── vllm_structured2.py
│   │   │   ├── generate_api_key.py
│   │   │   ├── open_ai_helper.py
│   │   │   ├── openai_compatible_api.py
│   │   │   ├── openai_mcp_api.py
│   │   │   ├── pgpt_api.py
│   │   │   ├── privategpt_api.py
│   │   │   └── vllmproxy.py
│   │   ├── README.md
│   │   └── requirements.txt
│   └── SourceManagerAgent
│       ├── __init__.py
│       ├── config.json.example
│       └── Python
│           ├── __init__.py
│           ├── file_tools
│           │   └── loader_factory.py
│           ├── file_upload_agent.py
│           └── local_db.py
├── clients
│   ├── __init__.py
│   ├── C# .Net
│   │   ├── 1.0 mcp_login
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_login.deps.json
│   │   │   │           ├── mcp_login.dll
│   │   │   │           ├── mcp_login.exe
│   │   │   │           ├── mcp_login.pdb
│   │   │   │           ├── mcp_login.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_login.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_login.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_login.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_login.assets.cache
│   │   │   │   │       ├── mcp_login.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_login.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_login.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_login.csproj.Up2Date
│   │   │   │   │       ├── mcp_login.dll
│   │   │   │   │       ├── mcp_login.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_login.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_login.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_login.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_login.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_login.dll
│   │   │   │   ├── mcp_login.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_login.csproj.nuget.g.props
│   │   │   │   ├── mcp_login.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 1.1 mcp_logout
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_logout.deps.json
│   │   │   │           ├── mcp_logout.dll
│   │   │   │           ├── mcp_logout.exe
│   │   │   │           ├── mcp_logout.pdb
│   │   │   │           ├── mcp_logout.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_logout.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_logout.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_logout.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_logout.assets.cache
│   │   │   │   │       ├── mcp_logout.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_logout.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_logout.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_logout.csproj.Up2Date
│   │   │   │   │       ├── mcp_logout.dll
│   │   │   │   │       ├── mcp_logout.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_logout.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_logout.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_logout.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_logout.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_logout.dll
│   │   │   │   ├── mcp_logout.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_logout.csproj.nuget.g.props
│   │   │   │   ├── mcp_logout.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 2.0 mcp_chat
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_chat.deps.json
│   │   │   │           ├── mcp_chat.dll
│   │   │   │           ├── mcp_chat.exe
│   │   │   │           ├── mcp_chat.pdb
│   │   │   │           ├── mcp_chat.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_chat.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_chat.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_chat.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_chat.assets.cache
│   │   │   │   │       ├── mcp_chat.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_chat.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_chat.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_chat.csproj.Up2Date
│   │   │   │   │       ├── mcp_chat.dll
│   │   │   │   │       ├── mcp_chat.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_chat.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_chat.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_chat.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_chat.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_chat.dll
│   │   │   │   ├── mcp_chat.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_chat.csproj.nuget.g.props
│   │   │   │   ├── mcp_chat.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 2.1 mcp_continue_chat
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_continue_chat.deps.json
│   │   │   │           ├── mcp_continue_chat.dll
│   │   │   │           ├── mcp_continue_chat.exe
│   │   │   │           ├── mcp_continue_chat.pdb
│   │   │   │           ├── mcp_continue_chat.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_continue_chat.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_cont.EF178231.Up2Date
│   │   │   │   │       ├── mcp_continue_chat.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_continue_chat.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_continue_chat.assets.cache
│   │   │   │   │       ├── mcp_continue_chat.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_continue_chat.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_continue_chat.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_continue_chat.dll
│   │   │   │   │       ├── mcp_continue_chat.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_continue_chat.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_continue_chat.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_continue_chat.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_continue_chat.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_continue_chat.dll
│   │   │   │   ├── mcp_continue_chat.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_continue_chat.csproj.nuget.g.props
│   │   │   │   ├── mcp_continue_chat.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 2.2 mcp_get_chat_info
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_get_chat_info.deps.json
│   │   │   │           ├── mcp_get_chat_info.dll
│   │   │   │           ├── mcp_get_chat_info.exe
│   │   │   │           ├── mcp_get_chat_info.pdb
│   │   │   │           ├── mcp_get_chat_info.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── Dokumente - Verknüpfung.lnk
│   │   │   ├── mcp_get_chat_info.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_get_.DFF47B4E.Up2Date
│   │   │   │   │       ├── mcp_get_chat_info.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_get_chat_info.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_get_chat_info.assets.cache
│   │   │   │   │       ├── mcp_get_chat_info.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_get_chat_info.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_get_chat_info.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_get_chat_info.dll
│   │   │   │   │       ├── mcp_get_chat_info.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_get_chat_info.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_get_chat_info.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_get_chat_info.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_get_chat_info.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_get_chat_info.dll
│   │   │   │   ├── mcp_get_chat_info.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_get_chat_info.csproj.nuget.g.props
│   │   │   │   ├── mcp_get_chat_info.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 3.0 mcp_create_source
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_create_source.deps.json
│   │   │   │           ├── mcp_create_source.dll
│   │   │   │           ├── mcp_create_source.exe
│   │   │   │           ├── mcp_create_source.pdb
│   │   │   │           ├── mcp_create_source.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_create_source.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_crea.CB4ED912.Up2Date
│   │   │   │   │       ├── mcp_create_source.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_create_source.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_create_source.assets.cache
│   │   │   │   │       ├── mcp_create_source.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_create_source.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_create_source.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_create_source.dll
│   │   │   │   │       ├── mcp_create_source.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_create_source.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_create_source.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_create_source.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_create_source.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_create_source.dll
│   │   │   │   ├── mcp_create_source.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_create_source.csproj.nuget.g.props
│   │   │   │   ├── mcp_create_source.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 3.1 mcp_get_source
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_get_source.deps.json
│   │   │   │           ├── mcp_get_source.dll
│   │   │   │           ├── mcp_get_source.exe
│   │   │   │           ├── mcp_get_source.pdb
│   │   │   │           ├── mcp_get_source.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_get_source.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_get_.4E61956F.Up2Date
│   │   │   │   │       ├── mcp_get_source.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_get_source.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_get_source.assets.cache
│   │   │   │   │       ├── mcp_get_source.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_get_source.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_get_source.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_get_source.dll
│   │   │   │   │       ├── mcp_get_source.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_get_source.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_get_source.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_get_source.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_get_source.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_get_source.dll
│   │   │   │   ├── mcp_get_source.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_get_source.csproj.nuget.g.props
│   │   │   │   ├── mcp_get_source.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 3.2 mcp_list_sources
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_list_sources.deps.json
│   │   │   │           ├── mcp_list_sources.dll
│   │   │   │           ├── mcp_list_sources.exe
│   │   │   │           ├── mcp_list_sources.pdb
│   │   │   │           ├── mcp_list_sources.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_list_sources.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_list_sources.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_list_sources.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_list_sources.assets.cache
│   │   │   │   │       ├── mcp_list_sources.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_list_sources.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_list_sources.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_list_sources.dll
│   │   │   │   │       ├── mcp_list_sources.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_list_sources.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_list_sources.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_list_sources.pdb
│   │   │   │   │       ├── mcp_list.A720E197.Up2Date
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_list_sources.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_list_sources.dll
│   │   │   │   ├── mcp_list_sources.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_list_sources.csproj.nuget.g.props
│   │   │   │   ├── mcp_list_sources.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 3.3 mcp_edit_source
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_edit_source.deps.json
│   │   │   │           ├── mcp_edit_source.dll
│   │   │   │           ├── mcp_edit_source.exe
│   │   │   │           ├── mcp_edit_source.pdb
│   │   │   │           ├── mcp_edit_source.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_edit_source.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_edit_source.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_edit_source.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_edit_source.assets.cache
│   │   │   │   │       ├── mcp_edit_source.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_edit_source.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_edit_source.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_edit_source.dll
│   │   │   │   │       ├── mcp_edit_source.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_edit_source.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_edit_source.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_edit_source.pdb
│   │   │   │   │       ├── mcp_edit.7303BE3B.Up2Date
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_edit_source.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_edit_source.dll
│   │   │   │   ├── mcp_edit_source.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_edit_source.csproj.nuget.g.props
│   │   │   │   ├── mcp_edit_source.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 3.4 mcp_delete_source
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_delete_source.deps.json
│   │   │   │           ├── mcp_delete_source.dll
│   │   │   │           ├── mcp_delete_source.exe
│   │   │   │           ├── mcp_delete_source.pdb
│   │   │   │           ├── mcp_delete_source.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_delete_source.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_dele.67DD13F9.Up2Date
│   │   │   │   │       ├── mcp_delete_source.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_delete_source.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_delete_source.assets.cache
│   │   │   │   │       ├── mcp_delete_source.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_delete_source.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_delete_source.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_delete_source.dll
│   │   │   │   │       ├── mcp_delete_source.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_delete_source.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_delete_source.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_delete_source.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_delete_source.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_delete_source.dll
│   │   │   │   ├── mcp_delete_source.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_delete_source.csproj.nuget.g.props
│   │   │   │   ├── mcp_delete_source.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 4.0 mcp_list_groups
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_list_groups.deps.json
│   │   │   │           ├── mcp_list_groups.dll
│   │   │   │           ├── mcp_list_groups.exe
│   │   │   │           ├── mcp_list_groups.pdb
│   │   │   │           ├── mcp_list_groups.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_list_groups.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_list_groups.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_list_groups.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_list_groups.assets.cache
│   │   │   │   │       ├── mcp_list_groups.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_list_groups.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_list_groups.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_list_groups.dll
│   │   │   │   │       ├── mcp_list_groups.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_list_groups.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_list_groups.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_list_groups.pdb
│   │   │   │   │       ├── mcp_list.EBD5E0D2.Up2Date
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_list_groups.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_list_groups.dll
│   │   │   │   ├── mcp_list_groups.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_list_groups.csproj.nuget.g.props
│   │   │   │   ├── mcp_list_groups.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 4.1 mcp_store_group
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_store_group.deps.json
│   │   │   │           ├── mcp_store_group.dll
│   │   │   │           ├── mcp_store_group.exe
│   │   │   │           ├── mcp_store_group.pdb
│   │   │   │           ├── mcp_store_group.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_store_group.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_stor.AFB4AA35.Up2Date
│   │   │   │   │       ├── mcp_store_group.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_store_group.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_store_group.assets.cache
│   │   │   │   │       ├── mcp_store_group.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_store_group.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_store_group.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_store_group.dll
│   │   │   │   │       ├── mcp_store_group.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_store_group.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_store_group.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_store_group.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_store_group.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_store_group.dll
│   │   │   │   ├── mcp_store_group.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_store_group.csproj.nuget.g.props
│   │   │   │   ├── mcp_store_group.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 4.2 mcp_delete_group
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_delete_group.deps.json
│   │   │   │           ├── mcp_delete_group.dll
│   │   │   │           ├── mcp_delete_group.exe
│   │   │   │           ├── mcp_delete_group.pdb
│   │   │   │           ├── mcp_delete_group.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_delete_group.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_dele.FE1C6298.Up2Date
│   │   │   │   │       ├── mcp_delete_group.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_delete_group.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_delete_group.assets.cache
│   │   │   │   │       ├── mcp_delete_group.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_delete_group.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_delete_group.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_delete_group.dll
│   │   │   │   │       ├── mcp_delete_group.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_delete_group.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_delete_group.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_delete_group.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_delete_group.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_delete_group.dll
│   │   │   │   ├── mcp_delete_group.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_delete_group.csproj.nuget.g.props
│   │   │   │   ├── mcp_delete_group.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 5.0 mcp_store_user
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_store_user.deps.json
│   │   │   │           ├── mcp_store_user.dll
│   │   │   │           ├── mcp_store_user.exe
│   │   │   │           ├── mcp_store_user.pdb
│   │   │   │           ├── mcp_store_user.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_store_user.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_stor.6C0F0C8A.Up2Date
│   │   │   │   │       ├── mcp_store_user.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_store_user.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_store_user.assets.cache
│   │   │   │   │       ├── mcp_store_user.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_store_user.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_store_user.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_store_user.dll
│   │   │   │   │       ├── mcp_store_user.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_store_user.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_store_user.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_store_user.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_store_user.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_store_user.dll
│   │   │   │   ├── mcp_store_user.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_store_user.csproj.nuget.g.props
│   │   │   │   ├── mcp_store_user.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 5.1 mcp_edit_user
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_edit_user.deps.json
│   │   │   │           ├── mcp_edit_user.dll
│   │   │   │           ├── mcp_edit_user.exe
│   │   │   │           ├── mcp_edit_user.pdb
│   │   │   │           ├── mcp_edit_user.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_edit_user.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_edit_user.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_edit_user.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_edit_user.assets.cache
│   │   │   │   │       ├── mcp_edit_user.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_edit_user.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_edit_user.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_edit_user.dll
│   │   │   │   │       ├── mcp_edit_user.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_edit_user.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_edit_user.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_edit_user.pdb
│   │   │   │   │       ├── mcp_edit.94A30270.Up2Date
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_edit_user.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_edit_user.dll
│   │   │   │   ├── mcp_edit_user.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_edit_user.csproj.nuget.g.props
│   │   │   │   ├── mcp_edit_user.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── 5.2 mcp_delete_user
│   │   │   ├── bin
│   │   │   │   └── Debug
│   │   │   │       └── net9.0
│   │   │   │           ├── mcp_delete_user.deps.json
│   │   │   │           ├── mcp_delete_user.dll
│   │   │   │           ├── mcp_delete_user.exe
│   │   │   │           ├── mcp_delete_user.pdb
│   │   │   │           ├── mcp_delete_user.runtimeconfig.json
│   │   │   │           └── Newtonsoft.Json.dll
│   │   │   ├── mcp_delete_user.csproj
│   │   │   ├── obj
│   │   │   │   ├── Debug
│   │   │   │   │   └── net9.0
│   │   │   │   │       ├── .NETCoreApp,Version=v9.0.AssemblyAttributes.cs
│   │   │   │   │       ├── apphost.exe
│   │   │   │   │       ├── mcp_dele.CEB7E33D.Up2Date
│   │   │   │   │       ├── mcp_delete_user.AssemblyInfo.cs
│   │   │   │   │       ├── mcp_delete_user.AssemblyInfoInputs.cache
│   │   │   │   │       ├── mcp_delete_user.assets.cache
│   │   │   │   │       ├── mcp_delete_user.csproj.AssemblyReference.cache
│   │   │   │   │       ├── mcp_delete_user.csproj.CoreCompileInputs.cache
│   │   │   │   │       ├── mcp_delete_user.csproj.FileListAbsolute.txt
│   │   │   │   │       ├── mcp_delete_user.dll
│   │   │   │   │       ├── mcp_delete_user.GeneratedMSBuildEditorConfig.editorconfig
│   │   │   │   │       ├── mcp_delete_user.genruntimeconfig.cache
│   │   │   │   │       ├── mcp_delete_user.GlobalUsings.g.cs
│   │   │   │   │       ├── mcp_delete_user.pdb
│   │   │   │   │       ├── ref
│   │   │   │   │       │   └── mcp_delete_user.dll
│   │   │   │   │       └── refint
│   │   │   │   │           └── mcp_delete_user.dll
│   │   │   │   ├── mcp_delete_user.csproj.nuget.dgspec.json
│   │   │   │   ├── mcp_delete_user.csproj.nuget.g.props
│   │   │   │   ├── mcp_delete_user.csproj.nuget.g.targets
│   │   │   │   ├── project.assets.json
│   │   │   │   └── project.nuget.cache
│   │   │   └── Program.cs
│   │   ├── Code Archiv
│   │   │   ├── mcp_chat.cs
│   │   │   ├── mcp_continue_chat.cs
│   │   │   ├── mcp_create_source.cs
│   │   │   ├── mcp_delete_group.cs
│   │   │   ├── mcp_delete_source.cs
│   │   │   ├── mcp_delete_user.cs
│   │   │   ├── mcp_edit_source.cs
│   │   │   ├── mcp_edit_user.cs
│   │   │   ├── mcp_get_chat_info.cs
│   │   │   ├── mcp_get_source.cs
│   │   │   ├── mcp_list_groups.cs
│   │   │   ├── mcp_list_sources.cs
│   │   │   ├── mcp_login.cs
│   │   │   ├── mcp_logout.cs
│   │   │   ├── mcp_store_group.cs
│   │   │   └── mcp_store_user.cs
│   │   └── README.md
│   ├── C++
│   │   ├── .vscode
│   │   │   └── launch.json
│   │   ├── 1.0 mcp_login
│   │   │   ├── MCPLoginClient.cpp
│   │   │   └── Non-TLS version
│   │   │       ├── MCPLoginClient.cpp
│   │   │       └── MCPLoginClient.exe
│   │   ├── 1.1 mcp_logout
│   │   │   ├── MCPLogoutClient.cpp
│   │   │   └── MCPLogoutClient.exe
│   │   ├── 2.0 mcp_chat
│   │   │   ├── MCPChatClient.cpp
│   │   │   └── MCPChatClient.exe
│   │   ├── 2.1 mcp_continue_chat
│   │   │   ├── MCPChatContinuationClient.cpp
│   │   │   └── MCPChatContinuationClient.exe
│   │   ├── 2.2 mcp_get_chat_info
│   │   │   ├── MCPGetChatInfoClient.cpp
│   │   │   └── MCPGetChatInfoClient.exe
│   │   ├── 3.0 mcp_create_source
│   │   │   ├── MCPCreateSourceClient.cpp
│   │   │   └── MCPCreateSourceClient.exe
│   │   ├── 3.1 mcp_get_source
│   │   │   ├── MCPGetSourceClient.cpp
│   │   │   └── MCPGetSourceClient.exe
│   │   ├── 3.2 mcp_list_sources
│   │   │   ├── MCPListSourcesClient.cpp
│   │   │   └── MCPListSourcesClient.exe
│   │   ├── 3.3 mcp_edit_source
│   │   │   ├── MCPEditSourceClient.cpp
│   │   │   └── MCPEditSourceClient.exe
│   │   ├── 3.4 mcp_delete_source
│   │   │   ├── MCPDeleteSourceClient.cpp
│   │   │   └── MCPDeleteSourceClient.exe
│   │   ├── 4.0 mcp_list_groups
│   │   │   ├── MCPListGroupsClient.cpp
│   │   │   └── MCPListGroupsClient.exe
│   │   ├── 4.1 mcp_store_group
│   │   │   ├── MCPStoreGroupClient.cpp
│   │   │   └── MCPStoreGroupClient.exe
│   │   ├── 4.2 mcp_delete_group
│   │   │   ├── MPCDeleteGroupClient.cpp
│   │   │   └── MPCDeleteGroupClient.exe
│   │   ├── 5.0 mcp_store_user
│   │   │   ├── MCPStoreUserClient.cpp
│   │   │   └── MCPStoreUserClient.exe
│   │   ├── 5.1 mcp_edit_user
│   │   │   ├── MCPEditUserClient.cpp
│   │   │   └── MCPEditUserClient.exe
│   │   ├── 5.2 mcp_delete_user
│   │   │   ├── MCPDeleteUserClient.cpp
│   │   │   └── MCPDeleteUserClient.exe
│   │   ├── 9.0 mcp_keygen
│   │   │   ├── MCPKeygenClient.cpp
│   │   │   └── MCPKeygenClient.exe
│   │   └── README.md
│   ├── Go
│   │   ├── 1.0 mcp_login
│   │   │   ├── go.mod
│   │   │   ├── MCPLoginClient.exe
│   │   │   └── MCPLoginClient.go
│   │   ├── 1.1 mcp_logout
│   │   │   ├── MCPLogoutClient.exe
│   │   │   └── MCPLogoutClient.go
│   │   ├── 2.0 mcp_chat
│   │   │   ├── MCPChatClient.exe
│   │   │   └── MCPChatClient.go
│   │   ├── 2.1 mcp_continue_chat
│   │   │   ├── MCPChatContinuationClient.exe
│   │   │   └── MCPChatContinuationClient.go
│   │   ├── 2.2 mcp_get_chat_info
│   │   │   ├── MCPGetChatInfoClient.exe
│   │   │   └── MCPGetChatInfoClient.go
│   │   ├── 3.0 mcp_create_source
│   │   │   ├── MCPCreateSourceClient.exe
│   │   │   └── MCPCreateSourceClient.go
│   │   ├── 3.1 mcp_get_source
│   │   │   ├── MCPGetSourceClient.exe
│   │   │   └── MCPGetSourceClient.go
│   │   ├── 3.2 mcp_list_sources
│   │   │   ├── MCPListSourcesClient.exe
│   │   │   └── MCPListSourcesClient.go
│   │   ├── 3.3 mcp_edit_source
│   │   │   ├── MCPEditSourceClient.exe
│   │   │   └── MCPEditSourceClient.go
│   │   ├── 3.4 mcp_delete_source
│   │   │   ├── MCPDeleteSourceClient.exe
│   │   │   └── MCPDeleteSourceClient.go
│   │   ├── 4.0 mcp_list_groups
│   │   │   ├── MCPListGroupsClient.exe
│   │   │   └── MCPListGroupsClient.go
│   │   ├── 4.1 mcp_store_group
│   │   │   ├── MCPStoreGroupClient.exe
│   │   │   └── MCPStoreGroupClient.go
│   │   ├── 4.2 mcp_delete_group
│   │   │   ├── MCPDeleteGroupClient.exe
│   │   │   └── MCPDeleteGroupClient.go
│   │   ├── 5.0 mcp_store_user
│   │   │   ├── MCPStoreUserClient.exe
│   │   │   └── MCPStoreUserClient.go
│   │   ├── 5.1 mcp_edit_user
│   │   │   ├── MCPEditUserClient.exe
│   │   │   └── MCPEditUserClient.go
│   │   ├── 5.2 mcp_delete_user
│   │   │   ├── MCPDeleteUserClient.exe
│   │   │   └── MCPDeleteUserClient.go
│   │   ├── 9.0 mcp_keygen
│   │   │   ├── MCPKeygenClient.exe
│   │   │   └── MCPKeygenClient.go
│   │   └── README.md
│   ├── Gradio
│   │   ├── Api.py
│   │   ├── config.json.example
│   │   ├── config.py
│   │   ├── favicon.ico
│   │   ├── file_tools
│   │   │   └── loader_factory.py
│   │   ├── language.py
│   │   ├── logos
│   │   │   ├── fsas.png
│   │   │   └── Logo_dark.svg
│   │   ├── main.py
│   │   ├── mcp_client.py
│   │   ├── mcp_servers
│   │   │   ├── arxiv
│   │   │   │   ├── arxiv-stdio.js
│   │   │   │   ├── package.json
│   │   │   │   ├── README.md
│   │   │   │   ├── requirements.txt
│   │   │   │   └── server_config.example.json
│   │   │   ├── demo-mcp-server
│   │   │   │   ├── demo-tools-sse.js
│   │   │   │   ├── demo-tools-stdio.js
│   │   │   │   └── tools
│   │   │   │       ├── assets.js
│   │   │   │       ├── calculator.js
│   │   │   │       └── weather.js
│   │   │   ├── filesystem
│   │   │   │   ├── Dockerfile
│   │   │   │   ├── index.ts
│   │   │   │   ├── package.json
│   │   │   │   ├── README.md
│   │   │   │   ├── test
│   │   │   │   │   └── new.txt
│   │   │   │   └── tsconfig.json
│   │   │   ├── moondream
│   │   │   │   └── server.py
│   │   │   ├── pgpt
│   │   │   │   ├── __init__.py
│   │   │   │   ├── Api.py
│   │   │   │   ├── config.json.example
│   │   │   │   ├── config.py
│   │   │   │   ├── language.py
│   │   │   │   ├── pyproject.toml
│   │   │   │   ├── README.md
│   │   │   │   └── server.py
│   │   │   ├── replicate_flux
│   │   │   │   └── server.py
│   │   │   └── sqlite
│   │   │       ├── .python-version
│   │   │       ├── Dockerfile
│   │   │       ├── pyproject.toml
│   │   │       ├── README.md
│   │   │       └── src
│   │   │           └── mcp_server_sqlite
│   │   │               ├── __init__.py
│   │   │               └── server.py
│   │   ├── messages
│   │   │   ├── __init__.py
│   │   │   ├── message_types
│   │   │   │   ├── __init__.py
│   │   │   │   ├── incrementing_id_message.py
│   │   │   │   ├── initialize_message.py
│   │   │   │   ├── json_rpc_message.py
│   │   │   │   ├── ping_message.py
│   │   │   │   ├── prompts_messages.py
│   │   │   │   ├── prompts_models.py
│   │   │   │   ├── resources_messages.py
│   │   │   │   └── tools_messages.py
│   │   │   ├── send_call_tool.py
│   │   │   ├── send_initialize_message.py
│   │   │   ├── send_message.py
│   │   │   ├── send_ping.py
│   │   │   ├── send_prompts.py
│   │   │   ├── send_resources.py
│   │   │   └── send_tools_list.py
│   │   ├── README.md
│   │   ├── requirements.txt
│   │   ├── server_config.json
│   │   ├── SourceManagement.py
│   │   ├── transport
│   │   │   ├── __init__.py
│   │   │   └── stdio
│   │   │       ├── __init__.py
│   │   │       ├── stdio_client.py
│   │   │       ├── stdio_server_parameters.py
│   │   │       └── stdio_server_shutdown.py
│   │   ├── tsconfig.json
│   │   └── UserManagement.py
│   ├── Java
│   │   ├── 1.0 mcp_login
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPLoginClient.class
│   │   │   └── MCPLoginClient.java
│   │   ├── 1.1 mcp_logout
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPLogoutClient.class
│   │   │   └── MCPLogoutClient.java
│   │   ├── 2.0 mcp_chat
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPChatClient.class
│   │   │   └── MCPChatClient.java
│   │   ├── 2.1 mcp_continue_chat
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPContinueChatClient.class
│   │   │   └── MCPContinueChatClient.java
│   │   ├── 2.2 mcp_get_chat_info
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPGetChatInfoClient.class
│   │   │   └── MCPGetChatInfoClient.java
│   │   ├── 3.0 mcp_create_source
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPCreateSourceClient.class
│   │   │   └── MCPCreateSourceClient.java
│   │   ├── 3.1 mcp_get_source
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPGetSourceClient.class
│   │   │   └── MCPGetSourceClient.java
│   │   ├── 3.2 mcp_list_sources
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPListSourcesClient.class
│   │   │   └── MCPListSourcesClient.java
│   │   ├── 3.3 mcp_edit_source
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPEditSourceClient.class
│   │   │   └── MCPEditSourceClient.java
│   │   ├── 3.4 mcp_delete_source
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPDeleteSourceClient.class
│   │   │   └── MCPDeleteSourceClient.java
│   │   ├── 4.0 mcp_list_groups
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPListGroupsClient.class
│   │   │   └── MCPListGroupsClient.java
│   │   ├── 4.1 mcp_store_group
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPStoreGroupClient.class
│   │   │   └── MCPStoreGroupClient.java
│   │   ├── 4.2 mcp_delete_group
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPDeleteGroupClient.class
│   │   │   └── MCPDeleteGroupClient.java
│   │   ├── 5.0 mcp_store_user
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPStoreUserClient.class
│   │   │   └── MCPStoreUserClient.java
│   │   ├── 5.1 mcp_edit_user
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPEditUserClient.class
│   │   │   └── MCPEditUserClient.java
│   │   ├── 5.2 mcp_delete_user
│   │   │   ├── json-20241224.jar
│   │   │   ├── MCPDeleteUserClient.class
│   │   │   └── MCPDeleteUserClient.java
│   │   └── README.md
│   ├── JavaScript
│   │   ├── 1.0 mcp_login
│   │   │   └── MCPLoginClient.js
│   │   ├── 1.1 mcp_logout
│   │   │   └── MCPLogoutClient.js
│   │   ├── 2.0 mcp_chat
│   │   │   └── MCPChatClient.js
│   │   ├── 2.1 mcp_continue_chat
│   │   │   └── MCPContinueChatClient.js
│   │   ├── 2.2 mcp_get_chat_info
│   │   │   └── MCPGetChatInfoClient.js
│   │   ├── 3.0 mcp_create_source
│   │   │   └── MCPCreateSourceClient.js
│   │   ├── 3.1 mcp_get_source
│   │   │   └── MCPGetSourceClient.js
│   │   ├── 3.2 mcp_list_sources
│   │   │   └── MCPListSourcesClient.js
│   │   ├── 3.3 mcp_edit_source
│   │   │   └── MCPEditSourceClient.js
│   │   ├── 3.4 mcp_delete_source
│   │   │   └── MCPDeleteSourceClient.js
│   │   ├── 4.0 mcp_list_groups
│   │   │   └── MCPListGroupsClient.js
│   │   ├── 4.1 mcp_store_group
│   │   │   └── MCPStoreGroupClient.js
│   │   ├── 4.2 mcp_delete_group
│   │   │   └── MCPDeleteGroupClient.js
│   │   ├── 5.0 mcp_store_user
│   │   │   └── MCPStoreUserClient.js
│   │   ├── 5.1 mcp_edit_user
│   │   │   └── MCPEditUserClient.js
│   │   ├── 5.2 mcp_delete_user
│   │   │   └── MCPDeleteUserClient.js
│   │   ├── 9.0 mcp_keygen
│   │   │   └── MCPKeygenClient.js
│   │   └── README.md
│   ├── PHP
│   │   ├── 1.0 mcp_login
│   │   │   └── MCPLoginClient.php
│   │   ├── 1.1 mcp_logout
│   │   │   └── MCPLogoutClient.php
│   │   ├── 2.0 mcp_chat
│   │   │   └── MCPChatClient.php
│   │   ├── 2.1 mcp_continue_chat
│   │   │   └── MCPContinueChatClient.php
│   │   ├── 2.2 mcp_get_chat_info
│   │   │   └── MCPGetChatInfoClient.php
│   │   ├── 3.0 mcp_create_source
│   │   │   └── MCPCreateSourceClient.php
│   │   ├── 3.1 mcp_get_source
│   │   │   └── MCPGetSourceClient.php
│   │   ├── 3.2 mcp_list_sources
│   │   │   └── MCPListSourcesClient.php
│   │   ├── 3.3 mcp_edit_source
│   │   │   └── MCPEditSourceClient.php
│   │   ├── 3.4 mcp_delete_source
│   │   │   └── MCPDeleteSourceClient.php
│   │   ├── 4.0 mcp_list_groups
│   │   │   └── MCPListGroupsClient.php
│   │   ├── 4.1 mcp_store_group
│   │   │   └── MCPStoreGroupClient.php
│   │   ├── 4.2 mcp_delete_group
│   │   │   └── MCPDeleteGroupClient.php
│   │   ├── 5.0 mcp_store_user
│   │   │   └── MCPStoreUserClient.php
│   │   ├── 5.1 mcp_edit_user
│   │   │   └── MCPEditUserClient.php
│   │   ├── 5.2 mcp_delete_user
│   │   │   └── MCPDeleteUserClient.php
│   │   ├── 9.0 mcp_keygen
│   │   │   └── MCPKeygenClient.php
│   │   └── README.md
│   └── Python
│       ├── __init__.py
│       ├── 1.0 mcp_login
│       │   └── MCPLoginClient.py
│       ├── 1.1 mcp_logout
│       │   └── MCPLogoutClient.py
│       ├── 2.0 mcp_chat
│       │   └── MCPChatClient.py
│       ├── 2.1 mcp_continue_chat
│       │   └── MCPContinueChatClient.py
│       ├── 2.2 mcp_get_chat_info
│       │   └── MCPGetChatInfoClient.py
│       ├── 2.3 mcp_delete_all_chats
│       │   └── MCPDeleteAllChatsClient.py
│       ├── 2.4 mcp_delete_chat
│       │   └── MCPDeleteChatClient.py
│       ├── 3.0 mcp_create_source
│       │   └── MCPCreateSourceClient.py
│       ├── 3.1 mcp_get_source
│       │   └── MCPGetSourceClient.py
│       ├── 3.2 mcp_list_sources
│       │   └── MCPListSourcesClient.py
│       ├── 3.3 mcp_edit_source
│       │   └── MCPEditSourceClient.py
│       ├── 3.4 mcp_delete_source
│       │   └── MCPDeleteSourceClient.py
│       ├── 4.0 mcp_list_groups
│       │   └── MCPListGroupsClient.py
│       ├── 4.1 mcp_store_group
│       │   └── MCPStoreGroupClient.py
│       ├── 4.2 mcp_delete_group
│       │   └── MCPDeleteGroupClient.py
│       ├── 5.0 mcp_store_user
│       │   └── MCPStoreUserClient.py
│       ├── 5.1 mcp_edit_user
│       │   └── MCPEditUserClient.py
│       ├── 5.2 mcp_delete_user
│       │   └── MCPDeleteUserClient.py
│       ├── 9.0 mcp_keygen
│       │   └── MCPKeygenClient.py
│       ├── Gradio
│       │   ├── __init__.py
│       │   └── server_config.json
│       └── README.md
├── examples
│   ├── create_users_from_csv
│   │   ├── config.json.example
│   │   ├── config.py
│   │   ├── create_users_from_csv.py
│   │   └── language.py
│   ├── dynamic_sources
│   │   └── rss_reader
│   │       ├── Api.py
│   │       ├── config.json.example
│   │       ├── config.py
│   │       ├── demo_dynamic_sources.py
│   │       └── rss_parser.py
│   ├── example_users_to_add_no_tz.csv
│   └── sftp_upload_with_id
│       ├── Api.py
│       ├── config_ftp.json.example
│       ├── config.py
│       ├── demo_upload.py
│       ├── language.py
│       └── requirements.txt
├── images
│   ├── alternative mcp client.png
│   ├── favicon
│   │   ├── android-chrome-192x192.png
│   │   ├── android-chrome-512x512.png
│   │   ├── apple-touch-icon.png
│   │   ├── favicon-16x16.png
│   │   ├── favicon-32x32.png
│   │   ├── favicon.ico
│   │   └── site.webmanifest
│   ├── mcp-general-architecture.png
│   ├── privateGPT-MCP.png
│   └── privateGPT.png
├── InstallMPCServer.sh
├── jest.config.js
├── LICENSE
├── package.json
├── pgpt.env.json.example
├── README.md
├── security
│   ├── generate_decrypted_password.js
│   └── generate_encrypted_password.js
├── src
│   ├── helper.js
│   ├── index.js
│   ├── logger.js
│   ├── pgpt-messages.js
│   ├── public
│   │   ├── index.html
│   │   └── pgpt-mcp-logo.png
│   ├── services
│   │   └── pgpt-service.ts
│   └── types
│       └── api.ts
├── start_chatbot_agent.ps1
├── start_chatbot_agent.sh
├── start_iot_agent.ps1
├── start_iot_agent.sh
├── start_openai_compatible_api_agent.ps1
├── start_openai_compatible_api_agent.sh
├── tsconfig.json
├── ver
│   ├── index_np.js
│   └── index_proxy_np.js
└── WORKLOG.md
```

# Files

--------------------------------------------------------------------------------
/ver/index_proxy_np.js:
--------------------------------------------------------------------------------

```javascript
  1 | ![PrivateGPT MCP Server](docs/images/pGPT-MCP.png)
  2 | 
  3 | ## Table of Contents
  4 | - [PrivateGPT MCP Server](#privategpt-mcp-server)
  5 | - [What is MCP?](#what-is-mcp)
  6 |   - [Why MCP?](#why-mcp)
  7 |   - [Why Agents](#why-agents)
  8 |   - [How it Works](#how-it-works) 
  9 | - [Interaction Between Agents, LLMs, and MCP Servers](#interaction-between-agents-llms-and-mcp-servers)
 10 |   - [Scenario](#scenario)
 11 |     1. [User Input](#user-input)
 12 |     2. [Agent Processing](#agent-processing)
 13 |     3. [LLM Interaction](#llm-interaction)
 14 |     4. [Response Processing](#response-processing)
 15 |     5. [Security and Logging](#security-and-logging)
 16 | - [Advantages of Using Agents in This Context](#advantages-of-using-agents-in-this-context)
 17 |   - [Modularity](#modularity)
 18 |   - [Security](#security)
 19 |   - [Efficiency](#efficiency)
 20 |   - [Flexibility](#flexibility)
 21 | - [Overview](#overview)
 22 | - [Security Features Overview](#security)
 23 |   - [1. Password Encryption](#1-password-encryption)
 24 |   - [2. Key Management](#2-key-management)
 25 |   - [3. Decryption on the Server](#3-decryption-on-the-server)
 26 |   - [4. Transport Layer Security (TLS)](#4-transport-layer-security-tls)
 27 |   - [5. Authorization Tokens](#5-authorization-tokens)
 28 |   - [6. Restriction of Key Generation (Keygen)](#6-restriction-of-key-generation-keygen)
 29 |   - [7. Certificate-Based Access Control (CBAC)](#7-certificate-based-access-control-cbac)
 30 |   - [8. Secure Configuration](#8-secure-configuration)
 31 |   - [9. Logging and Monitoring](#9-logging-and-monitoring)
 32 |   - [Summary](#summary)
 33 |   - [Encrypted Password Generation Tool](#encrypted-password-generation-tool)
 34 |     1. [Generate a password for the client and/or the server's Proxy_Config](#generate-encrypted-password)
 35 |     2. [Check the generated encrypted password](#check-the-generated-encrypted-password)
 36 |   - [Encrypted Password Decryption Tool](#encrypted-password-decryption-tool)
 37 |     1. [Check the generated encrypted password](#check-the-generated-encrypted-password)
 38 | - [Feature Overview for PGPT Server](#feature-overview-for-pgpt-server)
 39 |   - [1. **Authentication and Authorization**](#1-authentication-and-authorization)
 40 |   - [2. **Chat Management**](#2-chat-management)
 41 |   - [3. **Group Management**](#3-group-management)
 42 |   - [4. **Source Management**](#4-source-management)
 43 |   - [5. **User Management**](#5-user-management)
 44 |   - [6. **Configuration Flexibility**](#6-configuration-flexibility)
 45 |   - [7. **Error Handling and Logging**](#7-error-handling-and-logging)
 46 |   - [8. **Security Features**](#8-security-features)
 47 |   - [Example Use Cases](#example-use-cases)
 48 |   - [How to Use](#how-to-use)
 49 | - [Installation](#installation)
 50 |   - [Prerequisites](#prerequisites)
 51 |   - [Install Dependencies](#install-dependencies)
 52 |   - [Build the Project](#build-the-project-1)
 53 | - [Configuration Description](#configuration-description)
 54 |   - [Server Configuration](#server-configuration-1)
 55 |   - [PGPT URL](#pgpt-url)
 56 |   - [Server Port](#server-port)
 57 |   - [Language](#language-1)
 58 |   - [SSL Validation](#ssl-validation-1)
 59 |   - [Encryption](#encryption)
 60 |   - [Group Restrictions](#group-restrictions)
 61 |   - [Feature Activation/Deactivation](#feature-activationdeactivation)
 62 | - [Usage](#usage-1)
 63 |   - [Available Tools](#available-tools)
 64 | - [Development](#development-1)
 65 |   - [Building](#building-1)
 66 |   - [Type Checking](#type-checking)
 67 |   - [Linting](#linting)
 68 |   - [Testing](#testing)
 69 | - [Project Structure](#project-structure-1)
 70 | - [Error Handling](#error-handling-1)
 71 | - [License](#license)
 72 | 
 73 | 
 74 | # PrivateGPT MCP Server
 75 | A Model Context Protocol (MCP) server implementation that allows you to use PrivateGPT as an agent for your preferred MCP client. 
 76 | This enables seamless integration between PrivateGPT's powerful capabilities and any MCP-compatible application.
 77 | 
 78 | ## What is MCP?
 79 | MCP is an open protocol that standardizes how applications provide context to LLMs. Think of MCP like a USB-C port for AI applications. Just as USB-C provides a standardized way to connect your devices to various peripherals and accessories, MCP provides a standardized way to connect AI models to different data sources and tools.
 80 | 
 81 | ### Why MCP?
 82 | MCP helps you build agents and complex workflows on top of LLMs. LLMs frequently need to integrate with data and tools, and MCP provides:
 83 | - A growing list of pre-built integrations that your LLM can directly plug into
 84 | - The flexibility to switch between LLM providers and vendors
 85 | - Best practices for securing your data within your infrastructure
 86 | 
 87 | ### How it Works
 88 | At its core, MCP follows a client-server architecture where a host application can connect to multiple servers:
 89 | 
 90 | ![MCP General Architecture](docs/images/mcp-general-architecture.png)
 91 | 
 92 | - **MCP Hosts**: Programs like Applications, Claude Desktop, IDEs, or AI tools that want to access data through MCP
 93 | - **MCP Clients**: Protocol clients that maintain 1:1 connections with servers
 94 | - **MCP Servers**: Lightweight programs that each expose specific capabilities through the standardized Model Context Protocol
 95 | - **Local Data Sources**: Your computer's files, databases, and services that MCP servers can securely access
 96 | - **Remote Services**: External systems available over the internet (e.g., through APIs) that MCP servers can connect to
 97 | 
 98 | ## Overview
 99 | This server provides a bridge between MCP clients and the PrivateGPT API, allowing you to:
100 | - Chat with PrivateGPT using both public and private knowledge bases
101 | - Create and manage knowledge sources
102 | - Organize sources into groups
103 | - Control access through group-based permissions
104 | 
105 | 
106 | ---
107 | 
108 | # Why Agents
109 | An **agent** in relation to **LLMs** (Large Language Models) and **MCP servers** is a specialized software component that acts as an intermediary between language models and applications. It handles tasks such as processing requests, interacting with the LLM via MCP, managing workflows, ensuring security and efficiency within the overall system, and much more. By utilizing agents, complex AI-based applications can be designed to be effective, secure, and scalable.
110 | **The code for agents in this repsoitory can be used to implement it into own solutions / applications.**
111 | 
112 | ## Interaction Between Agents, LLMs, and MCP Servers
113 | The interaction of these components enables the development of powerful, scalable, and secure AI applications. Below is a simplified scenario that illustrates this interaction:
114 | 
115 | 1. **User Input:** A user sends a request through a user interface hosted on the MCP server.
116 | 2. **Agent Processing:** An agent on the MCP server receives the request, validates it, and prepares it for the LLM.
117 | 3. **LLM Interaction:** The agent forwards the request to the LLM, which generates a response.
118 | 4. **Response Processing:** The agent receives the response from the LLM, processes it further if necessary (e.g., formatting, integration with other data sources), and sends it back to the user.
119 | 5. **Security and Logging:** During this process, the agent monitors interactions, ensures that security policies are adhered to, and logs relevant information for later analysis.
120 | 
121 | ## Advantages of Using Agents in This Context
122 | - **Modularity:** Agents allow for a clear separation of responsibilities, making the system easier to maintain and scale.
123 | - **Security:** Centralized management of access and monitoring of activities help minimize security risks.
124 | - **Efficiency:** Automated agents can perform tasks faster and more consistently than manual processes.
125 | - **Flexibility:** Agents can be easily adapted or extended to support new functions or changes in business requirements.
126 | 
127 | ### Importance of Encrypting Passwords
128 | In any application handling sensitive data, security is paramount. This server manages two critical sets of credentials:
129 | 1. **Proxy Passwords:** Used by HAProxy to authenticate and manage traffic.
130 | 2. **LLM Access Passwords:** Used to secure access to the large language models.
131 | 
132 | Storing these passwords in plaintext poses significant security risks, including unauthorized access and potential data breaches. 
133 | To mitigate these risks, it is essential to encrypt these passwords and handle only their ciphertext within the system.
134 | 
135 | ### Benefits of Using Ciphertext Only
136 | - **Enhanced Security:** Even if an attacker gains access to the configuration files or environment variables, encrypted passwords remain unintelligible without the corresponding decryption keys.
137 | - **Compliance:** Encryption helps in adhering to security standards and regulations that mandate the protection of sensitive information.
138 | - **Integrity:** Ensures that passwords cannot be tampered with, maintaining the integrity of the authentication mechanisms.
139 | 
140 | ---
141 | 
142 | # Security
143 | The following security features are implemented to ensure data protection and secure communication between the client application and server. These features cover encryption, decryption, key management, and transport security.
144 | 
145 | ## 1. Password Encryption
146 | Passwords can be encrypted using RSA (Rivest–Shamir–Adleman) public-key cryptography. This ensures that sensitive data, such as user passwords, are never transmitted in plaintext.
147 | 
148 | ### Method
149 | - **Public key encryption** with a **2048-bit key length**.
150 | - **Padding**: `RSA_PKCS1_PADDING` to enhance security and prevent known padding attacks.
151 | 
152 | ### Process
153 | 1. The client encrypts the password using the server's public key (`id_rsa_public.pem`).
154 | 2. The encrypted password is sent to the server, where it is decrypted using the server's private key.
155 | 
156 | ### Advantages
157 | - **Asymmetric encryption** ensures that only the server can decrypt the password.
158 | - Even if the communication channel is compromised, encrypted data remains secure.
159 | 
160 | ## 2. Key Management
161 | To secure data communication and encryption processes, the following key management principles are followed:
162 | 
163 | ### Public Key
164 | - Stored securely on the server (`id_rsa_public.pem`).
165 | - Used only for encryption and does not pose a security risk if exposed.
166 | 
167 | ### Private Key
168 | - Stored securely on the server (`id_rsa`).
169 | - Restricted access with appropriate file permissions (`chmod 600`).
170 | - Used exclusively for decryption operations.
171 | 
172 | ### Key Rotation
173 | - Keys are rotated periodically or upon detection of a security incident.
174 | - Old keys are securely invalidated.
175 | 
176 | ## 3. Decryption on the Server
177 | Decryption is exclusively performed on the server using the private key:
178 | 
179 | ### Process
180 | 1. The server receives the encrypted password from the client.
181 | 2. The private key decrypts the password to retrieve the original plaintext.
182 | 3. The decrypted password is used internally (e.g., authentication) and never stored in plaintext.
183 | 
184 | ### Secure Handling
185 | - Decrypted passwords exist in memory only for the duration of processing.
186 | - Secure memory management practices ensure sensitive data is cleared immediately after use.
187 | 
188 | ## 4. Transport Layer Security (TLS)
189 | To secure communication between the client and server:
190 | 
191 | ### TLS Encryption
192 | - All data transmitted between the client and server is encrypted using TLS (minimum version 1.2).
193 | - Prevents man-in-the-middle (MITM) attacks and eavesdropping.
194 | 
195 | ### Certificate Validation
196 | - Certificates are validated on both sides to ensure the authenticity of the server and client.
197 | - Optionally, mutual TLS can be enabled for enhanced security.
198 | 
199 | ## 5. Authorization Tokens
200 | Tokens are used to authenticate requests and ensure only authorized users can access the system:
201 | 
202 | ### Token Management
203 | - Tokens are generated upon successful login.
204 | - They are short-lived and automatically expire after a predefined time.
205 | - Tokens are signed using HMAC or RSA, making them tamper-proof.
206 | 
207 | ### Secure Storage
208 | - Tokens are stored securely on the client side (e.g., in memory or encrypted storage).
209 | 
210 | ## 6. Restriction of Key Generation (Keygen)
211 | To prevent misuse of the system, key generation (`keygen`) is restricted:
212 | 
213 | ### Configuration
214 | - The server has a configuration option (`ALLOW_KEYGEN`) to enable or disable key generation.
215 | - Attempts to call the keygen function when disabled result in an error message.
216 | 
217 | ### Audit Logging
218 | - All keygen operations are logged for auditing and monitoring purposes.
219 | 
220 | ## 7. Certificate-Based Access Control (CBAC)
221 | - As the agent does not require a password when certificate authentication is activated and logs on to the server using a key, it is automatically locked to this server. 
222 | If it would want to log in to another MCP PGPT server, this login attempt is rejected as the key is checked against the server's private certificate. 
223 | 
224 | ### Features
225 | - Functions such as `keygen`, `store_user`, and `edit_source` are only accessible to authorized roles.
226 | - Unauthorized access attempts are denied with detailed error messages.
227 | 
228 | ### Configuration
229 | - Enabled or disabled functions can be specified in the server's configuration (`Functions` object).
230 | 
231 | ## 8. Secure Configuration
232 | The server configuration contains several security-related options:
233 | 
234 | ### SSL_VALIDATE
235 | - Validates SSL/TLS certificates to ensure secure communication.
236 | - Can be enabled or disabled based on environment requirements (e.g., testing vs. production).
237 | 
238 | ### PW_ENCRYPTION
239 | - Enables or disables password encryption.
240 | - Ensures compatibility in environments where encryption is not feasible.
241 | 
242 | ## 9. Logging and Monitoring
243 | All security-related events are logged for monitoring and troubleshooting:
244 | 
245 | ### Logged Events
246 | - Failed login attempts.
247 | - Key generation requests.
248 | - Unauthorized access attempts.
249 | - Encryption and decryption errors.
250 | 
251 | 
252 | ## Summary
253 | The implemented security features ensure:
254 | 
255 | - **Confidentiality** of sensitive data through encryption and secure transport.
256 | - **Integrity** of operations using robust key management and token validation.
257 | - **Role-based and configurable access control** for system functions.
258 | - **Comprehensive monitoring** for proactive detection and response to security threats.
259 | 
260 | These measures collectively provide a secure environment for client-server communication and data handling.
261 | 
262 | 
263 | # Feature Overview for PGPT Server
264 | The PGPT Server offers a robust set of features designed to provide efficient, flexible, and secure communication with the Model Context Protocol (MCP). Below is an overview of the key features and functionalities available in the server.
265 | 
266 | ---
267 | 
268 | ## Key Features
269 | 
270 | ### 1. **Authentication and Authorization**
271 | - **Login Functionality**: Authenticate users with email and password to generate access tokens.
272 | - **Logout Functionality**: Securely invalidate user tokens to end sessions.
273 | 
274 | ---
275 | 
276 | ### 2. **Chat Management**
277 | - **Start a Chat**: Initiate a conversation with the server, optionally using public knowledge bases or specific group contexts.
278 | - **Continue a Chat**: Resume an ongoing conversation by providing the chat ID and a follow-up message.
279 | - **Retrieve Chat Information**: Fetch metadata and messages for a specific chat by its ID.
280 | 
281 | ---
282 | 
283 | ### 3. **Group Management**
284 | - **List Groups**: View personal and assignable groups available to the user.
285 | - **Create Groups**: Add new groups with names and descriptions for organizational purposes.
286 | - **Delete Groups**: Remove existing groups (if enabled in configuration).
287 | 
288 | ---
289 | 
290 | ### 4. **Source Management**
291 | - **Create Sources**: Add new sources with content and assign them to specific groups.
292 | - **Edit Sources**: Update existing sources with new content or metadata.
293 | - **Delete Sources**: Remove sources that are no longer needed.
294 | - **Retrieve Sources**: Fetch information about a specific source by its ID.
295 | - **List Sources**: View all sources assigned to a specific group.
296 | 
297 | ---
298 | 
299 | ### 5. **User Management**
300 | - **Create Users**: Register new users with customizable roles, groups, and settings.
301 | - **Edit Users**: Update user information, including name, email, password, and roles.
302 | - **Delete Users**: Remove users from the system (if enabled in configuration).
303 | 
304 | ---
305 | 
306 | ### 6. **Configuration Flexibility**
307 | - **Function Activation/Deactivation**: Enable or disable individual server functionalities through the `.env` configuration file.
308 | - **Language Support**: Customize the server's system messages to your preferred language (e.g., English or German).
309 | - **SSL Validation**: Toggle SSL validation for secure connections to the server.
310 | 
311 | ---
312 | 
313 | ### 7. **Error Handling and Logging**
314 | - Detailed error messages and logging for:
315 |   - Invalid requests
316 |   - Authentication failures
317 |   - API communication issues
318 | - Configurable responses when a disabled function is accessed.
319 | 
320 | ---
321 | 
322 | ### 8. **Security Features**
323 | - **Token-Based Authentication**: Ensures secure and controlled access to server features.
324 | - **Restricted Group Access**: Option to limit access to `assignableGroups` for enhanced privacy.
325 | 
326 | ---
327 | 
328 | ### Example Use Cases
329 | 1. **Customer Support**: Use chat features to build intelligent conversational agents.
330 | 2. **Knowledge Management**: Manage and retrieve structured data with sources and groups.
331 | 3. **Multi-User Collaboration**: Create, edit, and assign users to groups for collaborative workflows.
332 | 4. **Customizable Functionality**: Activate only the features you need for your application.
333 | 
334 | ---
335 | 
336 | ### How to Use
337 | 1. Configure the server by editing the `.env` file.
338 | 2. Start the server using the provided script.
339 | 3. Interact with the server via API calls to utilize its features.
340 | 
341 | Refer to the **API Documentation** for detailed usage instructions and examples for each endpoint.
342 | 
343 | ---
344 | 
345 | The PGPT Server is a powerful tool for managing structured communication and data in a customizable environment. Tailor its features to your needs for maximum efficiency and control.
346 | 
347 | 
348 | ## Installation
349 | 1. Clone the repository:
350 | ```bash
351 | git clone https://github.com/pgpt-dev/MCP-Server-for-MAS-Developments.git
352 | cd MCP-Server-for-MAS-Developments
353 | ```
354 | 
355 | 2. Install npm:
356 | ```bash
357 | sudo apt install npm
358 | ```
359 | 3. Install Dependencies
360 | ```bash
361 | npm install
362 | ```
363 | 
364 |    and 
365 | 
366 | ```bash
367 | npm install dotenv
368 | npm install moment
369 | ```
370 | 
371 | 4. Build the project:
372 | ```bash
373 | npm run build
374 | ```
375 | 
376 | ---
377 | 
378 | ## Configuration Description
379 | 
380 | ### Server Configuration
381 | 
382 | For secure certificate authentification, create a `.env` file with your PrivateGPT credentials, for example pgpt.env.json 
383 | Settings can be adjusted in the `.env` file to customize the server and its functionalities.
384 | 
385 | Generate the cerificates, .pem files are needed:
386 | ```bash
387 | ssh-keygen -f ~/.ssh/id_rsa.pub -e -m PEM > ~/.ssh/id_rsa_public.pem
388 | ssh-keygen -p -m PEM -f ~/.ssh/id_rsa
389 | ```
390 | 
391 | After this process, you can create Ciphertext from passwords by using the Encrypted Password Encryption Tool and test the cipher with the Encrypted Password Decryption Tool.
392 | You will find the descriptionof how it works in the `Security` section of this document.
393 | 
394 | ---
395 | 
396 | Below is a sample `.env` configuration file for the PGPT server, including descriptions for each setting. 
397 | Customize these values to fit your environment and requirements.
398 | 
399 | ---
400 | 
401 | ## PGPT URL
402 | 
403 | | Key                       | Description                                              | Example Value                                |
404 | |---------------------------|----------------------------------------------------------|----------------------------------------------|
405 | | **PRIVATE_GPT_API_URL**   | The base URL of the Private GPT API.                     | `https://<YOUR DOMAIN OR IP>/api/v1`         |
406 | | **API_URL**               | Alias for the base API URL.                              | `https://*<YOUR DOMAIN OR IP>*/api/v1`       |
407 | 
408 | ---
409 | 
410 | ## Server Port
411 | | Key      | Description                                    | Example Value |
412 | |----------|------------------------------------------------|---------------|
413 | | **PORT** | The port on which the MCP server runs.         | `5000`        |
414 | 
415 | ---
416 | 
417 | ## Language
418 | | Key        | Description                                                         | Example Value |
419 | |------------|---------------------------------------------------------------------|---------------|
420 | | **LANGUAGE** | The language for the server's system messages (`en` or `de`).     | `"de"`        |
421 | 
422 | ---
423 | 
424 | ## SSL Validation
425 | | Key            | Description                                                                             | Example Value |
426 | |----------------|-----------------------------------------------------------------------------------------|---------------|
427 | | **SSL_VALIDATE** | Use `"false"` only if the server's certificate cannot be validated by the client.     | `"false"`     |
428 | 
429 | ---
430 | 
431 | ## Encryption
432 | | Key               | Description                                                                          | Example Value                    |
433 | |-------------------|--------------------------------------------------------------------------------------|----------------------------------|
434 | | **PW_ENCRYPTION** | If set to `"true"` the server only accepts passwords in Ciphertext.                  | `"false"`                        |
435 | | **PUBLIC_KEY**    | Specifies the file system path to the server's public PEM file used for SSL/TLS.     | `"~/.ssh/id_rsa_public.pem"`     |
436 | | **PRIVATE_KEY**   | Specifies the file system path to the server's private key file used for decryption. | `"~/.ssh/id_rsa_public.pem"`     |
437 | 
438 | ---
439 | 
440 | ## Group Restrictions
441 | | Key                 | Description                                                                                     | Example Value |
442 | |---------------------|-------------------------------------------------------------------------------------------------|---------------|
443 | | **RESTRICTED_GROUPS** | Setting `true` prevents client access to `assignableGroups`.                                  | `false`       |
444 | 
445 | ---
446 | 
447 | ## Feature Activation/Deactivation
448 | Control the availability of individual server functions. Set the corresponding value to `true` to enable the function, or `false` to disable it. Disabled functions will return a message indicating they are not available.
449 | 
450 | | Key                       | Description                                        | Example Value |
451 | |---------------------------|----------------------------------------------------|---------------|
452 | | **ENABLE_LOGIN**          | Enables or disables the login function.            | `true`        |
453 | | **ENABLE_LOGOUT**         | Enables or disables the logout function.           | `true`        |
454 | | **ENABLE_CHAT**           | Enables or disables the chat functionality.        | `true`        |
455 | | **ENABLE_CONTINUE_CHAT**  | Enables or disables continuing a chat.             | `true`        |
456 | | **ENABLE_GET_CHAT_INFO**  | Enables or disables retrieving chat information.   | `true`        |
457 | | **ENABLE_LIST_GROUPS**    | Enables or disables listing groups.                | `true`        |
458 | | **ENABLE_STORE_GROUP**    | Enables or disables creating a group.              | `true`        |
459 | | **ENABLE_DELETE_GROUP**   | Enables or disables deleting a group.              | `false`       |
460 | | **ENABLE_CREATE_SOURCE**  | Enables or disables creating a source.             | `true`        |
461 | | **ENABLE_EDIT_SOURCE**    | Enables or disables editing a source.              | `true`        |
462 | | **ENABLE_DELETE_SOURCE**  | Enables or disables deleting a source.             | `true`        |
463 | | **ENABLE_GET_SOURCE**     | Enables or disables retrieving a source.           | `true`        |
464 | | **ENABLE_LIST_SOURCES**   | Enables or disables listing sources.               | `true`        |
465 | | **ENABLE_STORE_USER**     | Enables or disables creating a user.               | `true`        |
466 | | **ENABLE_EDIT_USER**      | Enables or disables editing a user.                | `false`       |
467 | | **ENABLE_DELETE_USER**    | Enables or disables deleting a user.               | `false`       |
468 | 
469 | ---
470 | 
471 | ## Usage
472 | - **Enable a Function**: Set the corresponding value in the `.env` file to `true`.
473 | - **Disable a Function**: Set the corresponding value in the `.env` file to `false`.
474 |   - The server will respond with a message indicating that the function is disabled.
475 | 
476 | Example `.env` entry:
477 | ```dotenv
478 | {
479 |     "PGPT_Url": {
480 |         "PRIVATE_GPT_API_URL": "https://<YOUR_PGPT_URL>/api/v1",
481 |         "API_URL": "https://<YOUR_PGPT_URL>/api/v1"
482 |     },
483 |     "Proxy_Config": {
484 |     "USE_PROXY": "true",
485 |     "AUTH_REQUIRED": "true",
486 |     "UNSECURE_PW": "false",
487 |     "USER": "username@<MY_PGPT_DOMAIN>",
488 |     "PASSWORD": "Example: H3i2ufJEV8v5eQTeArvKIvl..."
489 |     },        
490 |     "Server_Config": {
491 |         "PORT": 5000,
492 |         "LANGUAGE": "en",
493 |         "SSL_VALIDATE": "false",
494 |         "PW_ENCRYPTION": "true",
495 |         "ALLOW_KEYGEN": "false",
496 |         "PUBLIC_KEY": "/<SERVER_PATH>/.ssh/id_rsa_public.pem",
497 |         "PRIVATE_KEY": "/<SERVER_PATH>/.ssh/id_rsa"
498 |     },
499 |     "Restrictions": {
500 |         "RESTRICTED_GROUPS": false
501 |     },
502 |     "Functions": {
503 |         "ENABLE_LOGIN": true,
504 |         "ENABLE_LOGOUT": true,
505 |         "ENABLE_CHAT": true,
506 |         "ENABLE_CONTINUE_CHAT": true,
507 |         "ENABLE_GET_CHAT_INFO": true,
508 |         "ENABLE_LIST_GROUPS": true,
509 |         "ENABLE_STORE_GROUP": true,
510 |         "ENABLE_DELETE_GROUP": false,
511 |         "ENABLE_CREATE_SOURCE": true,
512 |         "ENABLE_EDIT_SOURCE": true,
513 |         "ENABLE_DELETE_SOURCE": false,
514 |         "ENABLE_GET_SOURCE": true,
515 |         "ENABLE_LIST_SOURCES": true,
516 |         "ENABLE_STORE_USER": true,
517 |         "ENABLE_EDIT_USER": false,
518 |         "ENABLE_DELETE_USER": false
519 |     }
520 | }
521 | ```
522 | 
523 | ## Usage
524 | Start the server:
525 | ```bash
526 | node dist/index.js
527 | ```
528 | 
529 | The server will start and listen on stdio for MCP commands.
530 | 
531 | ## Project Structure
532 | ```
533 | src/
534 |   ├── index.js                        # Main server implementation
535 |   ├── types/                          # TypeScript type definitions
536 |   │   └── api.ts                      # API interface types
537 |   └── security/                       # Security Tools
538 |   │   └── generate_encrypted_password # Encrypt the passwod and use Cyphertext for login. It replaces the plain text password in the --password parameter and has to executed on the server.
539 |   │   └── generate_decrypted_password # Decrypt the Cyphertext to get the password. Execute it on the server to ensure the cyphertext contains the correct password.
540 |   └── services/                       # Service implementations
541 |   │   └── pgpt-service.ts             # PrivateGPT API service
542 |   └── clients/                        # Service implementations
543 |       └── Python                      # **Python**
544 |       │	  └── mcp...                  # Client examples written in Python
545 |       └── C# .Net                     # **C#**
546 |       │	  └── Code                    # Original files
547 |       │	    └── mcp...                # Client examples written in C#
548 |       └── C++                         # **C++**
549 |       │	  └── mcp...                  # Client examples written in C++
550 |       └── Java                        # **Java**
551 |       │	  └── mcp...                  # Client examples written in Java
552 |       └── JavaScrip                   # **JavaScript**
553 |       │	  └── mcp...                  # Client examples written in JavaScript
554 |       └── PHP                         # **PHP**
555 |       │	  └── mcp...                  # Client examples written in PHP
556 |       └── Go                          # **Go**
557 |           └── mcp...                  # Client examples written in Go
558 | ```
559 | 
560 | ## Error Handling
561 | The server handles various error scenarios:
562 | - Authentication failures
563 | - Network errors
564 | - Invalid requests
565 | - API errors
566 | - Rate limiting
567 | - Timeout errors
568 | 
569 | Errors are mapped to appropriate MCP error codes and include detailed messages for debugging.
570 | 
571 | ## Available Tools
572 | 
573 | ### Generate Encrypted Password
574 | Generate a password for the client and/or the server's Proxy_Config->Password entry:
575 | ```bash
576 |  node security/generate_encrypted_password.js ~/.ssh/id_rsa_public.pem
577 | ```
578 | 
579 | ### Check the generated encrypted password
580 | To check the encrytion use:
581 | ```bash
582 |  node security/generate_decrypted_password.js ~/.ssh/id_rsa
583 | ```
584 | 
585 | See the sections `Encrypted Password Generation Tool` and `Encrypted Password Decryption Tool` below for further information
586 | 
587 | ---
588 | 
589 | # Encrypted Password Generation Tool
590 | 
591 | ## Overview
592 | The **Encrypted Password Generation Tool** is a Node.js script designed to securely encrypt user passwords using RSA public-key cryptography. This tool ensures that sensitive password data remains protected during transmission and storage by leveraging robust encryption mechanisms. It is an essential component for systems requiring secure password handling and transmission between clients and servers.
593 | 
594 | ## Features
595 | - **Secure Encryption:** Utilizes RSA (Rivest–Shamir–Adleman) public-key cryptography to encrypt sensitive passwords.
596 | - **User-Friendly Interface:** Prompts users to input their passwords securely via the command line.
597 | - **Error Handling:** Provides comprehensive error messages for missing keys or encryption issues.
598 | - **Flexible Integration:** Can be integrated into larger systems requiring secure password handling and encryption.
599 | 
600 | ## How It Works
601 | 1. **Public Key Loading:** The script loads the RSA public key from a specified file path provided as a command-line argument.
602 | 2. **Password Input:** It prompts the user to enter their password securely via the command line.
603 | 3. **Encryption Process:** Using the loaded public key and `RSA_PKCS1_PADDING`, the script encrypts the entered password.
604 | 4. **Output:** The encrypted password is displayed in Base64 format, ready for secure transmission or storage.
605 | 
606 | ## Prerequisites
607 | - **Node.js:** Ensure that Node.js is installed on your system. You can download it from the [Node.js Official Website](https://nodejs.org/).
608 | - **RSA Public Key:** You must have access to the RSA public key (`id_rsa_public.pem`) used for encrypting the password.
609 | 
610 | ## Installation
611 | -  **Install Dependencies:**
612 |     The script uses built-in Node.js modules, so no additional dependencies are required. However, ensure that your Node.js version supports ES6 modules.
613 |     ```bash
614 |     npm install
615 |     ```
616 | 
617 | ## Usage
618 | 1. **Prepare Your RSA Public Key:**
619 |     Ensure you have your RSA public key (`id_rsa_public.pem`) stored securely on your MCP server.
620 | 
621 | 2. **Run the Script, you will find it at the `security` directory of the MCP server:**
622 |     Execute the script using Node.js, providing the path to your public key as a command-line argument.
623 |     ```bash
624 |     node encrypt_password.js /path/to/your/id_rsa_public.pem
625 |     ```
626 | 
627 |     **Example:**
628 |     ```bash
629 |     node security/encrypt_password.js ~/.ssh/id_rsa_public.pem
630 |     ```
631 | 
632 | 3. **Enter Your Password:**
633 |     When prompted, input your password securely.
634 |     ```bash
635 |     Please enter your password: ********
636 |     ```
637 | 
638 | 4. **View the Encrypted Password:**
639 |     The script will output the encrypted password in Base64 format.
640 |     ```bash
641 |     Encrypted Password: <Your_Encrypted_Password>
642 |     ```
643 | 
644 | ---
645 | 
646 | # Encrypted Password Decryption Tool
647 | 
648 | ## Overview
649 | The **Encrypted Password Decryption Tool** is a Node.js script designed to securely decrypt encrypted passwords using RSA private-key cryptography. 
650 | This tool ensures that sensitive password data remains protected during transmission and storage by leveraging robust encryption and decryption mechanisms.
651 | To verify or decrypt an encrypted password, use the private key. This is helpful to ensure that the encryption was performed correctly.
652 | 
653 | ## Features
654 | - **Secure Decryption:** Utilizes RSA (Rivest–Shamir–Adleman) private-key cryptography to decrypt sensitive password data.
655 | - **Error Handling:** Provides comprehensive error messages for missing keys or decryption issues.
656 | - **User-Friendly Interface:** Prompts users to input encrypted passwords securely via the command line.
657 | - **Flexible Integration:** Can be integrated into larger systems requiring secure password handling.
658 | 
659 | ## How It Works
660 | 1. **Private Key Loading:** The script loads the RSA private key from a specified file path provided as a command-line argument.
661 | 2. **Encrypted Password Input:** It prompts the user to enter an encrypted password in Base64 format.
662 | 3. **Decryption Process:** Using the loaded private key and RSA_PKCS1_PADDING, the script decrypts the encrypted password.
663 | 4. **Output:** The decrypted plaintext password is displayed in the console.
664 | 
665 | ## Prerequisites
666 | - **Node.js:** Ensure that Node.js is installed on your system. You can download it from [Node.js Official Website](https://nodejs.org/).
667 | - **RSA Private Key:** You must have access to the RSA private key (`id_rsa`) on your MCP server used for decrypting the password.
668 | 
669 | ## Installation
670 | - **Install Dependencies:**
671 |     The script uses built-in Node.js modules, so no additional dependencies are required. However, ensure that your Node.js version supports ES6 modules.
672 | 
673 | ## Usage
674 | 1. **Prepare Your RSA Private Key:**
675 |     Ensure you have your RSA private key (`id_rsa`) stored securely on your machine.
676 | 
677 | 2. **Run the Script, you will find it at the `security` directory of the MCP server:**:**
678 |     Execute the script using Node.js, providing the path to your private key as a command-line argument.
679 |     ```bash
680 |     node decrypt_password.js /path/to/your/id_rsa
681 |     ```
682 | 
683 |     **Example:**
684 |     ```bash
685 |     node decrypt_password.js ~/.ssh/id_rsa
686 |     ```
687 | 
688 | 3. **Enter the Encrypted Password:**
689 |     When prompted, input the encrypted password in Base64 format.
690 |     ```bash
691 |     Please enter the encrypted password: <Your_Encrypted_Password>
692 |     ```
693 | 
694 | 4. **View the Decrypted Password:**
695 |     The script will output the decrypted plaintext password.
696 |     ```bash
697 |     Decrypted Password: your_plaintext_password
698 |     ```
699 | This will decrypt the encrypted password and display the original value.
700 | 
701 | 
702 | ### Notes
703 | - Ensure that the `~/.ssh/id_rsa_public.pem` (public key) and `~/.ssh/id_rsa` (private key) files exist and have the correct permissions.
704 | - The encryption tool relies on the public key, while the decryption tool requires the private key.
705 | 
706 | ## License
707 | This project is licensed under the MIT License - see the LICENSE file for details.
708 | 
```

--------------------------------------------------------------------------------
/agents/ISMAgent/Python/ism_agent.py:
--------------------------------------------------------------------------------

```python
  1 | # ============================================================
  2 | # ISM Agent – generate descriptive text for ISM nodes
  3 | # - Structured white console logs (emoji-safe, aligned)
  4 | # - Optional AgentInterface imports (PrivateGPTAgent, etc.)
  5 | # - HTTP (FIPA-ACL) request to chatbot agent
  6 | # - Robust error handling, retries with backoff, NDJSON events
  7 | # - Paths (input/output/logs) are read from config["paths"]
  8 | # - After successful input read: archive input with sequential extension
  9 | # - NEW: Optional SFTP upload of the output file after completion
 10 | # - NEW: Delete local output file upon successful SFTP upload
 11 | # - NEW: logge Dauer zwischen chatbot request und response
 12 | # ============================================================
 13 | 
 14 | import json
 15 | import re
 16 | import time
 17 | import sys
 18 | import argparse
 19 | import logging
 20 | import os
 21 | import shutil
 22 | from datetime import datetime
 23 | from pathlib import Path
 24 | from typing import Any, Dict, List, Optional
 25 | 
 26 | import requests
 27 | from colorama import init as colorama_init, Fore, Style
 28 | from wcwidth import wcswidth  # emoji/wide-char aware width calc
 29 | 
 30 | # NEW: SFTP deps
 31 | import paramiko
 32 | import posixpath
 33 | 
 34 | # ------------------------------------------------------------
 35 | # OPTIONAL: AgentInterface (PrivateGPT) – best-effort imports
 36 | # ------------------------------------------------------------
 37 | try:
 38 |     from .AgentInterface.Python.agent import PrivateGPTAgent, GroupValidationError
 39 |     from .AgentInterface.Python.config import Config as PGPTConfig, ConfigError as PGPTConfigError
 40 |     from .AgentInterface.Python.language import languages as pgpt_languages
 41 |     from .AgentInterface.Python.color import Color
 42 |     _HAS_AGENT_IFACE = True
 43 | except Exception:
 44 |     _HAS_AGENT_IFACE = False
 45 | 
 46 | colorama_init()  # enable ANSI handling on Windows
 47 | 
 48 | # ============================================================
 49 | # Structured console / file logging (white text only)
 50 | # ============================================================
 51 | class StructuredLog:
 52 |     """IoT-style structured console/file logger with white text only and emoji-safe alignment."""
 53 |     COL_TIME = 19
 54 |     COL_ICON = 4
 55 |     COL_COMP = 12
 56 |     COL_ACT = 14
 57 |     COL_DIR = 9
 58 | 
 59 |     LEVEL_ICONS = {
 60 |         "DEBUG": "🐛",
 61 |         "INFO": "ℹ️",
 62 |         "WARNING": "⚠️",
 63 |         "ERROR": "❌",
 64 |         "CRITICAL": "‼️",
 65 |     }
 66 | 
 67 |     def __init__(self, ndjson_path: Optional[str] = None, use_color: bool = True):
 68 |         self.ndjson_path = ndjson_path
 69 |         self.use_color = use_color
 70 |         self._ensure_dir()
 71 | 
 72 |     # ---------------- internal helpers ----------------
 73 |     def _ensure_dir(self) -> None:
 74 |         if self.ndjson_path:
 75 |             os.makedirs(os.path.dirname(self.ndjson_path), exist_ok=True)
 76 |             if not os.path.exists(self.ndjson_path):
 77 |                 with open(self.ndjson_path, "w", encoding="utf-8"):
 78 |                     pass
 79 | 
 80 |     @staticmethod
 81 |     def _ts() -> str:
 82 |         return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 83 | 
 84 |     @staticmethod
 85 |     def _pad_raw(s: str, width: int) -> str:
 86 |         """Pad string to visual width 'width' (handles emojis & wide chars)."""
 87 |         s = (s or "")
 88 |         vis = wcswidth(s)
 89 |         if vis < 0:
 90 |             vis = len(s)
 91 |         return s + " " * max(0, width - vis)
 92 | 
 93 |     @staticmethod
 94 |     def _pad_raw_right_aligned(s: str, width: int) -> str:
 95 |         """Pad string to visual width 'width', right-aligned."""
 96 |         s = (s or "")
 97 |         vis = wcswidth(s)
 98 |         if vis < 0:
 99 |             vis = len(s)
100 |         return " " * max(0, width - vis) + s
101 | 
102 |     def _white(self, text: str) -> str:
103 |         """Force bright white output for all columns."""
104 |         if not self.use_color:
105 |             return text
106 |         return f"{Style.BRIGHT}{Fore.WHITE}{text}{Style.RESET_ALL}"
107 | 
108 |     def _icon(self, kind: str, level: Optional[str] = None) -> str:
109 |         """Return an icon (emoji + trailing pipe)."""
110 |         if level:
111 |             lvl = level.upper()
112 |             if lvl in self.LEVEL_ICONS:
113 |                 return self.LEVEL_ICONS[lvl] #+ " |"
114 |         return "ℹ️ "
115 | 
116 |     # >>> FIXED HERE <<< (kein level mehr!)
117 |     def _line(self, icon: str, component: str, action: str, direction: str, message: str) -> str:
118 |         # icon ist bereits fertig (z. B. "ℹ️ |" oder "‼️ |")
119 |         t_col = self._pad_raw(self._ts(), self.COL_TIME)
120 |         i_col = self._pad_raw(icon, self.COL_ICON)
121 |         c_col = self._pad_raw(component, self.COL_COMP)
122 |         a_col = self._pad_raw(action, self.COL_ACT)
123 | 
124 |         # bestimmte Felder rechtsbündig
125 |         if (
126 |             component.lower() == "filesystem" and (action == ":write" or action == ":append")
127 |         ) or (component.lower() == "ism" and action == ":process"):
128 |             d_col = self._pad_raw_right_aligned(direction, self.COL_DIR)
129 |         else:
130 |             d_col = self._pad_raw(direction, self.COL_DIR)
131 | 
132 |         c_col = self._white(c_col)
133 |         a_col = self._white(a_col)
134 |         d_col = self._white(d_col)
135 |         msg_col = self._white(message or "")
136 | 
137 |         return "".join(
138 |             [
139 |                 t_col,
140 |                 " | ",
141 |                 i_col,
142 |                 " ",
143 |                 c_col,
144 |                 " ",
145 |                 a_col,
146 |                 " ",
147 |                 d_col,
148 |                 " | ",
149 |                 msg_col,
150 |             ]
151 |         )
152 | 
153 |     # ---------------- public API ----------------
154 |     def console(
155 |         self,
156 |         icon_kind: str,
157 |         component: str,
158 |         action: str,
159 |         direction: str,
160 |         message: str,
161 |         level: str = "info",
162 |     ) -> None:
163 |         line = self._line(self._icon(icon_kind, level), component, action, direction, message)
164 |         lvl = (level or "info").lower()
165 |         if lvl == "error":
166 |             logging.error(line)
167 |         elif lvl == "warning":
168 |             logging.warning(line)
169 |         elif lvl == "debug":
170 |             logging.debug(line)
171 |         elif lvl == "critical":
172 |             logging.critical(line)
173 |         else:
174 |             logging.info(line)
175 | 
176 |     def file_event(self, **record) -> None:
177 |         """Append a JSON record to NDJSON log (if configured)."""
178 |         if not self.ndjson_path:
179 |             return
180 |         record.setdefault("ts", self._ts())
181 |         with open(self.ndjson_path, "a", encoding="utf-8") as f:
182 |             f.write(json.dumps(record, ensure_ascii=False) + "\n")
183 | 
184 | 
185 | slog: Optional[StructuredLog] = None
186 | 
187 | 
188 | # ============================================================
189 | # Logging setup
190 | # ============================================================
191 | def setup_logging(verbose: bool):
192 |     level = logging.DEBUG if verbose else logging.INFO
193 |     logging.basicConfig(level=level, format="%(message)s")
194 | 
195 | 
196 | # ============================================================
197 | # Config / Input Utilities
198 | # ============================================================
199 | def load_config(path: Path) -> Dict[str, Any]:
200 |     if not path.exists():
201 |         raise FileNotFoundError(f"Configuration file not found: {path}")
202 |     try:
203 |         data = json.loads(path.read_text(encoding="utf-8"))
204 |     except json.JSONDecodeError as e:
205 |         raise ValueError(f"Invalid JSON in configuration file: {path} ({e})")
206 | 
207 |     chatbot = data.get("chatbot_agent", {})
208 |     if not chatbot.get("api_url"):
209 |         raise ValueError("Missing 'chatbot_agent.api_url' in config.")
210 |     if not chatbot.get("api_key"):
211 |         raise ValueError("Missing 'chatbot_agent.api_key' in config.")
212 | 
213 |     chatbot.setdefault("use_public", True)
214 |     chatbot.setdefault("groups", [])
215 |     chatbot.setdefault("timeout_seconds", 20)
216 |     chatbot.setdefault("prompt_template", "prompt_template parameter not set. Repeat this sentence.")
217 |     data["chatbot_agent"] = chatbot
218 | 
219 |     data.setdefault("language", "en")
220 | 
221 |     paths = data.get("paths", {})
222 |     paths.setdefault("input", "agents/ISMAgent/data/ism_nodes.json")
223 |     paths.setdefault("inventory", "agents/ISMAgent/data/ism_inventory.json")
224 |     paths.setdefault("output", "agents/ISMAgent/output/ism_nodes_report.txt")
225 |     paths.setdefault("ndjson", "agents/ISMAgent/logs/ism_agent.ndjson")
226 |     paths.setdefault("dump_json_dir", "agents/ISMAgent/logs/node_json")
227 |     paths.setdefault("archive_dir", "agents/ISMAgent/archive")
228 |     data["paths"] = paths
229 | 
230 |     # optionale SFTP-Konfig
231 |     sftp = data.get("sftp", {})
232 |     if sftp:
233 |         sftp.setdefault("enabled", True)
234 |         sftp.setdefault("port", 22)
235 |         sftp.setdefault("remote_path", "/")
236 |         sftp.setdefault("remote_filename", None)
237 |         data["sftp"] = sftp
238 | 
239 |     return data
240 | 
241 | 
242 | def _parse_pdf_json(pdf_path: Path) -> Dict[str, Any]:
243 |     try:
244 |         import PyPDF2
245 |     except Exception as e:
246 |         raise RuntimeError("PyPDF2 is required to read PDFs. Install via 'pip install PyPDF2'.") from e
247 | 
248 |     text_chunks = []
249 |     with open(pdf_path, "rb") as f:
250 |         reader = PyPDF2.PdfReader(f)
251 |         for page in reader.pages:
252 |             text_chunks.append(page.extract_text() or "")
253 |     full = "\n".join(text_chunks)
254 |     start, end = full.find("{"), full.rfind("}")
255 |     if start == -1 or end == -1 or end <= start:
256 |         raise ValueError(f"No JSON found inside PDF: {pdf_path}")
257 |     json_text = full[start : end + 1]
258 |     json_text = re.sub(r"-\n", "", json_text)
259 |     json_text = re.sub(r"[ \t\r\f\v]+", " ", json_text)
260 |     try:
261 |         return json.loads(json_text)
262 |     except json.JSONDecodeError:
263 |         json_text_2 = re.sub(r",\s*([}\]])", r"\1", json_text)
264 |         return json.loads(json_text_2)
265 | 
266 | 
267 | def load_nodes(path: Path) -> List[Dict[str, Any]]:
268 |     if slog:
269 |         slog.console("info", "ism", ":filesystem", "read", f"Reading input file: {path}")
270 | 
271 |     if not path.exists():
272 |         if slog:
273 |             slog.console("error", "ism", ":filesystem", "Error", f"Input file not found: {path}", level="error")
274 |         raise FileNotFoundError(f"Input file not found: {path}")
275 | 
276 |     suffix = path.suffix.lower()
277 |     if suffix == ".json":
278 |         try:
279 |             data = json.loads(path.read_text(encoding="utf-8"))
280 |         except json.JSONDecodeError as e:
281 |             if slog:
282 |                 slog.console("error", "ism", ":json", "Error", f"Invalid JSON: {e}", level="error")
283 |             raise
284 |     elif suffix == ".pdf":
285 |         data = _parse_pdf_json(path)
286 |     else:
287 |         raise ValueError(f"Unsupported input file type: {suffix} (expected .json or .pdf)")
288 | 
289 |     if isinstance(data, dict):
290 |         nodes = (data.get("IsmBody") or {}).get("Nodes") or []
291 |     elif isinstance(data, list):
292 |         nodes = data
293 |     else:
294 |         raise ValueError("Unexpected JSON type (expected dict or list).")
295 | 
296 |     if not isinstance(nodes, list):
297 |         raise ValueError("'Nodes' field is not a list.")
298 | 
299 |     if not nodes:
300 |         if slog:
301 |             slog.console("warning", "ism", ":json", "-", "No nodes found in input.", level="warning")
302 |     else:
303 |         if slog:
304 |             slog.console("info", "ism", ":json", "-", f"{len(nodes)} nodes loaded.")
305 |     if slog:
306 |         slog.file_event(event="nodes_loaded", count=len(nodes), source=str(path))
307 |     return nodes
308 | 
309 | 
310 | def load_inventory_map(path: Path) -> Dict[int, Dict[str, Any]]:
311 |     if slog:
312 |         slog.console("info", "ism", ":filesystem", "read", f"Reading inventory file: {path}")
313 | 
314 |     if not path.exists():
315 |         if slog:
316 |             slog.console(
317 |                 "warning",
318 |                 "ism",
319 |                 ":filesystem",
320 |                 "Warn",
321 |                 f"Inventory file not found: {path}. Continuing without detailed inventory.",
322 |                 level="warning",
323 |             )
324 |         return {}
325 | 
326 |     try:
327 |         data = json.loads(path.read_text(encoding="utf-8"))
328 |     except json.JSONDecodeError as e:
329 |         if slog:
330 |             slog.console(
331 |                 "error",
332 |                 "ism",
333 |                 ":json",
334 |                 "Error",
335 |                 f"Invalid JSON in inventory: {e}. Continuing with empty inventory.",
336 |                 level="error",
337 |             )
338 |         return {}
339 | 
340 |     nodes = (data.get("IsmBody") or {}).get("Nodes") or []
341 |     inventory_map = {}
342 | 
343 |     for node in nodes:
344 |         node_id = node.get("NodeId")
345 |         if node_id is not None:
346 |             inventory_map[int(node_id)] = node.get("VariableData", {})
347 | 
348 |     if slog:
349 |         slog.console("info", "ism", ":json", "-", f"{len(inventory_map)} inventory details mapped.")
350 |         slog.file_event(event="inventory_mapped", count=len(inventory_map), source=str(path))
351 | 
352 |     return inventory_map
353 | 
354 | 
355 | # ============================================================
356 | # Health check (optional)
357 | # ============================================================
358 | def check_server_health(cfg: Dict[str, Any]) -> None:
359 |     url = (cfg.get("chatbot_agent") or {}).get("health_url")
360 |     if not url:
361 |         return
362 |     try:
363 |         r = requests.get(url, timeout=5)
364 |         if r.status_code >= 400:
365 |             if slog:
366 |                 slog.console("warning", "chatbot", ":health", "Warn", f"{r.status_code}: {r.text[:200]}", level="warning")
367 |     except Exception as e:
368 |         if slog:
369 |             slog.console("warning", "chatbot", ":health", "Error", f"{e}", level="warning")
370 | 
371 | 
372 | # ============================================================
373 | # Node parameter builder
374 | # ============================================================
375 | def _v(x: Any) -> str:
376 |     if x is None:
377 |         return ""
378 |     s = str(x).strip()
379 |     return s if s and s not in ("-", "None", "null", "NULL") and not s.endswith(" -") else ""
380 | 
381 | 
382 | def node_params(node: Dict[str, Any], inventory_map: Dict[int, Dict[str, Any]]) -> Dict[str, Any]:
383 |     node_id = int(node.get("NodeId", 0))
384 |     inv_data = inventory_map.get(node_id, {})
385 | 
386 |     params = {
387 |         "Node Name": _v(node.get("Name")),
388 |         "NodeId": _v(node.get("NodeId")),
389 |         "Type": _v(node.get("Type")),
390 |         "Model": _v(node.get("Model")),
391 |         "Status": _v(node.get("Status")),
392 |         "AlarmStatus": _v(node.get("AlarmStatus")),
393 |         "PowerStatus": _v(node.get("PowerStatus")),
394 |         "IP-Version": _v(node.get("IpVersion")),
395 |         "IP": _v(node.get("IpAddress")),
396 |         "WEB-URL": _v(node.get("WebUrl")),
397 |         "Rack Position": _v((node.get("RackInfo") or {}).get("Name")),
398 |         "Node Group": _v(node.get("NodeGroupName")),
399 |     }
400 | 
401 |     if inv_data:
402 |         cpus = inv_data.get("Cpus", [])
403 |         if cpus:
404 |             cpu_model = _v(cpus[0].get("Model"))
405 |             cpu_core_speed = _v(cpus[0].get("CoreSpeed"))
406 |             cpu_count = len(cpus)
407 |             if cpu_model and cpu_core_speed:
408 |                 params["CPU Summary"] = f"{cpu_count}x {cpu_model} @ {_v(cpu_core_speed)}MHz"
409 |             elif cpu_count > 0:
410 |                 params["CPU Summary"] = f"{cpu_count}x CPU (Details missing)"
411 | 
412 |         memory_modules = [m for m in inv_data.get("MemoryModules", []) if _v(m.get("MemorySize"))]
413 |         total_mem_gb = 0
414 |         if memory_modules:
415 |             for m in memory_modules:
416 |                 size_str = _v(m.get("MemorySize"))
417 |                 if size_str and "GB" in size_str:
418 |                     try:
419 |                         total_mem_gb += int(size_str.replace("GB", "").strip())
420 |                     except ValueError:
421 |                         pass
422 |             if total_mem_gb > 0:
423 |                 mem_freq = _v(memory_modules[0].get("Frequency"))
424 |                 params["Memory Summary"] = f"{len(memory_modules)} physical modules, {total_mem_gb}GB total RAM @ {mem_freq}"
425 | 
426 |         disks = inv_data.get("Disks", [])
427 |         if disks:
428 |             disk_count = len(disks)
429 |             disk_types = ", ".join(
430 |                 sorted(list(set([_v(d.get("MediaType")) for d in disks if _v(d.get("MediaType"))])))
431 |             )
432 |             disk_models = ", ".join(
433 |                 sorted(list(set([_v(d.get("Model")) for d in disks if _v(d.get("Model"))])))
434 |             )
435 |             total_raid_capacity_bytes = sum(
436 |                 [
437 |                     int(_v(r.get("TotalCapacity", 0)))
438 |                     for r in inv_data.get("Raid", [])
439 |                     if _v(r.get("TotalCapacityUnit")) == "B"
440 |                 ]
441 |             )
442 |             total_raid_capacity_tb = round(total_raid_capacity_bytes / (1000**4), 2)
443 |             if disk_types or disk_models or total_raid_capacity_bytes > 0:
444 |                 params["Storage Summary"] = (
445 |                     f"{disk_count} disks ({disk_types}), "
446 |                     f"{total_raid_capacity_tb}TB RAID capacity, models: {disk_models}"
447 |                 )
448 | 
449 |         os_list = inv_data.get("ElcmStatus", {}).get("SupportedOsList", [])
450 |         if os_list:
451 |             supported_os = ", ".join(
452 |                 sorted(list(set([_v(os.get("OsType")) for os in os_list if _v(os.get("OsType"))])))
453 |             )
454 |             if supported_os:
455 |                 params["Supported OS List"] = supported_os
456 | 
457 |         firmware_details = []
458 |         for fw in inv_data.get("Firmware", []):
459 |             fw_type = _v(fw.get("Type"))
460 |             fw_version = _v(fw.get("FirmwareVersion"))
461 |             fw_model = _v(fw.get("Model"))
462 |             if fw_type and fw_version:
463 |                 detail = f"{fw_model or 'Unknown'} {fw_type}: {fw_version}"
464 |                 firmware_details.append(detail)
465 |         if firmware_details:
466 |             params["Firmware Details"] = "; ".join(firmware_details)
467 | 
468 |         disk_health_issues = [d for d in disks if _v(d.get("Health")) and int(_v(d.get("Health"))) < 100]
469 |         if disk_health_issues:
470 |             params["Hardware Issues"] = "Disk health warning or failure detected."
471 |             params["Disk Health Issues"] = (
472 |                 f"{len(disk_health_issues)} disks report issues (e.g., predicted life left < 100%)."
473 |             )
474 | 
475 |     description = _v(node.get("Description"))
476 |     if description:
477 |         params["Node Description"] = description
478 | 
479 |     hardware_issues_from_nodes = _v(node.get("HardwareIssues")) or "No specific hardware problems mentioned."
480 |     params.setdefault("Hardware Issues", hardware_issues_from_nodes)
481 | 
482 |     final_params = {k: v for k, v in params.items() if v and v not in ("-", "None", "null", "NULL", "Not specified")}
483 |     return final_params
484 | 
485 | 
486 | # ============================================================
487 | # Chatbot Request (FIPA-ACL) – One Node per Request with retry
488 | # ============================================================
489 | def generate_logical_sentence(
490 |     parameters: Dict[str, Any],
491 |     language_code: str,
492 |     config: Dict[str, Any],
493 |     use_public: Optional[bool] = None,
494 |     groups: Optional[List[str]] = None,
495 |     wait_seconds: float = 5.0,
496 |     max_retries: int = 5,
497 | ) -> str:
498 |     attempt = 0
499 |     last_error = None
500 | 
501 |     prompt_template = config.get("chatbot_agent", {}).get("prompt_template")
502 |     if not prompt_template:
503 |         prompt_template = (
504 |             "Generate a fluent, well-written paragraph in {language_code} describing the following node. "
505 |             "It should read like a technical report (no tables or bullet points). "
506 |             "Here are the data:\n"
507 |             "{json_data}"
508 |         )
509 | 
510 |     prompt = prompt_template.format(
511 |         language_code=language_code.upper(),
512 |         json_data=json.dumps(parameters, ensure_ascii=False, indent=4),
513 |     )
514 | 
515 |     if use_public is None:
516 |         use_public = bool(config.get("chatbot_agent", {}).get("use_public", True))
517 |     if groups is None:
518 |         groups = config.get("chatbot_agent", {}).get("groups", [])
519 |     if not isinstance(groups, list):
520 |         groups = []
521 | 
522 |     timeout_sec = int(config.get("chatbot_agent", {}).get("timeout_seconds", 20))
523 |     api_url = config["chatbot_agent"]["api_url"]
524 | 
525 |     node_name = parameters.get("Node Name", "<unknown>")
526 | 
527 |     while attempt < max_retries:
528 |         attempt += 1
529 |         try:
530 |             payload = {
531 |                 "performative": "request",
532 |                 "sender": "ISM_Agent",
533 |                 "receiver": "Chatbot_Agent",
534 |                 "language": "fipa-sl",
535 |                 "ontology": "fujitsu-iot-ontology",
536 |                 "content": {
537 |                     "question": prompt,
538 |                     "usePublic": use_public,
539 |                     "groups": groups,
540 |                     "language": language_code or config.get("language", "en"),
541 |                     "json_data": parameters,
542 |                     "node": parameters,  # falls der Server 'node' statt 'json_data' erwartet
543 |                 },
544 |             }
545 | 
546 |             headers = {
547 |                 "Content-Type": "application/json",
548 |                 "X-API-KEY": config["chatbot_agent"]["api_key"],
549 |             }
550 | 
551 |             if slog:
552 |                 slog.console("cb", "chatbot", ":request", "Outgoing", f"Request for node: {node_name}")
553 |                 slog.file_event(event="request", component="chatbot_agent", node=node_name)
554 | 
555 |             # >>> NEU: Zeitmessung
556 |             t_start = time.perf_counter()
557 |             response = requests.post(api_url, json=payload, headers=headers, timeout=timeout_sec)
558 |             t_end = time.perf_counter()
559 |             elapsed = t_end - t_start  # Sekunden als float
560 | 
561 |             if slog:
562 |                 slog.console(
563 |                     "cb",
564 |                     "chatbot",
565 |                     ":response",
566 |                     "Incoming",
567 |                     f"{response.status_code} ({elapsed:.3f}s)",
568 |                 )
569 |                 slog.file_event(
570 |                     event="response",
571 |                     status=response.status_code,
572 |                     node=node_name,
573 |                     elapsed_seconds=round(elapsed, 3),
574 |                 )
575 |             # <<< ENDE NEU
576 | 
577 |             if response.status_code != 200:
578 |                 try:
579 |                     body = response.json()
580 |                 except Exception:
581 |                     body = response.text
582 |                 raise RuntimeError(f"HTTP {response.status_code}: {body}")
583 | 
584 |             data = response.json()
585 |             generated_sentence = (
586 |                 (data.get("content") or {}).get("answer")
587 |                 or data.get("answer")
588 |                 or data.get("response")
589 |                 or ""
590 |             )
591 |             if not generated_sentence:
592 |                 raise RuntimeError(f"Empty response from chatbot agent: {data}")
593 | 
594 |             return generated_sentence.strip()
595 | 
596 |         except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
597 |             last_error = f"Network error: {e}"
598 |             backoff = min(wait_seconds * (2 ** (attempt - 1)), 30)
599 |             if slog:
600 |                 slog.console("warning", "chatbot", ":network", "Error", last_error, level="warning")
601 |             time.sleep(backoff)
602 |         except Exception as e:
603 |             last_error = f"Chatbot request failed: {e}"
604 |             backoff = min(wait_seconds * (2 ** (attempt - 1)), 30)
605 |             if slog:
606 |                 slog.console("warning", "chatbot", ":error", "-", last_error, level="warning")
607 |             time.sleep(backoff)
608 | 
609 |     raise RuntimeError(last_error or "Unknown chatbot request error.")
610 | 
611 | 
612 | # ============================================================
613 | # Per-node JSON dump helpers
614 | # ============================================================
615 | def safe_filename(s: str) -> str:
616 |     s = re.sub(r"[^\w\-.]+", "_", s.strip())
617 |     return s or "noname"
618 | 
619 | 
620 | def dump_node_json(
621 |     dump_dir: Optional[Path],
622 |     idx: int,
623 |     node_name: str,
624 |     params: Dict[str, Any],
625 |     answer: Optional[str] = None,
626 | ):
627 |     if dump_dir is None:
628 |         return
629 |     dump_dir.mkdir(parents=True, exist_ok=True)
630 |     ts = datetime.now().strftime("%Y%m%d%H%M%S")
631 |     fname = f"{ts}_{idx:05d}_{safe_filename(node_name)}.json"
632 |     payload = {
633 |         "node": node_name,
634 |         "index": idx,
635 |         "parameters": params,
636 |         "answer": answer,
637 |         "timestamp": ts,
638 |     }
639 | 
640 |     json_text = json.dumps(payload, ensure_ascii=False, indent=2)
641 |     dump_length_bytes = len(json_text.encode("utf-8"))
642 | 
643 |     (dump_dir / fname).write_text(json_text, encoding="utf-8")
644 | 
645 |     if slog:
646 |         slog.console(
647 |             "file",
648 |             "filesystem",
649 |             ":write",
650 |             str(dump_length_bytes) + " B",
651 |             f"Record added to {dump_dir.name}/{fname}",
652 |         )
653 |         slog.file_event(
654 |             event="node_dump_written",
655 |             file=str(dump_dir / fname),
656 |             node=node_name,
657 |             size=dump_length_bytes,
658 |         )
659 | 
660 | 
661 | # ============================================================
662 | # Input Archiving helpers
663 | # ============================================================
664 | def _next_archive_path(src: Path, archive_dir: Path) -> Path:
665 |     archive_dir.mkdir(parents=True, exist_ok=True)
666 |     stem_with_suffix = src.name
667 |     max_idx = 0
668 |     for p in archive_dir.glob(stem_with_suffix + ".*"):
669 |         suf = p.suffix
670 |         if len(suf) >= 2 and suf[1:].isdigit():
671 |             try:
672 |                 idx = int(suf[1:])
673 |                 if idx > max_idx:
674 |                     max_idx = idx
675 |             except ValueError:
676 |                 pass
677 |     next_idx = max_idx + 1
678 |     return archive_dir / f"{stem_with_suffix}.{next_idx:03d}"
679 | 
680 | 
681 | def archive_input_file(src: Path, archive_dir: Path) -> Optional[Path]:
682 |     try:
683 |         try:
684 |             src.resolve().relative_to(archive_dir.resolve())
685 |             return None
686 |         except Exception:
687 |             pass
688 | 
689 |         if not src.exists():
690 |             return None
691 |         target = _next_archive_path(src, archive_dir)
692 |         shutil.move(str(src), str(target))
693 |         if slog:
694 |             slog.console("file", "filesystem", ":archive", "-", f"Archived input to: {target}")
695 |             slog.file_event(event="input_archived", source=str(src), target=str(target))
696 |         return target
697 |     except Exception as e:
698 |         if slog:
699 |             slog.console("warning", "filesystem", ":archive", "Error", f"{e}", level="warning")
700 |             slog.file_event(event="archive_failed", source=str(src), error=str(e))
701 |         return None
702 | 
703 | 
704 | # ============================================================
705 | # NEW: SFTP helpers
706 | # ============================================================
707 | def _sftp_mkdirs(sftp: paramiko.SFTPClient, remote_dir: str) -> None:
708 |     remote_dir = posixpath.normpath(remote_dir)
709 |     parts = [p for p in remote_dir.split("/") if p]
710 |     path = "/"
711 |     for p in parts:
712 |         path = posixpath.join(path, p)
713 |         try:
714 |             sftp.chdir(path)
715 |         except IOError:
716 |             sftp.mkdir(path)
717 |             sftp.chdir(path)
718 | 
719 | 
720 | def sftp_upload_file(local_path: Path, sftp_cfg: Dict[str, Any]) -> bool:
721 |     enabled = sftp_cfg.get("enabled", False)
722 |     host = sftp_cfg.get("host")
723 |     user = sftp_cfg.get("user") or sftp_cfg.get("username")
724 |     pwd = sftp_cfg.get("password")
725 |     port = int(sftp_cfg.get("port", 22))
726 |     remote_base = sftp_cfg.get("remote_path", "/")
727 |     remote_name = sftp_cfg.get("remote_filename") or local_path.name
728 | 
729 |     if not enabled or not host or not user or not pwd:
730 |         if slog:
731 |             slog.console("warning", "sftp", ":config", "Skip", "SFTP enabled but config incomplete.", level="warning")
732 |             slog.file_event(event="sftp_skipped", reason="incomplete_config")
733 |         return False
734 | 
735 |     transport = None
736 |     try:
737 |         if slog:
738 |             slog.console("info", "sftp", ":connect", "Outgoing", f"{user}@{host}:{port}")
739 |         transport = paramiko.Transport((host, port))
740 |         transport.connect(username=user, password=pwd)
741 |         sftp = paramiko.SFTPClient.from_transport(transport)
742 | 
743 |         _sftp_mkdirs(sftp, remote_base)
744 | 
745 |         remote_path = posixpath.join(remote_base, remote_name)
746 |         sftp.put(str(local_path), remote_path)
747 | 
748 |         if slog:
749 |             slog.console("info", "sftp", ":put", "Outgoing", f"{local_path} → {remote_path}")
750 |             slog.file_event(event="sftp_upload_ok", local=str(local_path), remote=remote_path)
751 | 
752 |         sftp.close()
753 |         transport.close()
754 |         return True
755 |     except Exception as e:
756 |         if slog:
757 |             slog.console("error", "sftp", ":put", "Error", f"{e}", level="error")
758 |             slog.file_event(event="sftp_upload_failed", local=str(local_path), error=str(e))
759 |         if transport:
760 |             try:
761 |                 transport.close()
762 |             except Exception:
763 |                 pass
764 |         return False
765 | 
766 | 
767 | # ============================================================
768 | # Main
769 | # ============================================================
770 | def main():
771 |     parser = argparse.ArgumentParser(description="ISM Agent – robust generator for ISM nodes.")
772 |     parser.add_argument("--config", default="agents/ISMAgent/config.json", help="Path to config.json")
773 |     parser.add_argument("--language", help="Override language from config (optional)")
774 |     parser.add_argument("--delay", type=float, default=0.5, help="Seconds to wait between requests")
775 |     parser.add_argument("--verbose", action="store_true", help="Enable verbose logging")
776 |     args = parser.parse_args()
777 | 
778 |     setup_logging(args.verbose)
779 |     global slog
780 | 
781 |     cfg = load_config(Path(args.config))
782 |     paths = cfg.get("paths", {})
783 |     input_path = Path(paths.get("input", "agents/ISMAgent/data/ism_nodes.json"))
784 |     inventory_path = Path(paths.get("inventory", "agents/ISMAgent/data/ism_inventory.json"))
785 |     output_path = Path(paths.get("output", "agents/ISMAgent/output/ism_nodes_report.txt"))
786 |     ndjson_path = paths.get("ndjson", "agents/ISMAgent/logs/ism_agent.ndjson")
787 |     dump_dir = Path(paths.get("dump_json_dir", "agents/ISMAgent/logs/node_json"))
788 |     archive_dir = Path(paths.get("archive_dir", "agents/ISMAgent/archive"))
789 | 
790 |     slog = StructuredLog(ndjson_path, use_color=True)
791 | 
792 |     check_server_health(cfg)
793 | 
794 |     lang = (args.language or cfg.get("language") or "en").strip().lower()
795 | 
796 |     try:
797 |         nodes = load_nodes(input_path)
798 |     except FileNotFoundError as e:
799 |         if slog:
800 |             slog.console("critical", "main", ":fatal", "Error", f"Fatal: Missing primary input file. {e}", level="critical")
801 |         sys.exit(1)
802 |     except Exception as e:
803 |         if slog:
804 |             slog.console("critical", "main", ":fatal", "Error", f"Fatal: Error loading nodes. {e}", level="critical")
805 |         sys.exit(1)
806 | 
807 |     try:
808 |         inventory_map = load_inventory_map(inventory_path)
809 |     except Exception:
810 |         inventory_map = {}
811 | 
812 |     archive_input_file(input_path, archive_dir)
813 | 
814 |     results: List[str] = []
815 | 
816 |     for idx, node in enumerate(nodes, 1):
817 |         node_name = node.get("Name", f"Node{idx}")
818 |         try:
819 |             params = node_params(node, inventory_map)
820 |             counter_str = f"{idx}/{len(nodes)}"
821 | 
822 |             if slog:
823 |                 slog.console("proc", "ism", ":process", counter_str, f"Processing node: {node_name}")
824 |                 slog.file_event(event="node_processing", node=node_name, index=idx)
825 | 
826 |             text = generate_logical_sentence(params, lang, cfg, max_retries=5)
827 |             results.append(text.strip())
828 | 
829 |             dump_node_json(dump_dir, idx, node_name, params, text)
830 | 
831 |             time.sleep(max(0.0, float(args.delay)))
832 |         except Exception as e:
833 |             if slog:
834 |                 slog.console("error", "ism", ":process", "Error", f"{node_name}: {e}", level="error")
835 |                 slog.file_event(event="node_failed", node=node_name, error=str(e))
836 |             # continue
837 | 
838 |     if not results:
839 |         if slog:
840 |             slog.console("error", "main", ":report", "Error", "No report could be generated.", level="error")
841 |         sys.exit(2)
842 | 
843 |     wrote_ok = False
844 |     try:
845 |         output_path.parent.mkdir(parents=True, exist_ok=True)
846 |         out_text = "\n\n".join(results).rstrip() + "\n"
847 |         output_length_bytes = len(out_text.encode("utf-8"))
848 |         byte_output_str = str(output_length_bytes) + "B"
849 | 
850 |         if output_path.exists():
851 |             with open(output_path, "a", encoding="utf-8") as f:
852 |                 f.write(out_text)
853 |             if slog:
854 |                 slog.console("file", "filesystem", ":append", byte_output_str, f"Appended {output_length_bytes} bytes to: {output_path}")
855 |                 slog.file_event(event="report_appended", path=str(output_path), size=len(out_text))
856 |         else:
857 |             with open(output_path, "w", encoding="utf-8") as f:
858 |                 f.write(out_text)
859 |             if slog:
860 |                 slog.console("file", "filesystem", ":write", byte_output_str, f"Report created: {output_path}")
861 |                 slog.file_event(event="report_written", path=str(output_path), size=len(out_text))
862 |         wrote_ok = True
863 |     except Exception as e:
864 |         if slog:
865 |             slog.console("error", "filesystem", ":write", "Error", f"{e}", level="error")
866 |         sys.exit(3)
867 | 
868 |     if wrote_ok:
869 |         sftp_cfg = cfg.get("sftp") or {}
870 |         if sftp_cfg.get("enabled", False):
871 |             upload_ok = sftp_upload_file(output_path, sftp_cfg)
872 | 
873 |             if upload_ok:
874 |                 try:
875 |                     os.remove(output_path)
876 |                     if slog:
877 |                         slog.console("file", "filesystem", ":delete", "Done", f"Local report deleted after successful SFTP: {output_path}")
878 |                         slog.file_event(event="report_deleted", path=str(output_path))
879 |                 except Exception as e:
880 |                     if slog:
881 |                         slog.console("error", "filesystem", ":delete", "Error", f"Failed to delete local report: {e}", level="error")
882 |                         slog.file_event(event="delete_failed", path=str(output_path), error=str(e))
883 |             else:
884 |                 if slog:
885 |                     slog.console("warning", "sftp", ":post", "Warn", "Upload failed; report remains local.", level="warning")
886 |         else:
887 |             if slog:
888 |                 slog.console("info", "sftp", ":post", "Skip", "SFTP disabled in config.")
889 | 
890 | 
891 | if __name__ == "__main__":
892 |     try:
893 |         main()
894 |     except KeyboardInterrupt:
895 |         if slog:
896 |             slog.console("warning", "main", ":shutdown", "-", "User aborted (CTRL+C).", level="warning")
897 |         sys.exit(130)
898 |     except Exception as e:
899 |         if slog:
900 |             slog.console("critical", "main", ":fatal", "Error", f"{e}", level="critical")
901 |         sys.exit(99)
902 | 
```

--------------------------------------------------------------------------------
/clients/Gradio/main.py:
--------------------------------------------------------------------------------

```python
   1 | import asyncio
   2 | import base64
   3 | import io
   4 | import json
   5 | import os
   6 | import shutil
   7 | import time
   8 | import uuid
   9 | from datetime import datetime
  10 | from pathlib import Path
  11 | 
  12 | import gradio as gr
  13 | import httpx
  14 | from PIL import Image
  15 | from gradio_modal import Modal
  16 | from openai import OpenAI
  17 | 
  18 | from agents.AgentInterface.Python.config import Config, ConfigError
  19 | from clients.Gradio.Api import PrivateGPTAPI
  20 | from clients.Gradio.file_tools.loader_factory import LoadersFactory
  21 | from clients.Gradio.mcp_client import MCPClient, generate_system_prompt, load_config, clean_response
  22 | from clients.Gradio.messages.send_call_tool import send_call_tool
  23 | from clients.Gradio.messages.send_initialize_message import send_initialize
  24 | from clients.Gradio.messages.send_tools_list import send_tools_list
  25 | from clients.Gradio.transport.stdio.stdio_client import stdio_client
  26 | 
  27 | 
  28 | # config
  29 | mcp_config = "./clients/Gradio/server_config.json"
  30 | 
  31 | #selection of mcp servers from the config
  32 | server_names = ["dp"] #"demo-tools", "filesystem", "sqlite", "nostr",  "agent_web_search", "hf_flux", ] #"google-calendar"] #
  33 | # if all_mcp_servers is True, the above list will be overwritten and all servers in the config will be considered
  34 | all_mcp_servers = True
  35 | 
  36 | temperature = 0.8
  37 | top_p = 0.8
  38 | #model = "/models/mistral-nemo-12b" #vllm
  39 | #model = "mistralai/Mistral-Small-3.1-24B-Instruct-2503"
  40 | #model = "mistralai/Mistral-Small-3.2-24B-Instruct-2506"
  41 | md_model = None
  42 | 
  43 | 
  44 | image_url = None
  45 | 
  46 | 
  47 | 
  48 | # Load configuration file
  49 | try:
  50 |     # Get the absolute path to the config.json file
  51 |     config_file = Path.absolute(Path(__file__).parent / "config.json")
  52 |     # Initialize configuration with required fields
  53 |     config = Config(config_file=config_file, required_fields=["base_url"])
  54 |     # Retrieve default groups and VLLM configuration from config file
  55 |     default_groups = config.get("groups", [])
  56 |     vllm_url =  config.get("vllm_url", "")
  57 |     vllm_api_key = config.get("vllm_api_key", "")
  58 |     model = config.get("model", "/models/mistral-nemo-12b")
  59 |     access_header = config.get("access_header", None)
  60 |     proxy_user = config.get("proxy_user", None)
  61 |     if proxy_user == "":
  62 |         proxy_user = None
  63 |     proxy_password = config.get("proxy_password", None)
  64 |     if proxy_password == "":
  65 |         proxy_password = None
  66 | except ConfigError as e:
  67 |     # Display an error message and exit if configuration cannot be loaded
  68 |     print(f"Configuration Error: {e}")
  69 |     exit(1)
  70 | 
  71 | # If all_mcp_servers is set to True, load all mcp servers from the config file
  72 | if all_mcp_servers:
  73 |     with open(mcp_config, 'r') as f:
  74 |         # Read the config file and extract mcp server names
  75 |         server_names = list(json.load(f)['mcpServers'].keys())
  76 |         print(server_names)
  77 | 
  78 | 
  79 | 
  80 | mcp_servers = []
  81 | 
  82 | #user_data_source = ["User1", "User2", "User3", "User4", "User5"]
  83 | selected_groups = []
  84 | pgpt = None
  85 | 
  86 | # Function to handle login logic
  87 | async def login(username, password, selected_options, selected_options2):
  88 |     global pgpt
  89 |     config.set_value("email", username)
  90 |     config.set_value("password", password)
  91 |     pgpt = PrivateGPTAPI(config)
  92 |     if pgpt.logged_in:
  93 |         # Successful login
  94 |         groups = pgpt.list_personal_groups()
  95 |         return gr.update(visible=False), gr.update(visible=True), "", gr.update(choices=groups, value=None), gr.update(choices=groups, value=None)
  96 |     else:
  97 |         gr.Warning("Error connecting.")
  98 |         return gr.update(), gr.update(visible=False), "Invalid credentials. Please try again.", gr.update(choices=[], value=None), gr.update(choices=[], value=None)
  99 | 
 100 | MAX_ITEMS = 200  # Max number of sources
 101 | 
 102 | def delete_source(sources, index):
 103 |     if 0 <= index < len(sources):
 104 |         source_to_delete = sources[index]
 105 |         print(source_to_delete)
 106 |         status = pgpt.delete_source(source_to_delete["id"])
 107 |         if status == "success":
 108 |             gr.Success("Entry deleted")
 109 |         else:
 110 |             gr.Error("Failed")
 111 |         sources.pop(index)
 112 | 
 113 |     return sources
 114 | 
 115 | def render_ui(sources):
 116 |     updates = []
 117 |     if sources is None:
 118 |         sources = []
 119 |     for i in range(MAX_ITEMS):
 120 |         if i < len(sources):
 121 |             src = sources[i]
 122 |             updates.extend([
 123 |                 gr.update(visible=True),                # Row visible
 124 |                 gr.update(value=src["name"]),           # Name
 125 |                 gr.update(value="\n".join(src["groups"])),         # Groups
 126 |                 gr.update(value=src["creator"]),        # Creator
 127 |                 gr.update(value=src["date"]),           # Date
 128 |                 gr.update(value=src["status"]),         # Status
 129 |                 gr.update(visible=True),                # Delete button visible
 130 |             ])
 131 |         else:
 132 |             updates.extend([
 133 |                 gr.update(visible=False),
 134 |                 gr.update(value=""),
 135 |                 gr.update(value=""),
 136 |                 gr.update(value=""),
 137 |                 gr.update(value=""),
 138 |                 gr.update(value="Draft"),
 139 |                 gr.update(visible=False),
 140 |             ])
 141 |     return updates
 142 | 
 143 | 
 144 | async def init_mcp_stdio(mcp_config, server_names):
 145 |     try:
 146 |         for server_name in server_names:
 147 |             mcp_client = MCPClient(vllm_url, vllm_api_key)
 148 |             server_params = await load_config(mcp_config, server_name)
 149 |             try:
 150 |                 await mcp_client.connect_to_stdio_server(server_params, server_name)
 151 |                 response = await mcp_client.session.list_tools()
 152 |                 tools = []
 153 |                 for tool in response.tools:
 154 |                     try:
 155 |                         print(tool)
 156 |                         tools.append(
 157 |                             {
 158 |                             "type": "function",
 159 |                             "function": {
 160 |                                 "name": tool.name,
 161 |                                 "description": tool.description,
 162 |                                 "parameters": tool.inputSchema
 163 |                                 }
 164 |                              }
 165 |                         )
 166 |                     except Exception as e:
 167 |                         print(e)
 168 | 
 169 |                 mcp_servers.append((mcp_client, tools, server_name))
 170 |             except Exception as e:
 171 |                 print(e)
 172 | 
 173 |     except Exception as e:
 174 |         print(e)
 175 |         print("error connecting to MCP Stdio server")
 176 |     #finally:
 177 |     #    await client.cleanup()
 178 | 
 179 | 
 180 | def show_image(img):
 181 |     return img
 182 | 
 183 | def transcribe_whisper(file_path):
 184 |     from faster_whisper import WhisperModel
 185 | 
 186 |     model_size = "base"
 187 | 
 188 |     # Run on GPU with FP16
 189 |     # model = WhisperModel(model_size, device="cuda", compute_type="float16")
 190 | 
 191 |     # or run on GPU with INT8
 192 |     # model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
 193 |     # or run on CPU with INT8
 194 |     whisper_model = WhisperModel(model_size, device="cpu", compute_type="int8")
 195 | 
 196 |     segments, info = whisper_model.transcribe(file_path, beam_size=5)
 197 | 
 198 |     print("Detected language '%s' with probability %f" % (
 199 |         info.language, info.language_probability))
 200 |     message = ""
 201 |     for segment in segments:
 202 |         print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
 203 |         message += segment.text + "\n"
 204 | 
 205 |     return message.rstrip("\n")
 206 | 
 207 | async def create_interface():
 208 |     theme = gr.themes.Default(primary_hue="blue").set(
 209 |         loader_color="#FF0000",
 210 |         slider_color="#FF0000",
 211 |     )
 212 |     with (gr.Blocks(theme="ocean",
 213 |                    title="PrivateGPT MCP Multi-API Demo",
 214 |                    fill_height=True,
 215 |                    #css="footer{display:none !important}"
 216 |                    css="footer {visibility: hidden}"
 217 |                     )
 218 |           as demo):
 219 |         # Login UI Elements
 220 |         login_message = gr.Markdown("")
 221 | 
 222 |         await init_mcp_stdio(mcp_config=mcp_config, server_names=server_names)
 223 | 
 224 | 
 225 | 
 226 |         with gr.Group() as login_interface:
 227 |             # Store/Save credentials in browser
 228 |             get_local_storage = """
 229 |                 function() {
 230 |                   globalThis.setStorage = (key, value)=>{
 231 |                     localStorage.setItem(key, JSON.stringify(value))
 232 |                   }
 233 |                    globalThis.getStorage = (key, value)=>{
 234 |                     return JSON.parse(localStorage.getItem(key))
 235 |                   }
 236 |                    const username_input =  getStorage('login')
 237 |                    const password_input =  getStorage('password')
 238 |                    return [username_input, password_input];
 239 |                   }
 240 |                 """
 241 | 
 242 |             with gr.Row():
 243 |                 gr.Image(value="./clients/Gradio/logos/Logo_dark.svg", show_label=False,
 244 |                          show_download_button=False,
 245 |                          show_fullscreen_button=False, height=300, width=200, scale=1)
 246 |                 gr.Image(value="./clients/Gradio/logos/fsas.png", show_label=False,
 247 |                          show_download_button=False,
 248 |                          show_fullscreen_button=False, height=300, scale=3)
 249 |             username_input = gr.Textbox(label="Username")
 250 |             username_input.change(None, username_input, None, js="(v)=>{ setStorage('login',v) }")
 251 |             password_input = gr.Textbox(label="Password", type="password")
 252 |             password_input.change(None, password_input, None, js="(v)=>{ setStorage('password',v) }")
 253 | 
 254 |             login_button = gr.Button("Login")
 255 | 
 256 |             with gr.Blocks() as vl:
 257 |                 vl.load(
 258 |                     None,
 259 |                     inputs=None,
 260 |                     outputs=[username_input, password_input],
 261 |                     js=get_local_storage,
 262 |                 )
 263 | 
 264 | 
 265 |             saved_message = gr.Markdown("✅ Saved to local storage", visible=False)
 266 | 
 267 |         # Dashboard UI Elements
 268 |         with gr.Group(visible=False) as dashboard_interface:
 269 | 
 270 |             with gr.Blocks() as main:
 271 |                 with gr.Tab("Chat"):
 272 |                     async def predict(message, history):
 273 |                         global selected_groups
 274 |                         global mcp_servers
 275 |                         global temperature
 276 |                         global top_p
 277 |                         global model
 278 |                         global md_model
 279 |                         global image_url
 280 | 
 281 |                         files = []
 282 |                         # deal with multimodal textfield
 283 |                         try:
 284 |                             files = message["files"]
 285 |                             message = str(message["text"])
 286 |                         except:
 287 |                             print("using regular message")
 288 | 
 289 |                         image_url = None
 290 |                         if len(files) > 0:
 291 |                             for file_path in files:
 292 |                                 print(file_path)
 293 |                                 # Get the file extension
 294 |                                 file_extension = os.path.splitext(file_path)[1]
 295 |                                 print(f"File Extension: {file_extension}")
 296 | 
 297 |                                 if file_extension == ".wav":
 298 |                                    message = transcribe_whisper(file_path)
 299 | 
 300 |                                 elif file_extension == ".jpg" or file_extension == ".jpeg" or file_extension == ".png" or file_extension == ".bmp":
 301 |                                     image_url = file_path
 302 | 
 303 |                                 else:
 304 | 
 305 |                                     content = ""
 306 |                                     if file_extension == ".pdf":
 307 |                                         content = LoadersFactory().pdf(file_path)
 308 |                                     elif file_extension == ".csv":
 309 |                                         content = LoadersFactory().csv(file_path)
 310 |                                     elif file_extension == ".xlsx":
 311 |                                         content = LoadersFactory().xlsx(file_path)
 312 |                                     elif file_extension == ".md":
 313 |                                         content = LoadersFactory().markdown(file_path)
 314 |                                     # todo add more sources
 315 | 
 316 |                                     markdown = LoadersFactory().convert_documents_to_markdown(content)
 317 |                                     print(markdown)
 318 |                                     message += "\n\n" + markdown
 319 | 
 320 | 
 321 |                         history_openai_format = []
 322 |                         tools = []
 323 |                         file_extension = ""
 324 |                         if len(files) > 0:
 325 |                             file_extension = os.path.splitext(files[0])[1]
 326 |                         # only add mcp servers when we don't have a file attached for now.
 327 |                         if len(files) == 0  or len(files) == 1 and (file_extension == ".wav" or  file_extension == ".jpg" or file_extension == ".jpeg" or file_extension == ".png" or file_extension == ".bmp"):
 328 |                             for mcp_server, mcptools, mcpname in mcp_servers:
 329 |                                 for tool in mcptools:
 330 |                                     tools.append(tool)
 331 | 
 332 | 
 333 | 
 334 | 
 335 |                         if len(selected_groups) == 0:
 336 |                             # If we don't use a group, we use vllm directly.
 337 | 
 338 |                             # only make the mcp prompt when we don't have a file attached
 339 |                             if len(files) == 0 or len(files) == 1 and os.path.splitext(files[0])[1] == ".wav":
 340 |                                 system_prompt = generate_system_prompt(tools)
 341 | 
 342 |                             else:
 343 |                                 system_prompt = "You have access to a document. The user will instruct you what to do with it."
 344 | 
 345 |                             history_openai_format.append({"role": "system", "content": system_prompt})
 346 | 
 347 | 
 348 |                             last_role = "system"
 349 |                             for entry in history:
 350 |                                 if last_role != entry["role"] and not hasattr(entry, "tool_calls") or  (hasattr(entry, "tool_calls") and (entry["tool_calls"] is None  or entry["tool_calls"] == [])):
 351 |                                     history_openai_format.append({"role": entry["role"], "content": str(entry["content"])})
 352 |                                     last_role = entry["role"]
 353 | 
 354 |                             if image_url is None:
 355 |                                 history_openai_format.append({"role": "user", "content": message})
 356 |                             else:
 357 |                                 image = Image.open(image_url)
 358 |                                 # Convert the image to a byte stream
 359 |                                 buffered = io.BytesIO()
 360 |                                 image.save(buffered, format="JPEG")  # Specify the format (e.g., JPEG, PNG)
 361 |                                 image_bytes = buffered.getvalue()
 362 |                                 image_base64 = base64.b64encode(image_bytes).decode("utf-8")
 363 | 
 364 |                                 history_openai_format.append(
 365 | 
 366 |                                     {
 367 |                                         "role": "user",
 368 |                                         "content": [
 369 |                                             {"type": "text", "text": message},
 370 |                                             {
 371 |                                                 "type": "image_url",
 372 |                                                 "image_url": {
 373 |                                                     "url": f"data:image/jpeg;base64,{image_base64}"
 374 |                                                 },
 375 |                                             },
 376 |                                         ],
 377 |                                     }
 378 | 
 379 |                                 )
 380 | 
 381 |                             print(history_openai_format)
 382 |                             headers = {
 383 |                                 'Accept': 'application/json',
 384 |                                 'Content-Type': 'application/json',
 385 |                             }
 386 | 
 387 |                             if access_header is not None:
 388 |                                 headers['X-Custom-Header'] = access_header
 389 |                             elif proxy_user is not None and proxy_password is not None:
 390 |                                 auth = base64.b64encode(f"{proxy_user}:{proxy_password}".encode()).decode()
 391 |                                 headers['Authorization'] = f'Basic {auth}'
 392 | 
 393 |                             client = OpenAI(
 394 |                                 base_url=vllm_url,
 395 |                                 api_key=vllm_api_key,
 396 |                                 http_client=httpx.Client(verify=False, headers=headers)
 397 |                             )
 398 | 
 399 |                             completion = client.chat.completions.create(
 400 |                                 model=model,
 401 |                                 messages=history_openai_format,
 402 |                                 temperature=temperature,
 403 |                                 top_p=top_p,
 404 |                                 tools = tools or None,
 405 |                                 stream=False,
 406 |                             )
 407 | 
 408 |                             # Process response and handle tool calls
 409 |                             tool_results = []
 410 | 
 411 |                             result = completion.choices[0].message
 412 |                             print(result)
 413 |                             tool_calls = []
 414 | 
 415 |                             # work around the mistral llm weirdness
 416 |                             other_weird_stuff = str(result.content).lstrip().replace("[\n```", "```").replace("{\n```", "```")
 417 |                             if result.content is not None or other_weird_stuff.startswith('```'):
 418 |                                 if result.content.startswith("[TOOL_CALLS]") or other_weird_stuff.startswith("```") :
 419 |                                     print("entering TOOL_CALLS")
 420 |                                     time.ctime()  # 'Mon Oct 18 13:35:29 2010'
 421 |                                     current_timestamp = time.time()
 422 |                                     formatted_timestamp = time.strftime("%Y-%m-%d %H:%M:%S",
 423 |                                                                         time.localtime(current_timestamp))
 424 |                                     system_prompt = "The timezone is Europe/Berlin. This is the CURRENT DATE: " + formatted_timestamp
 425 | 
 426 |                                     history_openai_format = [{"role": "system", "content": system_prompt},
 427 |                                                              {"role": "user", "content": message}]
 428 | 
 429 |                                     completion = client.chat.completions.create(
 430 |                                         model=model,
 431 |                                         messages=history_openai_format,
 432 |                                         temperature=temperature,
 433 |                                         top_p=top_p,
 434 |                                         tools=tools or None,
 435 |                                         stream=False
 436 |                                     )
 437 |                                     tool_results = []
 438 | 
 439 |                                     result = completion.choices[0].message
 440 |                                     print(message)
 441 |                                     tool_calls = []
 442 | 
 443 |                             # Convert tool calls to OpenAI format
 444 |                             if hasattr(result, "tool_calls") and result.tool_calls:
 445 |                                 for tool in result.tool_calls:
 446 |                                     print(tool.function.arguments)
 447 |                                     tool_calls.append(
 448 |                                         {
 449 |                                             "id": str(uuid.uuid4()),  # Generate unique ID
 450 |                                             "type": "function",
 451 |                                             "function": {
 452 |                                                 "name": tool.function.name,
 453 |                                                 "arguments": tool.function.arguments,
 454 |                                             },
 455 |                                         }
 456 |                                     )
 457 | 
 458 |                             if tool_calls:
 459 |                                 for tool_call in tool_calls:
 460 |                                     tool_call_id = str(uuid.uuid4())
 461 |                                     if hasattr(tool_call, "id"):
 462 |                                         tool_call_id = str(tool_call.id)
 463 | 
 464 |                                     if hasattr(tool_call, "function"):
 465 |                                         print(tool_call.function)
 466 |                                         tool_name = getattr(tool_call.function, "name", "unknown tool")
 467 |                                         raw_arguments = getattr(tool_call.function, "arguments", {})
 468 | 
 469 |                                     elif isinstance(tool_call, dict) and "function" in tool_call:
 470 |                                         fn_info = tool_call["function"]
 471 |                                         tool_name = fn_info.get("name", "unknown tool")
 472 |                                         raw_arguments = fn_info.get("arguments", {})
 473 |                                     else:
 474 |                                         tool_name = "unknown tool"
 475 |                                         raw_arguments = {}
 476 | 
 477 |                                     # If raw_arguments is a string, try to parse it as JSON
 478 |                                     if isinstance(raw_arguments, str):
 479 |                                         try:
 480 |                                             raw_arguments = json.loads(raw_arguments)
 481 |                                         except json.JSONDecodeError:
 482 |                                             print("error json not valid")
 483 |                                             # If it's not valid JSON, just display as is
 484 |                                             pass
 485 | 
 486 |                                     # Now raw_arguments should be a dict or something we can pretty-print as JSON
 487 |                                     tool_args_str = json.dumps(raw_arguments, indent=2)
 488 | 
 489 |                                     tool_message =  f"```json\n{tool_args_str}\n```"
 490 |                                     print(tool_message)
 491 | 
 492 | 
 493 |                                     yield [
 494 |                                         {
 495 |                                             "role": "assistant",
 496 |                                             "content": "\n" + tool_message + "\n",
 497 |                                             "metadata": {"title": f"🛠️ Using tool {tool_name}",
 498 |                                                          "status": "pending"}
 499 |                                         },
 500 | 
 501 |                                     ]
 502 | 
 503 |                                     for mcp_server, tools, name in mcp_servers:
 504 |                                         if tool_name in str(tools): #todo: better check
 505 |                                             print(tool_name + " in tools")
 506 | 
 507 |                                             meta = await call_tool(mcp_server.name, tool_name, raw_arguments)
 508 |                                             if meta is None:
 509 |                                                 return
 510 | 
 511 |                                             content = meta.get('content', [])
 512 |                                             print("Tool " + tool_name + " reply: " + str(content))
 513 | 
 514 |                                             tool_results.append({"call": str(tool_name), "result": content})
 515 | 
 516 |                                             history_openai_format.append(
 517 |                                                 {
 518 |                                                     "role": "assistant",
 519 |                                                     "content": None,
 520 |                                                     "tool_calls": [
 521 |                                                         {
 522 |                                                             "id": tool_call_id,
 523 |                                                             "type": "function",
 524 |                                                             "function": {
 525 |                                                                 "name": tool_name,
 526 |                                                                 "arguments": json.dumps(raw_arguments)
 527 |                                                                 if isinstance(raw_arguments, dict)
 528 |                                                                 else raw_arguments,
 529 |                                                             },
 530 |                                                         }
 531 |                                                     ],
 532 |                                                 }
 533 |                                             )
 534 | 
 535 |                                             # Continue conversation with tool results
 536 |                                             if  len(content)> 0 and content[0].get("type") == "text" and content[0].get("text") is not None:
 537 | 
 538 |                                                 #temporary workaround, move to image instead of text
 539 | 
 540 |                                                 content = content[0].get("text")
 541 |                                                 isimagejson = False
 542 |                                                 isvideojson = False
 543 |                                                 j = None
 544 |                                                 try:
 545 |                                                     j = json.loads(content)
 546 |                                                     if j.get("type") == "image":
 547 |                                                         isimagejson = True
 548 |                                                     elif  j.get("type") == "video":
 549 |                                                         isvideojson = True
 550 |                                                 except:
 551 |                                                     isimagejson = False
 552 | 
 553 |                                                 if isimagejson:
 554 |                                                     yield  [
 555 |                                                         {
 556 |                                                             "role": "assistant",
 557 |                                                             "content": "\n" + tool_message + "\n" + f"Reply:\n {content}" + "\n",
 558 |                                                             "tool_calls": [tool_name],
 559 |                                                             "metadata": {"title": f"🛠️ Used tool {tool_name}",
 560 |                                                                          "status": "done"}
 561 |                                                         },
 562 |                                                         {
 563 |                                                             "role": "assistant",
 564 |                                                             "content":  f"{j.get("message")}:\n![Image Description]({j.get("url")})"
 565 |                                                         }
 566 | 
 567 | 
 568 |                                                     ]
 569 |                                                 elif isvideojson:
 570 |                                                     html_content = f"""
 571 |                                                     <!DOCTYPE html>
 572 |                                                     <html>
 573 |                                                     <head>
 574 |                                                         <title>Video Preview</title>
 575 |                                                     </head>
 576 |                                                     <body>
 577 |                                                         <video width="640" controls>
 578 |                                                             <source src="{j.get("url")}" type="video/mp4">
 579 |                                                             Your browser does not support the video tag.
 580 |                                                         </video>
 581 |                                                     </body>
 582 |                                                     </html>
 583 |                                                     """
 584 | 
 585 | 
 586 | 
 587 |                                                     yield  [
 588 |                                                         {
 589 |                                                             "role": "assistant",
 590 |                                                             "content": "\n" + tool_message + "\n" + f"Reply:\n {content}" + "\n",
 591 |                                                             "tool_calls": [tool_name],
 592 |                                                             "metadata": {"title": f"🛠️ Used tool {tool_name}",
 593 |                                                                          "status": "done"}
 594 |                                                         },
 595 |                                                         {
 596 |                                                             "role": "assistant",
 597 |                                                             "content":  html_content
 598 |                                                         }
 599 | 
 600 | 
 601 |                                                     ]
 602 | 
 603 |                                                 else:
 604 | 
 605 |                                                     history_openai_format.append(
 606 |                                                         {
 607 |                                                             "role": "tool",
 608 |                                                             "name": tool_name,
 609 |                                                             "content": content,
 610 |                                                             "tool_call_id": tool_call_id,
 611 |                                                         }
 612 |                                                     )
 613 | 
 614 |                                                     response = client.chat.completions.create(
 615 |                                                         model=model,
 616 |                                                         messages=history_openai_format,
 617 |                                                         temperature=temperature,
 618 |                                                         top_p=top_p,
 619 |                                                         stream=False
 620 |                                                     )
 621 | 
 622 |                                                     partial_mes = ""
 623 |                                                     #history.append({"role": "assistant", "content": "I called a tool",
 624 |                                                     #                     "metadata": {"title": f"🛠️ Used tool {"Test"}"}})
 625 |                                                     tokens = clean_response(response.choices[0].message.content).split(" ")
 626 |                                                     for i, token in enumerate(tokens):
 627 |                                                         partial_mes = partial_mes + token + " "
 628 |                                                         await asyncio.sleep(0.05)
 629 |                                                         yield partial_mes
 630 |                                                     #history.append({"role": "assistant", "content": clean_response(response.choices[0].message.content)})
 631 | 
 632 |                                                     yield [
 633 |                                                             {
 634 |                                                                 "role": "assistant",
 635 |                                                                 "content": "\n" + tool_message + "\n" + f"Reply:\n {content}" + "\n",
 636 |                                                                 "tool_calls": [tool_name],
 637 |                                                                 "metadata": {"title": f"🛠️ Used tool {tool_name}",
 638 |                                                                              "status": "done"}
 639 |                                                             },
 640 |                                                             {
 641 |                                                                 "role": "assistant",
 642 |                                                                 "content": clean_response(response.choices[0].message.content)
 643 |                                                             },
 644 | 
 645 | 
 646 |                                                         ]
 647 | 
 648 |                                                     break
 649 |                                             elif len(content)> 0 and content[0].get("type") == "image":
 650 |                                                 try:
 651 |                                                     base64_string = content[0].get("data")
 652 |                                                     image_bytes = base64.b64decode(base64_string)
 653 | 
 654 |                                                     from PIL import Image as PilImage
 655 |                                                     pil_image = PilImage.open(io.BytesIO(image_bytes))
 656 | 
 657 |                                                     pil_image.save("test.jpg")
 658 |                                                     fullpath = Path("test.jpg").absolute()
 659 |                                                     print(fullpath)
 660 | 
 661 | 
 662 |                                                     yield [ {
 663 |                                                             "role": "assistant",
 664 |                                                             "content": "\n" + tool_message + "\n",
 665 |                                                             "tool_calls": [tool_name],
 666 |                                                             "metadata": {"title": f"🛠️ Used tool {tool_name}",
 667 |                                                                          "status": "done"}
 668 |                                                         },
 669 |                                                         {
 670 |                                                         "text": content[0].get("message"),
 671 |                                                         "files" : [fullpath]
 672 |                                                         }
 673 | 
 674 |                                                     ]
 675 | 
 676 |                                                 except Exception as e:
 677 |                                                     print(e)
 678 |                                                     yield [
 679 |                                                         {
 680 |                                                             "role": "assistant",
 681 |                                                             "content": "\n" + tool_message + "\n" + f"Reply:\n {content}" + "\n",
 682 |                                                             "tool_calls": [tool_name],
 683 |                                                             "metadata": {"title": f"🛠️ Used tool {tool_name}",
 684 |                                                                          "status": "done"}
 685 |                                                         },
 686 |                                                         {
 687 |                                                             "role": "assistant",
 688 |                                                             "content": "Error receiving an image"
 689 |                                                         }
 690 |                                                     ]
 691 | 
 692 |                             else:
 693 |                                 partial_mes = ""
 694 |                                 tokens = clean_response(result.content).split(" ")
 695 |                                 for i, token in enumerate(tokens):
 696 |                                     partial_mes = partial_mes + token + " "
 697 |                                     await asyncio.sleep(0.05)
 698 |                                     yield partial_mes
 699 | 
 700 | 
 701 |                         else:
 702 |                             # if at least one group is seleceted we use the api code to use the rag.
 703 | 
 704 |                             last_role = "system"
 705 |                             for entry in history:
 706 |                                 if last_role != entry["role"]:
 707 |                                     history_openai_format.append({"role": entry["role"], "content": entry["content"]})
 708 |                                     last_role = entry["role"]
 709 | 
 710 |                             history_openai_format.append({"role": "user", "content": message})
 711 | 
 712 | 
 713 |                             config.set_value("groups", selected_groups)
 714 |                             pgpt = PrivateGPTAPI(config)
 715 |                             response = pgpt.respond_with_context(history_openai_format)
 716 |                             print(response)
 717 |                             user_input = ""
 718 |                             for message in history_openai_format:
 719 |                                 user_input += json.dumps(message)
 720 | 
 721 |                             tokens = response["answer"].split(" ")
 722 |                             partial_message = ""
 723 | 
 724 |                             for i, token in enumerate(tokens):
 725 |                                 partial_message = partial_message + token + " "
 726 |                                 await asyncio.sleep(0.05)
 727 |                                 yield partial_message
 728 | 
 729 |                             citations = []
 730 |                             for source in response["sources"]:
 731 |                                 document_info = pgpt.get_document_info(source["documentId"])
 732 | 
 733 |                                 citations.append(document_info["title"] +
 734 |                                                  #" Page: " + str(source["page"] + 1) +
 735 |                                                  "\n" + str(source["context"]).replace("#", "") + "\n\n")
 736 |                             result = [{"role": "assistant",
 737 |                                     "content": response["answer"]
 738 |                                     }
 739 |                                  ]
 740 |                             if len(citations) > 0:
 741 |                                 result.append({
 742 |                                     "role": "user",
 743 |                                     "content": " "
 744 |                                 })
 745 |                                 result.append( {
 746 |                                        "role": "assistant",
 747 |                                        "content": "\n".join([f"• {cite}" for cite in citations]),
 748 |                                        "metadata": {"title": "📚 Citations",
 749 |                                                     "status": "done"}
 750 |                                    })
 751 | 
 752 |                             yield result
 753 | 
 754 |                     async def call_tool(mcp_server, tool_name, tool_args) -> json:
 755 |                         print("starting to call the tool")
 756 | 
 757 |                         tool_response = None
 758 |                         try:
 759 |                             server_params = await load_config(mcp_config, mcp_server)
 760 |                             try:
 761 |                                 async with stdio_client(server_params) as (read_stream, write_stream):
 762 |                                     # Check if our current config has a tool.
 763 | 
 764 |                                     init_result = await send_initialize(read_stream, write_stream)
 765 |                                     # check we got a result
 766 |                                     if not init_result:
 767 |                                         print("Server initialization failed")
 768 |                                         return
 769 | 
 770 |                                     tools = await send_tools_list(read_stream, write_stream)
 771 |                                     stuff = json.dumps(tools)
 772 |                                     toolsobject = json.loads(stuff)["tools"]
 773 |                                     print(toolsobject)
 774 | 
 775 |                                     server_has_tool = False
 776 |                                     for tool in toolsobject:
 777 |                                         if tool["name"] == tool_name:
 778 |                                             print(f"Found tool {tool_name}.")
 779 |                                             server_has_tool = True
 780 |                                     if server_has_tool is False:
 781 |                                         print("no tool in server")
 782 |                                     else:
 783 |                                         print(tool_args)
 784 |                                         tool_response = await send_call_tool(
 785 |                                             tool_name, tool_args, read_stream, write_stream)
 786 |                                         raise BaseException()  # Until we find a better way to leave the async with
 787 | 
 788 |                             except:
 789 |                                 raise BaseException()
 790 | 
 791 |                             raise BaseException()
 792 |                         except BaseException as e:
 793 |                             pass
 794 | 
 795 |                         return tool_response
 796 | 
 797 |                     def change_group(selected_item):
 798 |                         global selected_groups
 799 |                         selected_groups = selected_item
 800 | 
 801 |                     groupslist = gr.CheckboxGroup(choices=[], label="Groups")
 802 |                     groupslist.change(change_group, groupslist, None)
 803 | 
 804 |                     chatbot = gr.Chatbot(
 805 |                                         height="60vh",
 806 |                                         show_label=False,
 807 |                                         type="messages",
 808 |                                         avatar_images=(
 809 |                                               None,
 810 |                                               "./clients/Gradio/logos/Logo_dark.svg"
 811 |                                           ),
 812 |                                          )
 813 | 
 814 |                     gr.ChatInterface(predict,
 815 |                                      chatbot=chatbot,
 816 |                                      type="messages",
 817 |                                      textbox=gr.MultimodalTextbox(placeholder="Ask me a question", autofocus=True, container=True, scale=7, sources=["upload", "microphone"]),
 818 |                                      examples=["Hello", "Write a Python function that counts all numbers from 1 to 10",
 819 |                                                "How is the weather today in Munich?"],
 820 |                                      cache_examples=False
 821 | 
 822 | 
 823 |                     )
 824 |                     with gr.Row():
 825 |                         #prompt_dd = gr.Dropdown(choices=prompt_dict)
 826 |                         show_btn = gr.Button("Chat Settings")
 827 |                         show_btn2 = gr.Button("MCP Tools")
 828 |                 with gr.Tab("Sources"):
 829 | 
 830 | 
 831 |                     def upload_file(file, sources):
 832 |                         global pgpt
 833 |                         global selected_groups
 834 |                         global default_groups
 835 |                         UPLOAD_FOLDER = "./data"
 836 | 
 837 |                         if len(selected_groups) == 0:
 838 |                             gr.Warning("Select at least one group, source was not added")
 839 |                             return sources
 840 | 
 841 |                         if not os.path.exists(UPLOAD_FOLDER):
 842 |                             os.mkdir(UPLOAD_FOLDER)
 843 |                         shutil.copy(file, UPLOAD_FOLDER)
 844 |                         print()
 845 |                         file_path = os.path.join(UPLOAD_FOLDER, file)
 846 | 
 847 | 
 848 |                         file_extension = os.path.splitext(file_path)[1]
 849 |                         print(f"File Extension: {file_extension}")
 850 | 
 851 | 
 852 |                         if file_extension == ".wav":
 853 |                             markdown = transcribe_whisper(file_path)
 854 | 
 855 |                         else:
 856 |                             content = ""
 857 |                             if file_extension == ".pdf":
 858 |                                 content = LoadersFactory().pdf(file_path)
 859 |                             elif file_extension == ".csv":
 860 |                                 content = LoadersFactory().csv(file_path)
 861 |                             elif file_extension == ".xlsx":
 862 |                                 content = LoadersFactory().xlsx(file_path)
 863 |                             elif file_extension == ".md":
 864 |                                 content = LoadersFactory().markdown(file_path)
 865 |                             # todo add more sources
 866 | 
 867 |                             markdown = LoadersFactory().convert_documents_to_markdown(content)
 868 |                             print(markdown)
 869 | 
 870 |                             
 871 |                         if os.path.exists(file_path):
 872 |                             os.remove(file_path)
 873 |                             print("File deleted successfully.")
 874 |                         else:
 875 |                             print("File does not exist.")
 876 | 
 877 | 
 878 |                         gr.Info("Processing, please wait...")
 879 | 
 880 | 
 881 |                         if pgpt is not None:
 882 |                             print(pgpt.base_url)
 883 |                             filepath = Path(file_path)
 884 |                             file_name = filepath.name
 885 |                             answer =  pgpt.add_source(markdown, selected_groups, file_name)
 886 |                             print(str(answer["documentId"]))
 887 |                             document_info = pgpt.get_document_info(answer["documentId"])
 888 |                             #gr.Info("Added:" + str(document_info))
 889 |                             dt = datetime.fromisoformat(document_info["createdAt"])
 890 |                             # Format to human-readable string
 891 |                             human_readable = dt.strftime("%A, %B %d, %Y  %I:%M %p %Z")
 892 | 
 893 |                             if len(sources) < MAX_ITEMS:
 894 |                                 sources.append({"name": document_info["title"], "creator": document_info["creator"]["name"], "date": human_readable, "status": document_info["state"], "groups": document_info["groups"], "id": document_info["sourceId"]})
 895 |                             return sources
 896 | 
 897 | 
 898 | 
 899 | 
 900 |                     gr.Markdown("## 📚 PrivateGPT Sources")
 901 | 
 902 |                     groupslist2 = gr.CheckboxGroup(choices=[], label="Groups")
 903 |                     groupslist2.change(change_group, groupslist2, None)
 904 | 
 905 |                     upload_button = gr.UploadButton("➕ Add Source")
 906 | 
 907 | 
 908 | 
 909 |                     MAX_ITEMS = 200  # Max number of sources
 910 | 
 911 | 
 912 |                     sources_state = gr.State([])  # Start with an empty state
 913 | 
 914 |                     rows = []
 915 | 
 916 |                     # Create rows for each source
 917 |                     for i in range(MAX_ITEMS):
 918 |                         with gr.Row(visible=False) as row:  # Initially invisible
 919 |                             name = gr.Text(value="", label="Name", interactive=False, show_label=False)  # Read-only field
 920 |                             groups = gr.Text(value="", label="Groups", interactive=False,
 921 |                                               show_label=False)  # Read-only field
 922 |                             creator = gr.Text(value="", label="Creator", interactive=False,  show_label=False)  # Read-only field
 923 |                             date = gr.Text(value="", label="Date", interactive=False,  show_label=False)  # Read-only field
 924 |                             status = gr.Text(value="", label="Status",  show_label=False)  # Label for status
 925 |                             delete_btn = gr.Button("🗑️", scale=0)
 926 |                             rows.append((row, name, groups, creator, date, status, delete_btn))
 927 | 
 928 |                             # Delete handler
 929 |                             delete_btn.click(delete_source, inputs=[sources_state, gr.State(i)],
 930 |                                              outputs=[sources_state])
 931 | 
 932 |                     # Add source button
 933 |                     #upload_button.click(add_source, inputs=[sources_state], outputs=[sources_state])
 934 |                     upload_button.upload(upload_file, inputs=[upload_button, sources_state], outputs=sources_state)
 935 | 
 936 |                     # Auto re-render UI when sources change
 937 |                     sources_state.change(
 938 |                         render_ui,
 939 |                         inputs=[sources_state],
 940 |                         outputs=[comp for row in rows for comp in row]
 941 |                     )
 942 | 
 943 |                     # Fetch sources from "API" and initialize the UI with them
 944 |                     def load_sources():
 945 |                         #todo that's ugly.
 946 |                         while pgpt is None:
 947 |                             time.sleep(2)
 948 | 
 949 |                         if not pgpt.logged_in:
 950 |                             return
 951 | 
 952 | 
 953 |                         groups = pgpt.list_personal_groups()
 954 |                         print(groups)
 955 | 
 956 |                         sources = []
 957 |                         for group in groups:
 958 |                             group_sources = pgpt.get_sources_from_group(group)
 959 |                             for entry in group_sources:
 960 |                                 sources.append(entry)
 961 | 
 962 |                         final = []
 963 |                         for source in sources:
 964 |                             print(source)
 965 |                             dt = datetime.fromisoformat(source["createdAt"])
 966 |                             # Format to human-readable string
 967 |                             human_readable = dt.strftime("%A, %B %d, %Y  %I:%M %p %Z")
 968 |                             final.append({"name": source["title"], "creator": source["creator"]["name"], "date": human_readable, "status": source["state"], "groups": source["groups"], "id": source["sourceId"]})
 969 | 
 970 |                         return final
 971 | 
 972 |                     #load_data()
 973 | 
 974 |                     # Trigger data fetching and rendering on app load
 975 | 
 976 |                     main.load(load_sources, outputs=[sources_state])
 977 | 
 978 |                 with Modal(visible=False) as modalsettings:
 979 |                     global temperature
 980 |                     global top_p
 981 | 
 982 |                     def change_temperature(value):
 983 |                         global temperature
 984 |                         try:
 985 |                             val = float(value)
 986 |                             if isinstance(val, float):
 987 |                                 if 0.0 <= val <= 1.0:
 988 |                                     temperature = float(value)
 989 |                                     success_message = gr.Success("New Temperature saved")
 990 |                         except:
 991 |                             error_message = gr.Warning("Not a valid entry")
 992 | 
 993 |                     def change_top_p(value):
 994 |                         global top_p
 995 |                         try:
 996 |                             val = float(value)
 997 |                             if isinstance(val, float):
 998 |                                 if 0.0 <= val <= 1.0:
 999 |                                     top_p = float(value)
1000 |                                     success_message = gr.Success("New top_p value saved")
1001 |                         except:
1002 |                             error_message = gr.Warning("Not a valid entry")
1003 | 
1004 | 
1005 | 
1006 |                     temperature_input = gr.Textbox(label="Temperature", placeholder=str(temperature))
1007 |                     temperature_input.change(change_temperature, temperature_input)
1008 | 
1009 |                     top_p_input = gr.Textbox(label="Top_p", placeholder=str(top_p))
1010 |                     top_p_input.change(change_top_p, top_p_input)
1011 | 
1012 |                 with Modal(visible=False) as modalmcp:
1013 |                     global mcp_servers
1014 | 
1015 |                     gr.Markdown("## 🛠️ Available MCP Tools")
1016 | 
1017 |                     for mcp_server in mcp_servers:
1018 |                         descr = ""
1019 |                         lines = 1
1020 |                         for tool in mcp_server[1]:
1021 |                             descr += "Tool: " + tool["function"]["name"] + "\n" + "Description: " + tool["function"]["description"] + "\n\n"
1022 |                             lines+=3
1023 | 
1024 | 
1025 |                         gr.Textbox(descr, show_label=True, label=mcp_server[2], lines=lines)
1026 | 
1027 | 
1028 |                 show_btn.click(lambda: Modal(visible=True), None, modalsettings)
1029 |                 show_btn2.click(lambda: Modal(visible=True), None, modalmcp)
1030 |                 # todo add management of sources, users etc later.
1031 | 
1032 | 
1033 |             #with gr.Tab("Users"):
1034 |                 # Initial data source
1035 |             #    gr.Markdown("Test function, not working.")
1036 |                 # TODO Api.
1037 | 
1038 | 
1039 |         # Connect button to function and update components accordingly
1040 |         login_button.click(
1041 |             fn=login,
1042 |             inputs=[username_input, password_input, groupslist, groupslist2],
1043 |             outputs=[login_interface, dashboard_interface, login_message, groupslist, groupslist2]
1044 |         )
1045 | 
1046 |     demo.launch(favicon_path="./clients/Gradio/favicon.ico")
1047 | 
1048 | 
1049 | asyncio.run(create_interface())
1050 | 
```
Page 17/20FirstPrevNextLast