How to Integrate Dify with Java: HTTP, Custom Tools, and MCP Methods

This guide explains three ways Dify can invoke Java programs—using HTTP requests, defining custom tools with OpenAPI schemas, and configuring MCP communication—providing step‑by‑step setup, code examples, and configuration snippets for each approach.

Architect's Alchemy Furnace
Architect's Alchemy Furnace
Architect's Alchemy Furnace
How to Integrate Dify with Java: HTTP, Custom Tools, and MCP Methods

Solution Overview

Dify can call external Java programs through three different mechanisms: an HTTP request, a custom tool defined via OpenAPI, or MCP (Message Communication Protocol) communication.

1. HTTP Request

Adding an “HTTP Request” node in a Dify workflow allows you to send HTTP calls to a Java service. You can configure request parameters, handle file uploads, and set retry or error handling options. The Java side only needs to expose a simple HTTP endpoint, for example:

import com.ai.difyhttpserver.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/it")
public class InterviewController {
    @RequestMapping("/add")
    public boolean add(User user) {
        // Execute database insert operation
        System.out.println(user.toString());
        return true;
    }
}

2. Custom Tool

To use a Java program as a custom tool, you must register it in Dify with an OpenAPI‑compatible schema. The schema describes the tool’s endpoint, parameters, and request/response formats. An example JSON schema looks like:

{
  "openapi": "3.1.0",
  "info": {
    "title": "html to image tools",
    "description": "Generate image files based on HTML code",
    "version": "v1.0.0"
  },
  "servers": [{"url": "http://192.168.3.94:8080"}],
  "paths": {
    "/html2img/gen": {
      "get": {
        "description": "Generate image files based on HTML code",
        "operationId": "html2image",
        "parameters": [{
          "name": "html",
          "in": "query",
          "description": "HTML code",
          "required": true,
          "schema": {"type": "string"}
        }],
        "deprecated": false
      }
    }
  },
  "components": {"schemas": {}}
}

3. MCP Communication

MCP provides a more complex but powerful way to expose multiple Java methods to Dify. You need a Java server that implements the MCP protocol (e.g., using Spring AI) and an Agent strategy in Dify that supports MCP tools. MCP servers are configured with JSON like:

{
  "server_name1": {"transport": "sse", "url": "http://127.0.0.1:8000/sse", "headers": {}, "timeout": 50, "sse_read_timeout": 50},
  "server_name2": {"transport": "sse", "url": "http://127.0.0.1:8001/sse"},
  "server_name3": {"transport": "streamable_http", "url": "http://127.0.0.1:8002/mcp", "headers": {}, "timeout": 50},
  "server_name4": {"transport": "streamable_http", "url": "http://127.0.0.1:8003/mcp"}
}
{
  "mcpServers": {
    "server_name1": {"transport": "sse", "url": "http://127.0.0.1:8000/sse", "headers": {}, "timeout": 50, "sse_read_timeout": 50},
    "server_name2": {"transport": "sse", "url": "http://127.0.0.1:8001/sse"},
    "server_name3": {"transport": "streamable_http", "url": "http://127.0.0.1:8002/mcp", "headers": {}, "timeout": 50},
    "server_name4": {"transport": "streamable_http", "url": "http://127.0.0.1:8003/mcp"}
  }
}

Conclusion

Dify can invoke Java programs via three approaches—HTTP request, custom tool, and MCP communication. The HTTP request method is the simplest and most flexible, custom tools suit tool‑oriented calls, and MCP is ideal when you need to inject multiple Java methods for Dify to use.

JavaIntegrationMCPbackend developmentHTTPDifyCustom Tool
Architect's Alchemy Furnace
Written by

Architect's Alchemy Furnace

A comprehensive platform that combines Java development and architecture design, guaranteeing 100% original content. We explore the essence and philosophy of architecture and provide professional technical articles for aspiring architects.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.