ApiLogicServer 15.0.9__py3-none-any.whl → 15.0.10__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- api_logic_server_cli/api_logic_server.py +2 -3
- api_logic_server_cli/api_logic_server_info.yaml +3 -3
- api_logic_server_cli/manager.py +1 -0
- api_logic_server_cli/prototypes/base/integration/mcp/mcp_client_executor.py +471 -133
- {apilogicserver-15.0.9.dist-info → apilogicserver-15.0.10.dist-info}/METADATA +1 -1
- {apilogicserver-15.0.9.dist-info → apilogicserver-15.0.10.dist-info}/RECORD +10 -10
- {apilogicserver-15.0.9.dist-info → apilogicserver-15.0.10.dist-info}/WHEEL +0 -0
- {apilogicserver-15.0.9.dist-info → apilogicserver-15.0.10.dist-info}/entry_points.txt +0 -0
- {apilogicserver-15.0.9.dist-info → apilogicserver-15.0.10.dist-info}/licenses/LICENSE +0 -0
- {apilogicserver-15.0.9.dist-info → apilogicserver-15.0.10.dist-info}/top_level.txt +0 -0
|
@@ -12,11 +12,10 @@ ApiLogicServer CLI: given a database url, create [and run] customizable ApiLogic
|
|
|
12
12
|
Called from api_logic_server_cli.py, by instantiating the ProjectRun object.
|
|
13
13
|
'''
|
|
14
14
|
|
|
15
|
-
__version__ = "15.00.
|
|
15
|
+
__version__ = "15.00.10" # last public release: 15.00.10 (15.00.00)
|
|
16
16
|
recent_changes = \
|
|
17
17
|
f'\n\nRecent Changes:\n' +\
|
|
18
|
-
"\t06/08/2024 - 15.00.
|
|
19
|
-
"\t05/28/2024 - 15.00.00: MCP \n"\
|
|
18
|
+
"\t06/08/2024 - 15.00.10: MCP, optional shortening of stacktrace lines, bugfix[92] \n"\
|
|
20
19
|
"\t05/16/2024 - 14.05.00: safrs 3.1.7, running mcp preview \n"\
|
|
21
20
|
"\t04/27/2024 - 14.04.00: Graphics preview, Vibe install fix, Improved IDE Chat Logic, MCP Exploration \n"\
|
|
22
21
|
"\t03/30/2024 - 14.03.25: WebGenAI fixes for Kafka and Keycloak \n"\
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
last_created_date: June
|
|
2
|
-
last_created_project_name:
|
|
3
|
-
last_created_version: 15.00.
|
|
1
|
+
last_created_date: June 08, 2025 16:06:20
|
|
2
|
+
last_created_project_name: samples/nw_sample_nocust
|
|
3
|
+
last_created_version: 15.00.09
|
api_logic_server_cli/manager.py
CHANGED
|
@@ -19,6 +19,7 @@ def create_manager(clean: bool, open_with: str, api_logic_server_path: Path,
|
|
|
19
19
|
1. .vscode, readme
|
|
20
20
|
2. System folder (GenAI sample prompts / responses, others TBD)
|
|
21
21
|
3. pre-created samples (optional)
|
|
22
|
+
4. readme (from docs: Sample-Basic-Tour.md)
|
|
22
23
|
|
|
23
24
|
Example, from CLI in directory containing a `venv` (see https://apilogicserver.github.io/Docs/Manager/):
|
|
24
25
|
als start
|
|
@@ -1,150 +1,488 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
#
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
start_up_message = "normal start"
|
|
29
|
-
|
|
30
|
-
import os, logging, logging.config, sys, yaml # failure here means venv probably not set
|
|
31
|
-
from flask_sqlalchemy import SQLAlchemy
|
|
32
|
-
import json
|
|
1
|
+
"""
|
|
2
|
+
A basic MCP Client Executor: takes a natural language query and:
|
|
3
|
+
1. Discovers MCP servers (from mcp_server_discovery.json)
|
|
4
|
+
2. Queries OpenAI's GPT-4 model to obtain the tool context, based on a provided schema and a natural language query
|
|
5
|
+
3. Processes the tool context (calls the indicated MCP (als) endpoints)
|
|
6
|
+
|
|
7
|
+
To run:
|
|
8
|
+
1. Start the server (F5), and:
|
|
9
|
+
2. Run this:
|
|
10
|
+
1. Terminal: python integration/mcp/mcp_client_executor.py
|
|
11
|
+
2. Or, if you have installed SysMcp:
|
|
12
|
+
1. curl -X 'POST' 'http://localhost:5656/api/SysMcp/' -H 'accept: application/vnd.api+json' -H 'Content-Type: application/json' -d '{ "data": { "attributes": {"request": "List the orders date_shipped is null and CreatedOn before 2023-07-14, and send a discount email (subject: '\''Discount Offer'\'') to the customer for each one."}, "type": "SysMcp"}}'
|
|
13
|
+
2. Or, open the Admin App:
|
|
14
|
+
* List the orders date_shipped is null and CreatedOn before 2023-07-14, and send a discount email (subject: 'Discount Offer') to the customer for each one.
|
|
15
|
+
|
|
16
|
+
See: https://apilogicserver.github.io/Docs/Integration-MCP/
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
################
|
|
20
|
+
# debug settings
|
|
21
|
+
################
|
|
22
|
+
|
|
23
|
+
create_tool_context_from_llm = False
|
|
24
|
+
''' set to False to bypass LLM call and save 2-3 secs in testing, no API Key required. '''
|
|
25
|
+
|
|
26
|
+
import os, logging, logging.config, sys
|
|
33
27
|
from pathlib import Path
|
|
34
|
-
from
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
declare_logic_message = ""
|
|
48
|
-
declare_security_message = "ALERT: *** Security Not Enabled ***"
|
|
49
|
-
|
|
50
|
-
os.chdir(project_dir) # so admin app can find images, code
|
|
51
|
-
import api.system.api_utils as api_utils
|
|
52
|
-
logic_logger_activate_debug = False
|
|
53
|
-
""" True prints all rules on startup """
|
|
54
|
-
|
|
55
|
-
from typing import TypedDict
|
|
56
|
-
import safrs # fails without venv - see https://apilogicserver.github.io/Docs/Project-Env/
|
|
57
|
-
from safrs import ValidationError, SAFRSBase, SAFRSAPI as _SAFRSAPI
|
|
58
|
-
from logic_bank.logic_bank import LogicBank
|
|
28
|
+
from typing import Dict, List
|
|
29
|
+
import yaml
|
|
30
|
+
|
|
31
|
+
mcp_path = Path(os.path.abspath(os.path.dirname(__file__)))
|
|
32
|
+
project_path = mcp_path.parent.parent
|
|
33
|
+
sys.path.append(str(project_path)) # add project root to sys.path
|
|
34
|
+
|
|
35
|
+
import re
|
|
36
|
+
import json
|
|
37
|
+
from openai import OpenAIError
|
|
38
|
+
import openai
|
|
39
|
+
import requests
|
|
40
|
+
from logic_bank.logic_bank import Rule
|
|
59
41
|
from logic_bank.exec_row_logic.logic_row import LogicRow
|
|
60
|
-
from
|
|
61
|
-
from
|
|
62
|
-
from sqlalchemy.orm import Session
|
|
63
|
-
import socket
|
|
64
|
-
import warnings
|
|
65
|
-
from flask import Flask, redirect, send_from_directory, send_file
|
|
66
|
-
from flask_cors import CORS
|
|
67
|
-
import ui.admin.admin_loader as AdminLoader
|
|
68
|
-
from security.system.authentication import configure_auth
|
|
69
|
-
import oracledb
|
|
42
|
+
from database import models
|
|
43
|
+
from logic_bank.util import ConstraintException
|
|
70
44
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
else:
|
|
74
|
-
app_logger = server_setup.logging_setup()
|
|
45
|
+
# Set your OpenAI API key
|
|
46
|
+
openai.api_key = os.getenv("APILOGICSERVER_CHATGPT_APIKEY")
|
|
75
47
|
|
|
48
|
+
log = logging.getLogger('integration.mcp')
|
|
76
49
|
|
|
50
|
+
default_query = "List customers with credit_limit > 1000."
|
|
51
|
+
default_query_email = "List the orders date_shipped is null and CreatedOn before 2023-07-14, and send a discount email (subject: 'Discount Offer') to the customer for each one."
|
|
77
52
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
53
|
+
def discover_mcp_servers() -> str:
|
|
54
|
+
"""Discover MCP servers (aka 'tools'), and retrieve their API learnings and schemas.
|
|
55
|
+
This function performs the following steps:
|
|
56
|
+
1. Reads a configuration file (`integration/mcp/mcp_server_discovery.json`) to obtain a list of available MCP servers.
|
|
57
|
+
2. For each server, calls its `schema_url` endpoint to retrieve the MCP learnings_and_schema.
|
|
58
|
+
See: .well-known/mcp.json (see api/api_discovery/mcp_discovery.py)
|
|
59
|
+
3. Logs the discovered servers and their schemas for informational purposes.
|
|
81
60
|
|
|
82
|
-
|
|
61
|
+
Raises:
|
|
62
|
+
FileNotFoundError: If the discovery configuration file is not found.
|
|
63
|
+
json.JSONDecodeError: If the configuration file contains invalid JSON.
|
|
64
|
+
requests.RequestException: If there is an error making HTTP requests to the schema URLs.
|
|
83
65
|
|
|
84
|
-
|
|
85
|
-
|
|
66
|
+
Returns:
|
|
67
|
+
learnings_and_schema: str
|
|
68
|
+
"""
|
|
86
69
|
|
|
87
|
-
|
|
70
|
+
discovery_file_path = os.path.join(os.path.dirname(__file__), "../../integration/mcp/mcp_server_discovery.json")
|
|
71
|
+
try:
|
|
72
|
+
with open(discovery_file_path, "r") as discovery_file:
|
|
73
|
+
discovery_data = json.load(discovery_file)
|
|
74
|
+
log.info(f"\n1. Discovered MCP servers from config file: {discovery_file_path}:" + json.dumps(discovery_data, indent=4))
|
|
75
|
+
except FileNotFoundError:
|
|
76
|
+
log.info(f"Discovery file not found at {discovery_file_path}.")
|
|
77
|
+
except json.JSONDecodeError as e:
|
|
78
|
+
log.info(f"Error decoding JSON from {discovery_file_path}: {e}")
|
|
79
|
+
|
|
80
|
+
api_schema = {} # initialize api_schema to an empty dict
|
|
81
|
+
for each_server in discovery_data["servers"]:
|
|
82
|
+
discovery_url = each_server["schema_url"]
|
|
88
83
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
84
|
+
# Call the discovery_url to get the MCP/API schema
|
|
85
|
+
try:
|
|
86
|
+
response = requests.get(discovery_url)
|
|
87
|
+
if response.status_code == 200:
|
|
88
|
+
each_schema = response.json()
|
|
89
|
+
api_schema[discovery_url] = each_schema
|
|
90
|
+
if format_for_print := False:
|
|
91
|
+
each_schema["learning"] = each_schema['learning'].split('\n') # split learning into a list of lines
|
|
92
|
+
request_print = json.dumps(each_schema, indent=4)[0:1200] # limit for readability
|
|
93
|
+
request_print_schema = json.dumps(each_schema.get("resources", {}), indent=4)[0:200] + '\n... etc'
|
|
94
|
+
log.info(f"\n\nLearnings and Schema from discovery schema_url: {discovery_url}:\n" + request_print)
|
|
95
|
+
log.info(f' "resources":\n' + request_print_schema)
|
|
96
|
+
else:
|
|
97
|
+
log.info(f"Failed to retrieve API schema from {discovery_url}: {response.status_code}")
|
|
98
|
+
except requests.RequestException as e:
|
|
99
|
+
log.info(f"Error calling OpenAPI URL: {e}")
|
|
100
|
+
pass
|
|
101
|
+
debug_print = json.dumps(api_schema, indent=4)
|
|
102
|
+
return json.dumps(api_schema)
|
|
92
103
|
|
|
93
|
-
args.get_cli_args(dunder_name=__name__, args=args)
|
|
94
|
-
app_logger.debug(f"\nCLI args: \n{args}") # api_logic_server_run cl args
|
|
95
104
|
|
|
96
|
-
flask_app.config.from_prefixed_env(prefix="APILOGICPROJECT") # env overrides (e.g., docker)
|
|
97
|
-
app_logger.debug(f"\nENV args: \n{args}\n\n")
|
|
98
105
|
|
|
99
|
-
|
|
106
|
+
def query_llm_with_nl(learnings_and_schema: str, nl_query: str):
|
|
107
|
+
"""
|
|
108
|
+
Query the LLM with a natural language query and schema text to generate a tool context block.
|
|
100
109
|
|
|
101
|
-
|
|
110
|
+
This returns a string like:
|
|
111
|
+
Natural language query:
|
|
112
|
+
List the orders date_shipped is null and CreatedOn before 2023-07-14, and send a discount email (subject: 'Discount Offer') to the customer for each one.
|
|
113
|
+
<docs/mcp_learning/mcp.prompt>
|
|
114
|
+
<docs/mcp_learning/mcp_schema.txt>
|
|
102
115
|
|
|
103
|
-
|
|
116
|
+
It handles both orchestration and simple GET requests.
|
|
117
|
+
"""
|
|
104
118
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
app_logger.info(f'.. see security.declare_security.py -- {server_setup.declare_security_message}\n\n')
|
|
132
|
-
|
|
133
|
-
app_logger.info(f'*************************************************************************')
|
|
134
|
-
app_logger.info(f'* Startup Instructions: Open your Browser at: {start_up_message}')
|
|
135
|
-
app_logger.info(f'*************************************************************************\n')
|
|
136
|
-
|
|
137
|
-
flask_app.run(host=args.flask_host, threaded=True, port=args.port)
|
|
138
|
-
else:
|
|
139
|
-
msg = f'API Logic Project Loaded (WSGI), version 15.00.08\n'
|
|
140
|
-
msg += f'.. startup message: {start_up_message}\n'
|
|
141
|
-
|
|
142
|
-
if server_setup.is_docker():
|
|
143
|
-
msg += f' (running from docker container at {args.flask_host} - may require refresh)\n'
|
|
119
|
+
global create_tool_context_from_llm
|
|
120
|
+
|
|
121
|
+
content = f"Natural language query:\n {nl_query}\n\nLearnings_and_Schema:\n{learnings_and_schema}"
|
|
122
|
+
messages = [
|
|
123
|
+
{
|
|
124
|
+
"role": "system",
|
|
125
|
+
"content": "You are an API planner that converts natural language queries into MCP Tool Context blocks using JSON:API. Return only the tool context as JSON."
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
"role": "user",
|
|
129
|
+
"content": f"{content}"
|
|
130
|
+
}
|
|
131
|
+
]
|
|
132
|
+
|
|
133
|
+
request_print = content[0:1400] + '\n... etc from step 1' # limit for readability
|
|
134
|
+
log.debug("\n\n\n2a. LLM request:\n\n" + request_print)
|
|
135
|
+
schema_print = json.dumps(json.loads(learnings_and_schema), indent=4)[:400] # limit for readability
|
|
136
|
+
# log.debug(schema_print)
|
|
137
|
+
|
|
138
|
+
if create_tool_context_from_llm: # takes 2-3 seconds...
|
|
139
|
+
response = openai.chat.completions.create(
|
|
140
|
+
model="gpt-4",
|
|
141
|
+
messages=messages,
|
|
142
|
+
temperature=0.2
|
|
143
|
+
)
|
|
144
|
+
tool_context_str = response.choices[0].message.content
|
|
144
145
|
else:
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
146
|
+
# read integration/mcp/mcp_tool_context.json
|
|
147
|
+
tool_context_file_path = os.path.join(os.path.dirname(__file__), "../../integration/mcp/examples/mcp_tool_context_response_get.json")
|
|
148
|
+
if nl_query == default_query_email:
|
|
149
|
+
tool_context_file_path = os.path.join(os.path.dirname(__file__), "../../integration/mcp/examples/mcp_tool_context_response.json")
|
|
150
|
+
try:
|
|
151
|
+
with open(tool_context_file_path, "r") as tool_context_file:
|
|
152
|
+
tool_context_str = tool_context_file.read()
|
|
153
|
+
# log.info(f"\n\n2c. Tool context from file {tool_context_file_path}:\n" + tool_context_str)
|
|
154
|
+
except FileNotFoundError:
|
|
155
|
+
raise ConstraintException(f"Tool context file not found at {tool_context_file_path}.")
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
tool_context_str_no_cr = tool_context_str.replace("\n", '') # convert single quotes to double quotes
|
|
159
|
+
try:
|
|
160
|
+
tool_context = json.loads(tool_context_str_no_cr)
|
|
161
|
+
except json.JSONDecodeError:
|
|
162
|
+
print("Failed to decode JSON from response:\n" + tool_context_str)
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
log.info(f"\n2b. generated tool context from LLM:\n" + json.dumps(tool_context, indent=4))
|
|
166
|
+
|
|
167
|
+
if "resources" not in tool_context:
|
|
168
|
+
raise ConstraintException("GenAI Error - LLM response does not contain 'resources'.")
|
|
169
|
+
return tool_context
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def process_tool_context(tool_context):
|
|
174
|
+
|
|
175
|
+
log.info("\n3. MCP Client Executor – Starting Tool Context Execution\n")
|
|
176
|
+
context_results = []
|
|
177
|
+
''' results from each step are appended to this list,
|
|
178
|
+
which is used to resolve variables in subsequent steps. '''
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def get_query_param_filter(query_params):
|
|
182
|
+
""" return json:api filter
|
|
183
|
+
|
|
184
|
+
eg
|
|
185
|
+
curl -qg 'http://localhost:5656/api/Order?filter=[{"name":"date_shipped","op":"eq","val":null},{"name":"CreatedOn","op":"lt","val":"2023-07-14"}]'
|
|
186
|
+
|
|
187
|
+
curl -qg 'http://localhost:5656/api/Order?filter=[{"name":"date_shipped","op":"gt","val":"2023-07-14"}]'
|
|
188
|
+
curl -qg 'http://localhost:5656/api/Order?filter=[{"name":"date_shipped","op":"eq","val":null}]'
|
|
189
|
+
curl -qg 'http://localhost:5656/api/Customer?filter=[{"name":"credit_limit","op":"gt","val":"1000"}]'
|
|
190
|
+
|
|
191
|
+
query_params might be simple:
|
|
192
|
+
"query_params": [ {"name": "credit_limit", "op": "gt", "val": "1000"} ]
|
|
193
|
+
==> ?filter=[{"name":"credit_limit","op":"gt","val":"1000"}]
|
|
194
|
+
|
|
195
|
+
or a list:
|
|
196
|
+
"query_params": [
|
|
197
|
+
{
|
|
198
|
+
"name": "date_shipped",
|
|
199
|
+
"op": "eq",
|
|
200
|
+
"val": None
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
"name": "date_created",
|
|
204
|
+
"op": "lt",
|
|
205
|
+
"val": "2023-07-14"
|
|
206
|
+
}
|
|
207
|
+
],
|
|
208
|
+
|
|
209
|
+
"""
|
|
210
|
+
|
|
211
|
+
added_rows = 0
|
|
212
|
+
|
|
213
|
+
query_param_filter = ''
|
|
214
|
+
assert isinstance(query_params, list), "Query Params filter expected to be a list"
|
|
215
|
+
query_param_filter = 'filter=' + str(query_params)
|
|
216
|
+
# use urlencode to convert to JSON:API format...
|
|
217
|
+
# val urllib.parse.quote() or urllib.parse.urlencode()
|
|
218
|
+
# tool instructions... filtering, email etc "null"
|
|
219
|
+
query_param_filter = query_param_filter.replace("'", '"') # convert single quotes to double quotes
|
|
220
|
+
query_param_filter = query_param_filter.replace("None", 'null')
|
|
221
|
+
query_param_filter = query_param_filter.replace('"null"', 'null')
|
|
222
|
+
# query_param_filter = query_param_filter.replace("date_created", 'CreatedOn') # TODO - why this name?
|
|
223
|
+
return query_param_filter # end get_query_param_filter
|
|
224
|
+
|
|
225
|
+
def print_get_response(query_param_filter, mcp_response):
|
|
226
|
+
""" Print the response from the GET request. """
|
|
227
|
+
log.info("\n3. MCP Server (als) GET filter(query_param_filter):\n" + query_param_filter)
|
|
228
|
+
log.info(" GET Response:\n" + mcp_response.text)
|
|
229
|
+
results : List[Dict] = mcp_response.json()['data']
|
|
230
|
+
# print results in a table format
|
|
231
|
+
if results:
|
|
232
|
+
# Get all unique keys from all result dicts
|
|
233
|
+
keys = set()
|
|
234
|
+
for row in results:
|
|
235
|
+
if isinstance(row, dict):
|
|
236
|
+
keys.update(row.keys())
|
|
237
|
+
keys = list(keys)
|
|
238
|
+
# Print header
|
|
239
|
+
log.info("\n| " + " | ".join(keys) + " |")
|
|
240
|
+
log.info("|" + "|".join(["---"] * len(keys)) + "|")
|
|
241
|
+
# Print rows
|
|
242
|
+
for row in results:
|
|
243
|
+
log.info("| " + " | ".join(str(row.get(k, "")) for k in keys) + " |")
|
|
244
|
+
else:
|
|
245
|
+
log.info("No results found.")
|
|
246
|
+
|
|
247
|
+
def substitute_vars(val, context, row=None, ref_index=None):
|
|
248
|
+
"""
|
|
249
|
+
Substitutes variable references in a value using a provided context.
|
|
250
|
+
|
|
251
|
+
If `val` is a string starting with '$', attempts to parse it as a variable reference
|
|
252
|
+
of the form '$<step_idx>[*].<attr>' or '$<step_idx>.<attr>'. Retrieves the corresponding
|
|
253
|
+
value from the `context` list or from the `row` dictionary if the reference index matches.
|
|
254
|
+
|
|
255
|
+
Args:
|
|
256
|
+
val (Any): The value to substitute. If not a string or not a variable reference, returned as-is.
|
|
257
|
+
reference example: '$0[*].customer_id' or '$1.email'
|
|
258
|
+
context (list): A list of dictionaries or objects used for variable substitution.
|
|
259
|
+
The result list from prior step
|
|
260
|
+
Each item in the list is expected to be a dictionary with attributes that can be accessed.
|
|
261
|
+
row (dict, optional): A dictionary representing the current row, used if the reference index matches `ref_index`.
|
|
262
|
+
The current row (eg, order) dictionary for variable substitution.
|
|
263
|
+
ref_index (int, optional): The index to compare against the variable reference for row substitution.
|
|
264
|
+
|
|
265
|
+
Returns:
|
|
266
|
+
Any: The substituted value if a variable reference is found and resolved, otherwise the original value.
|
|
267
|
+
"""
|
|
268
|
+
if isinstance(val, str) and val.startswith("$"):
|
|
269
|
+
match = re.match(r"\$(\d+)(\[\*\])?\.(\w+)", val)
|
|
270
|
+
if not match:
|
|
271
|
+
return val
|
|
272
|
+
step_idx, star, attr = match.groups()
|
|
273
|
+
step_idx = int(step_idx)
|
|
274
|
+
if enabled_fix_me := False and star: # TODO: fix this disabled code
|
|
275
|
+
return context[step_idx]
|
|
276
|
+
if row is not None and step_idx == ref_index:
|
|
277
|
+
return row['attributes'][attr] if attr in row['attributes'] else row.get(attr)
|
|
278
|
+
return context[step_idx].get(attr)
|
|
279
|
+
return val
|
|
280
|
+
|
|
281
|
+
def resolve_step(step, context, row=None, ref_index=None):
|
|
282
|
+
"""
|
|
283
|
+
Resolves variables in the 'body' and 'query_params' fields of a step dictionary using the provided context, row, and ref_index.
|
|
284
|
+
|
|
285
|
+
Args:
|
|
286
|
+
step (dict): The step dictionary containing 'body' and 'query_params' fields, each as a list of field dictionaries.
|
|
287
|
+
context (dict): The context dictionary used for variable substitution. eg, the orders
|
|
288
|
+
row (dict, optional): An optional source row (eg, order) dictionary for variable substitution.
|
|
289
|
+
ref_index (int, optional): An optional reference index for variable substitution. Defaults to None.
|
|
290
|
+
|
|
291
|
+
Returns:
|
|
292
|
+
dict: A copy of the step dictionary with variables in 'body' and 'query_params' fields resolved.
|
|
293
|
+
"""
|
|
294
|
+
|
|
295
|
+
def resolve_field_list(field_list):
|
|
296
|
+
"""
|
|
297
|
+
Resolves a list of field dictionaries by substituting variables in their 'value' fields.
|
|
298
|
+
|
|
299
|
+
Each field in the input list is expected to be a dictionary containing a 'value' key.
|
|
300
|
+
The function applies the substitute_vars function to the 'value' of each field,
|
|
301
|
+
using the provided context, row, and ref_index, and returns a new list of fields
|
|
302
|
+
with the substituted values.
|
|
303
|
+
|
|
304
|
+
Args:
|
|
305
|
+
field_list (list of dict): A list of field dictionaries (eg, email post row), each containing at least a 'value' key, eg
|
|
306
|
+
{'subject': 'Discount Offer', 'message': 'You have a new discount offer', 'customer_id': '$0[*].customer_id'}
|
|
307
|
+
|
|
308
|
+
Returns:
|
|
309
|
+
list of dict: A new list of field dictionaries with the 'value' field updated after variable substitution.
|
|
310
|
+
"""
|
|
311
|
+
|
|
312
|
+
# return dict(f, value=substitute_vars(f.get("value"), context, row, ref_index)) for f in field_list
|
|
313
|
+
resolved_fields = []
|
|
314
|
+
for field_name, field_value in field_list.items():
|
|
315
|
+
resolved_field = {}
|
|
316
|
+
resolved_field[field_name] = substitute_vars(field_value, context, row, ref_index)
|
|
317
|
+
resolved_fields.append(resolved_field)
|
|
318
|
+
return resolved_fields
|
|
319
|
+
|
|
320
|
+
step_copy = {**step}
|
|
321
|
+
step_copy["body"] = resolve_field_list(step.get("body", []))
|
|
322
|
+
if "query_params" in step_copy:
|
|
323
|
+
step_copy["query_params"] = resolve_field_list(step.get("query_params", []))
|
|
324
|
+
return step_copy
|
|
325
|
+
|
|
326
|
+
def find_fan_out_key(step):
|
|
327
|
+
"""
|
|
328
|
+
Fan-out means that the step has a key pattern like '$<number>[*].<field_name>',
|
|
329
|
+
so the action (eg, send mail) is repeated for each item (eg, order) in the list at 'context[<number>]'.
|
|
330
|
+
|
|
331
|
+
Searches for a fan-out key pattern in the 'body' of the given step.
|
|
332
|
+
|
|
333
|
+
The function iterates over the fields in the 'body' of the step dictionary,
|
|
334
|
+
looking for a field whose 'value' is a string containing the pattern '[*]'.
|
|
335
|
+
If such a pattern is found and matches the format '$<number>[*].<field_name>',
|
|
336
|
+
it extracts and returns the number and field name as a tuple.
|
|
337
|
+
|
|
338
|
+
Args:
|
|
339
|
+
step (dict): A dictionary representing a step, expected to have a 'body' key
|
|
340
|
+
containing a list of field dictionaries with a 'value' key.
|
|
341
|
+
|
|
342
|
+
{.. 'body': {'subject': 'Discount Offer', 'message': 'You have a new discount offer', 'customer_id': '$0[*].customer_id'}
|
|
343
|
+
|
|
344
|
+
Returns:
|
|
345
|
+
tuple[int, str] or None: A tuple containing the integer index and the field name
|
|
346
|
+
if a matching pattern is found, otherwise None.
|
|
347
|
+
"""
|
|
348
|
+
if 'body' in step:
|
|
349
|
+
body = step.get("body", [])
|
|
350
|
+
# iterate the body fields / values
|
|
351
|
+
# This loop checks each field in the body for a fan-out pattern
|
|
352
|
+
for attr_name, attr_value in body.items():
|
|
353
|
+
if isinstance(attr_value, str) and "[*]" in attr_value:
|
|
354
|
+
match = re.match(r"\$(\d+)\[\*\]\.(\w+)", attr_value)
|
|
355
|
+
if match:
|
|
356
|
+
return int(match.group(1)), match.group(2)
|
|
357
|
+
# If the body is a list, iterate through each field
|
|
358
|
+
for field in body:
|
|
359
|
+
if isinstance(field["value"], str) and "[*]" in field["value"]: # string indices must be integers, not 'str'
|
|
360
|
+
match = re.match(r"\$(\d+)\[\*\]\.(\w+)", field["value"])
|
|
361
|
+
if match:
|
|
362
|
+
return int(match.group(1)), match.group(2)
|
|
363
|
+
return None
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
def call_llm(step, context, tool_context):
|
|
367
|
+
prompt = f"""
|
|
368
|
+
User Goal: {step.get('llm_goal')}
|
|
369
|
+
Step Result: {json.dumps(context[-1], indent=2)}
|
|
370
|
+
|
|
371
|
+
Based on this, generate the next tool_context step(s) as a JSON list.
|
|
372
|
+
"""
|
|
373
|
+
try:
|
|
374
|
+
import openai
|
|
375
|
+
response = openai.chat.completions.create(
|
|
376
|
+
model="gpt-4",
|
|
377
|
+
messages=[{"role": "user", "content": prompt}],
|
|
378
|
+
temperature=0.0
|
|
379
|
+
)
|
|
380
|
+
return json.loads(response.choices[0].message.content)
|
|
381
|
+
except OpenAIError as e:
|
|
382
|
+
log.info(f"OpenAI error: {e}")
|
|
383
|
+
return []
|
|
384
|
+
except Exception as e:
|
|
385
|
+
log.info(f"Failed LLM call: {e}")
|
|
386
|
+
return []
|
|
387
|
+
|
|
388
|
+
def execute_api_step(step, step_num):
|
|
389
|
+
url = step["base_url"].rstrip("/") + "/" + step["path"].lstrip("/")
|
|
390
|
+
method = step["method"].upper()
|
|
391
|
+
# params = {p["name"]: p["val"] for p in step.get("query_params", [])}
|
|
392
|
+
params = get_query_param_filter(step.get("query_params", []))
|
|
393
|
+
# body = {p["name"]: p["value"] for p in step.get("body", [])} if step.get("body", []) else None # fixme: name?
|
|
394
|
+
body = {}
|
|
395
|
+
if step.get("body", []):
|
|
396
|
+
# eg: POST http://localhost:5656/api/SysEmail {'data': {'type': 'SysEmail', 'attributes': {'customer_id': 5, 'message': {'to': '{{ order.customer_id }}', 'subject': 'Discount for your order', 'body': 'Dear customer, you have a discount for your recent order. Thank you for shopping with us.'}}}}
|
|
397
|
+
body = {'data': {"type": step["path"].split("/")[-1], 'attributes': {}}} # eg: SysEmail
|
|
398
|
+
for each_field in step["body"]:
|
|
399
|
+
body['data']['attributes'].update(each_field) # each_field is a dict, eg: {'subject': 'Discount Offer', 'message': 'You have a new discount offer', 'customer_id': '$0[*].customer_id'}
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
log.info(f"\n\n➡️ MCP execute_api_step[{step_num}]:")
|
|
403
|
+
log.info(f" Method: {method} {url}")
|
|
404
|
+
log.info(f" Query: {params}")
|
|
405
|
+
log.info(f" Body: {body}\n")
|
|
406
|
+
try:
|
|
407
|
+
resp = requests.request(method, url, json=body if method in ["POST", "PATCH"] else None, params=params)
|
|
408
|
+
resp.raise_for_status()
|
|
409
|
+
return resp.json()
|
|
410
|
+
except requests.RequestException as e:
|
|
411
|
+
log.info(f"❌ Request failed: {e}")
|
|
412
|
+
return {}
|
|
413
|
+
|
|
414
|
+
step_num = 0
|
|
415
|
+
steps = tool_context["resources"]
|
|
416
|
+
for each_step in steps:
|
|
417
|
+
|
|
418
|
+
if each_step.get("llm_call"):
|
|
419
|
+
log.info(f"\n🔁 LLM Call triggered at step {i}")
|
|
420
|
+
new_steps = call_llm(each_step, context_results, tool_context)
|
|
421
|
+
tool_context[i+1:i+1] = new_steps
|
|
422
|
+
i += 1
|
|
423
|
+
continue
|
|
424
|
+
|
|
425
|
+
fan_out = find_fan_out_key(each_step)
|
|
426
|
+
if fan_out:
|
|
427
|
+
ref_idx, attr = fan_out
|
|
428
|
+
fan_out_list = context_results[ref_idx]
|
|
429
|
+
if isinstance(fan_out_list, dict) and "data" in fan_out_list:
|
|
430
|
+
fan_out_list = fan_out_list["data"]
|
|
431
|
+
for row in fan_out_list:
|
|
432
|
+
resolved = resolve_step(each_step, context_results, row, ref_idx)
|
|
433
|
+
result = execute_api_step(resolved, step_num)
|
|
434
|
+
context_results.append(result)
|
|
435
|
+
else:
|
|
436
|
+
resolved = each_step if len(context_results) == 0 else resolve_step(each_step, context_results)
|
|
437
|
+
result = execute_api_step(resolved, step_num)
|
|
438
|
+
context_results.append(result)
|
|
439
|
+
step_num += 1
|
|
440
|
+
|
|
441
|
+
log.info("\n✅ MCP Client Executor – All Steps Executed\n")
|
|
442
|
+
|
|
443
|
+
return context_results
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def mcp_client_executor(query: str):
|
|
448
|
+
"""
|
|
449
|
+
|
|
450
|
+
#als: create an MCP request. See https://apilogicserver.github.io/Docs/Integration-MCP/
|
|
451
|
+
|
|
452
|
+
Test:
|
|
453
|
+
* curl -X 'POST' 'http://localhost:5656/api/SysMcp/' -H 'accept: application/vnd.api+json' -H 'Content-Type: application/json' -d '{ "data": { "attributes": {"request": "List the orders date_shipped is null and CreatedOn before 2023-07-14, and send a discount email (subject: '\''Discount Offer'\'') to the customer for each one."}, "type": "SysMcp"}}'
|
|
454
|
+
* Or, use the Admin App and insert a row into SysMCP (see default `query`, below)
|
|
455
|
+
|
|
456
|
+
Args:
|
|
457
|
+
query (str): The natural language query to process.
|
|
458
|
+
"""
|
|
459
|
+
|
|
460
|
+
learnings_and_schema = discover_mcp_servers() # see: 1-discovery-from-als
|
|
461
|
+
|
|
462
|
+
tool_context = query_llm_with_nl(learnings_and_schema, query) # see: 2-tool-context-from-LLM
|
|
463
|
+
|
|
464
|
+
mcp_response = process_tool_context(tool_context) # see: 3-MCP-server response
|
|
465
|
+
|
|
466
|
+
log.info("\nTest complete.\n")
|
|
467
|
+
|
|
468
|
+
return tool_context, mcp_response
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
if __name__ == "__main__": # F5 to start API Logic Server
|
|
472
|
+
|
|
473
|
+
logging_config = f'{project_path}/config/logging.yml'
|
|
474
|
+
if os.getenv('APILOGICPROJECT_LOGGING_CONFIG'):
|
|
475
|
+
logging_config = project_path.joinpath(os.getenv("APILOGICPROJECT_LOGGING_CONFIG"))
|
|
476
|
+
with open(logging_config,'rt') as f: # see also logic/declare_logic.py
|
|
477
|
+
config=yaml.safe_load(f.read())
|
|
478
|
+
f.close()
|
|
479
|
+
logging.config.dictConfig(config) # log levels: notset 0, debug 10, info 20, warn 30, error 40, critical 50
|
|
480
|
+
|
|
481
|
+
query = default_query # default query if no argument is provided
|
|
482
|
+
|
|
483
|
+
if len(sys.argv) > 1: # if 1 non-blank argument, use it as the query
|
|
484
|
+
query = sys.argv[1]
|
|
485
|
+
if query == 'mcp':
|
|
486
|
+
query = default_query_email
|
|
487
|
+
|
|
488
|
+
mcp_client_executor(query)
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
api_logic_server_cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
api_logic_server_cli/api_logic_server.py,sha256=
|
|
3
|
-
api_logic_server_cli/api_logic_server_info.yaml,sha256=
|
|
2
|
+
api_logic_server_cli/api_logic_server.py,sha256=nE2vSudfjI-clkh-s5WDEQrdlon9IOjBg-4XRbQ_B0E,96146
|
|
3
|
+
api_logic_server_cli/api_logic_server_info.yaml,sha256=Nycwn4IK0eMJvxY0FSHZaOi1I3uNShONHxJjzbDfYDY,125
|
|
4
4
|
api_logic_server_cli/cli.py,sha256=AT1cWszOygHWIbpxDoXFhaTeSai3Tf5SbGoXvN4h510,83134
|
|
5
5
|
api_logic_server_cli/cli_args_base.py,sha256=lr27KkOB7_WpZwTs7LgiK8LKDIHMKQkoZCTnE99BFxw,3280
|
|
6
6
|
api_logic_server_cli/cli_args_project.py,sha256=I5no_fGRV_ZsK3SuttVDAaQYI4Q5zCjx6LojGkM024w,4645
|
|
7
7
|
api_logic_server_cli/extended_builder.py,sha256=EhtXGAt_RrDR2tCtgvc2U82we7fr-F6pP-e6HS6dQWQ,13867
|
|
8
8
|
api_logic_server_cli/logging.yml,sha256=isWhKviFwJwYgjIUejfhUxcMli2zEbZeQbEvVhNk_4Y,1812
|
|
9
|
-
api_logic_server_cli/manager.py,sha256=
|
|
9
|
+
api_logic_server_cli/manager.py,sha256=YlUat3sRgBu1h8MntJZg7-kiKU1nrLnw0zyfxG_oHTM,11096
|
|
10
10
|
api_logic_server_cli/add_cust/add_cust.py,sha256=ZRmCr62ljocGuiMgplFhpLMFfLJrqsGo7KX0-zKZx-U,14046
|
|
11
11
|
api_logic_server_cli/create_from_model/.DS_Store,sha256=1lFlJ5EFymdzGAUAaI30vcaaLHt3F1LwpG7xILf9jsM,6148
|
|
12
12
|
api_logic_server_cli/create_from_model/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -630,7 +630,7 @@ api_logic_server_cli/prototypes/base/integration/kafka/kafka_consumer.py,sha256=
|
|
|
630
630
|
api_logic_server_cli/prototypes/base/integration/kafka/kafka_producer.py,sha256=g0nMAVfz1Y0iKJbbXfvRpdf-QUmyB4uUGZ6lyaVoXag,4470
|
|
631
631
|
api_logic_server_cli/prototypes/base/integration/kafka/kafka_readme.md,sha256=MlwykHWM2w41KzWh4vPuTnIodR8f-BQzrWpV4P1hrsI,161
|
|
632
632
|
api_logic_server_cli/prototypes/base/integration/mcp/.DS_Store,sha256=1lFlJ5EFymdzGAUAaI30vcaaLHt3F1LwpG7xILf9jsM,6148
|
|
633
|
-
api_logic_server_cli/prototypes/base/integration/mcp/mcp_client_executor.py,sha256=
|
|
633
|
+
api_logic_server_cli/prototypes/base/integration/mcp/mcp_client_executor.py,sha256=vwWMdfNkTiDxkooAEDU-48OMArQrwku8L_YV1CouUH4,23143
|
|
634
634
|
api_logic_server_cli/prototypes/base/integration/mcp/mcp_server_discovery.json,sha256=TUyInb67AWoGw7XFE9iDZxmM8UEID-ahQmdmzpF9AmQ,188
|
|
635
635
|
api_logic_server_cli/prototypes/base/integration/mcp/examples/mcp_discovery_response.json,sha256=f1RP5kuTU8rkqxWsZoATBF8xdaVEgPTaB4MRoExIF-Q,3763
|
|
636
636
|
api_logic_server_cli/prototypes/base/integration/mcp/examples/mcp_request.prompt,sha256=vmt_fvwOK-C2fnI1LLUMe5WbLk6qxv2RdVJkBUTo9zM,2601
|
|
@@ -6089,9 +6089,9 @@ api_logic_server_cli/tools/mini_skel/database/system/SAFRSBaseX.py,sha256=p8C7AF
|
|
|
6089
6089
|
api_logic_server_cli/tools/mini_skel/database/system/TestDataBase.py,sha256=U02SYqThsbY5g3DX7XGaiMxjZBuOpzvtPS6RfI1WQFg,371
|
|
6090
6090
|
api_logic_server_cli/tools/mini_skel/logic/declare_logic.py,sha256=fTrlHyqMeZsw_TyEXFa1VlYBL7fzjZab5ONSXO7aApo,175
|
|
6091
6091
|
api_logic_server_cli/tools/mini_skel/logic/load_verify_rules.py,sha256=Rr5bySJpYCZmNPF2h-phcPJ53nAOPcT_ohZpCD93-a0,7530
|
|
6092
|
-
apilogicserver-15.0.
|
|
6093
|
-
apilogicserver-15.0.
|
|
6094
|
-
apilogicserver-15.0.
|
|
6095
|
-
apilogicserver-15.0.
|
|
6096
|
-
apilogicserver-15.0.
|
|
6097
|
-
apilogicserver-15.0.
|
|
6092
|
+
apilogicserver-15.0.10.dist-info/licenses/LICENSE,sha256=67BS7VC-Z8GpaR3wijngQJkHWV04qJrwQArVgn9ldoI,1485
|
|
6093
|
+
apilogicserver-15.0.10.dist-info/METADATA,sha256=VFMeIl21nasGZYnXoAk7ewyP5J1DefP1xHce2dIl-zM,6549
|
|
6094
|
+
apilogicserver-15.0.10.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
6095
|
+
apilogicserver-15.0.10.dist-info/entry_points.txt,sha256=KiLloZJ3c_RW-nIDqBtoE0WEsQTnZ3dELwHLWi23LMA,103
|
|
6096
|
+
apilogicserver-15.0.10.dist-info/top_level.txt,sha256=-r0AT_GEApleihg-jIh0OMvzzc0BO1RuhhOpE91H5qI,21
|
|
6097
|
+
apilogicserver-15.0.10.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|