Zum Inhalt springen

MCP-Tools erstellen

Erstellen Sie benutzerdefinierte MCP-Tools, um die Fähigkeiten Ihres Digital Workers mit externen Integrationen, benutzerdefinierter Logik und Datenzugriff zu erweitern.

  1. WIT-Schnittstelle definieren

    wit/tool.wit
    package greentic:my-tool;
    interface tool {
    record input {
    query: string,
    options: option<string>,
    }
    record output {
    success: bool,
    data: option<string>,
    error: option<string>,
    }
    execute: func(input: input) -> output;
    }
    world my-tool {
    export tool;
    }
  2. In Rust implementieren

    src/lib.rs
    use wit_bindgen::generate;
    generate!({
    world: "my-tool",
    path: "wit",
    });
    struct MyTool;
    impl tool::Guest for MyTool {
    fn execute(input: tool::Input) -> tool::Output {
    // Your tool logic here
    let result = process_query(&input.query);
    tool::Output {
    success: true,
    data: Some(result),
    error: None,
    }
    }
    }
    fn process_query(query: &str) -> String {
    format!("Processed: {}", query)
    }
    export!(MyTool);
  3. WASM-Komponente bauen

    Terminal-Fenster
    cargo build --target wasm32-wasip2 --release
  4. Tool-Manifest erstellen

    tool.yaml
    name: my_tool
    version: "1.0.0"
    description: My custom MCP tool
    parameters:
    - name: query
    type: string
    description: The query to process
    required: true
    - name: options
    type: string
    description: Optional configuration
    required: false
    returns:
    type: object
    properties:
    success:
    type: boolean
    data:
    type: string
    error:
    type: string
    capabilities:
    - network:outbound
  5. Tool registrieren

    greentic.demo.yaml
    mcp:
    tools:
    - name: my_tool
    component: "tools/my-tool.wasm"
    manifest: "tools/my-tool.yaml"
impl tool::Guest for HttpTool {
fn execute(input: tool::Input) -> tool::Output {
let url = &input.url;
let method = input.method.unwrap_or("GET".to_string());
match http_request(&method, url, input.body.as_deref()) {
Ok(response) => tool::Output {
success: true,
data: Some(response),
error: None,
},
Err(e) => tool::Output {
success: false,
data: None,
error: Some(e.to_string()),
},
}
}
}
impl tool::Guest for DbTool {
fn execute(input: tool::Input) -> tool::Output {
// Get connection string from secrets
let conn_str = get_secret("database_url")?;
// Execute query (pseudo-code)
let results = db_query(&conn_str, &input.query)?;
tool::Output {
success: true,
data: Some(serde_json::to_string(&results)?),
error: None,
}
}
}
impl tool::Guest for EmailTool {
fn execute(input: tool::Input) -> tool::Output {
let api_key = get_secret("sendgrid_api_key")?;
let result = send_email(
&api_key,
&input.to,
&input.subject,
&input.body,
);
match result {
Ok(_) => tool::Output {
success: true,
data: Some("Email sent".to_string()),
error: None,
},
Err(e) => tool::Output {
success: false,
data: None,
error: Some(e.to_string()),
},
}
}
}
- id: call_tool
type: mcp-tool
config:
tool: "my_tool"
parameters:
query: "{{user_query}}"
output: tool_result
- id: agent
type: llm
config:
model: "gpt-4"
system_prompt: |
You are an assistant with access to tools.
Use tools when needed to help the user.
tools:
- my_tool
- http_request
- send_email
tool_choice: "auto"
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_execute() {
let input = tool::Input {
query: "test".to_string(),
options: None,
};
let output = MyTool::execute(input);
assert!(output.success);
assert!(output.data.is_some());
}
}
Terminal-Fenster
# Test with sample input
echo '{"query": "test"}' | greentic-mcp test ./my-tool.wasm
  1. Alle Fehler behandeln - Niemals panic auslösen, Fehler immer in output zurückgeben
  2. Eingaben validieren - Parameter vor der Verarbeitung prüfen
  3. Secrets verwenden - Zugangsdaten niemals hart kodieren
  4. Logging hinzufügen - Hilft beim Debugging
  5. Gründlich dokumentieren - Klare Beschreibungen im Manifest
  6. Edge Cases testen - Leere Eingaben, große Datenmengen usw. behandeln