4d-mcp-tool-kit
v1.0.0
Published
   The
src/config.tsfile loads these variables usingdotenv.
The manifest.json File
This JSON file defines the tools (4D functions) to expose. See the Advanced Configuration section for schema details.
Using the parseTools Function
The parseTools function reads your manifest, builds input schemas, and registers tools on the MCP server. It supports custom tool registration and dynamic parameter validation.
const server = await parseTools(
tools,
new Client_4D(config.baseUrl, config.acessKey),
{
beforeReturn: customTools,
afterRegister: ar,
excludeTools: [],
info: { name: "TestServer" }
}
);Running the Provided Examples
HTTP Server (Express):
npm run build npm startThe server will listen on the port specified by
MCP_PORT(default: 3000).Stdio Transport:
ts-node src/index_stdio.ts
🔬 Scripts & Development
Common scripts (see package.json):
npm run build— Compile TypeScript and copy manifest and .env to build directory.npm test— Run tests with Jest.npm run test:watch— Run tests with Jest in watch mode.npm run test:coverage— Run tests with coverage report.npm run start-http-server— Start the HTTP server from the build output.npm run start-stdio-server— Start the stdio server from the build output.npm run inspect— Run the MCP inspector tool.
🧩 Advanced Configuration & Customization
Registering Custom Tools
You can register custom tools (e.g., CheckDataStore) in src/index.ts using the MCP server API. This enables dynamic HTTP method selection and catalog inspection.
Flexible manifest.json Schema
Attributes for Data Retrieval:
Specify which attributes to retrieve for each table to optimize performance.{ "name": "vectorSearch", "attributes": { "TableName": ["attribute1", "attribute2"] } }Enums for Parameter Control:
Restrict parameters to predefined values.{ "name": "TableName", "type": "string", "enum": ["users", "Employee"], "description": "The name of the table to perform the search on." }Dynamic Parameters with
enumMapanddependsOn:
Make parameter options depend on other parameter values.{ "name": "EmbeddingKey", "type": "string", "dependsOn": "TableName", "enumMap": { "users": ["vector"], "Employee": ["vector", "embedding"] }, "description": "The name of the vector/embedding column to use for the search. The valid options depend on the selected TableName." }
📦 Example 4D Function
Below is an example of a 4D function designed for vector search that can be exposed on the REST API. This function must be exposed as a DataStore method.
// Exposed as a DataStore method, for example, by extending the DataStore Class
#DECLARE($querry : Text; $maxResults : Integer; $TableName : Text; $embeddingKey : Text; $similarity : Text)->$similarities : cs.EntitySelection
// initialisation de openai embeddings
var $client:=cs.AIKit.OpenAI.new("YOUR_OPEN_API_API_KEY")
var $result:=$client.embeddings.create($querry; "text-embedding-3-large"; cs.AIKit.OpenAIEmbeddingsParameters.new({dimensions: 1536}))
var $vector : 4D.Vector:=$result.vector
// Récupération des données :
$entries:=ds[$TableName].all()
// Création du vecteur de recherche
var $SearchVector : 4D.Vector
$SearchVector:=$vector
// Initialisation de la collection des similarités
$similarities:=ds[$tableName].newSelection()
// Déclaration des variables
var $entry : cs.Entity
var $VectorField : 4D.Vector
var $cs : Real
var $csArray:=[]
var $item : Object
// Boucle sur chaque employé
For each ($entry; $entries)
$VectorField:=$entry[$embeddingKey]
Case of
: ($similarity="cosineSimilarity")
$cs:=$VectorField.cosineSimilarity($SearchVector)
: ($similarity="dotSimilarity")
$cs:=$VectorField.dotSimilarity($SearchVector)
: ($similarity="euclideanDistance")
$cs:=$VectorField.euclideanDistance($SearchVector)
End case
$csArray.push(New object("similarity"; $cs; "key"; $entry.getKey())
End for each
var $output:=[]
Case of
: ($similarity="euclideanDistance")
$csArray:=$csArray.orderBy("similarity asc")
Else
$csArray:=$csArray.orderBy("similarity desc")
End case
$csArray:=$csArray.slice(0; $maxResults)
For each ($item; $csArray)
$similarities.add(ds[$tableName].get($item.key))
End for each
🧪 Testing
- Tests are written using Jest.
- Test files are located in the
test/directory. - To run all tests:
npm test - To run with coverage:
npm run test:coverage
🔒 Notes
- Ensure your 4D database REST API is accessible and the access key is set.
- The project uses strict TypeScript settings for reliability.
- For more details, see the code and comments in
src/index.tsandsrc/parseTools.ts.
🤝 Contributing
Contributions are welcome! Please open issues or submit pull requests for improvements and bug fixes.
📄 License
This project is licensed under the ISC License. See the LICENSE file for details.
