Skip to content

callibrity/mocapi

Mocapi

Mocapi is a modular framework for building Model Context Protocol (MCP) tools and prompts using Spring Boot. It simplifies secure, structured interactions between LLMs and services via annotated Java components.

Maven Central Version GitHub License

Maintainability Rating Reliability Rating Security Rating Vulnerabilities Quality Gate Status Coverage Lines of Code

Getting Started

Mocapi includes a Spring Boot starter, making it easy to get started by simply adding a dependency:

<dependency>
    <groupId>com.callibrity.mocapi</groupId>
    <artifactId>mocapi-spring-boot-starter</artifactId>
    <version>${mocapi.version}</version>
</dependency>

By default, Mocapi will listen for MCP requests on the /mcp endpoint. You can change this by setting the ripcurl.endpoint property:

ripcurl.endpoint=/your-custom-endpoint

Creating MCP Tools

To create MCP tools using Mocapi, you first need to import the mocapi-tools dependency into your project:

<dependency>
    <groupId>com.callibrity.mocapi</groupId>
    <artifactId>mocapi-tools</artifactId>
    <version>${mocapi.version}</version>
</dependency>

This will automatically activate the MocapiToolsAutoConfiguration which will enable MCP tools support. To register a tool, you need to create a bean annotated with @ToolService having methods annotated with @Tool:

import com.callibrity.mocapi.tools.annotation.Tool;
import com.callibrity.mocapi.tools.annotation.ToolService;
import org.springframework.stereotype.Component;

@Component
@ToolService
public class HelloTool {

    @Tool
    public HelloResponse sayHello(String name) {
        return new HelloResponse(String.format("Hello, %s!", name));
    }

    public record HelloResponse(String message) { }
}

Creating MCP Prompts

To create MCP prompts using Mocapi, you first need to import the mocapi-prompts dependency into your project:

<dependency>
    <groupId>com.callibrity.mocapi</groupId>
    <artifactId>mocapi-prompts</artifactId>
    <version>${mocapi.version}</version>
</dependency>

This will automatically activate the MocapiPromptsAutoConfiguration which will enable MCP prompts support. To register a prompt, you need to create a bean annotated with @PromptService having methods annotated with @Prompt:

import com.callibrity.mocapi.prompts.GetPromptResult;
import com.callibrity.mocapi.prompts.PromptMessage;
import com.callibrity.mocapi.prompts.Role;
import com.callibrity.mocapi.prompts.annotation.Prompt;
import com.callibrity.mocapi.prompts.annotation.PromptService;
import com.callibrity.mocapi.prompts.content.TextContent;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@PromptService
public class CodeReviewPrompts {

    @Prompt(name = "review-code", description = "Provide a short review of the given code snippet")
    public GetPromptResult reviewCode(String language, String code) {
        var prompt = String.format("""
                Please review the following %s code and suggest improvements:
                
                ```%s
                %s
                ```
                """, language, language, code);

        return new GetPromptResult("Provide a short review of the given code snippet", List.of(
                new PromptMessage(Role.USER, new TextContent(prompt))
        ));
    }
}

Running the Example Application

Mocapi includes an example application to demonstrate how to use the framework. To run it, follow these steps:

  1. Navigate to the mocapi-example directory.

    cd mocapi-example
  2. Run the application using Maven:

    mvn spring-boot:run
  3. The application will start on port 8080 by default. You can access the example tools and prompts at the /mcp endpoint.

Using the MCP Inspector

You can interact with the example application using the MCP Inspector. To do this, follow these steps:

  1. Launch the MCP inspector by running the following command in your terminal:
    npx @modelcontextprotocol/inspector
  2. Your browser will open the MCP Inspector interface. Enter the URL of the running example application (e.g., http://localhost:8080/mcp) in the URL field.
  3. Select the "Streamable HTTP" transport option.
  4. Click "Connect" to establish a connection to the example application.
  5. You can now explore the available tools and prompts, send requests, and view responses directly in the MCP Inspector interface.
  6. Enjoy!

What's in a Name?

Mocapi is a made-up word that includes the letters MCP (Model Context Protocol). It’s pronounced moh-cap-ee (/ˈmoʊˌkæpi/), like a friendly little robot who speaks protocol.

Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Building from Source

To build the project yourself, simply clone the repository and use Apache Maven to compile and package it:

mvn clean install

License

This project is licensed under the Apache License 2.0—see the LICENSE file for details.