Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mocapi-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<version>0.0.1-SNAPSHOT</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.callibrity.mocapi</groupId>
<artifactId>mocapi-resources</artifactId>
<version>0.0.1-SNAPSHOT</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright © 2025 Callibrity, Inc. ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.callibrity.mocapi.autoconfigure.resources;

import com.callibrity.mocapi.autoconfigure.MocapiAutoConfiguration;
import com.callibrity.mocapi.resources.McpResource;
import com.callibrity.mocapi.resources.McpResourceProvider;
import com.callibrity.mocapi.resources.McpResourcesCapability;
import com.callibrity.mocapi.resources.annotation.AnnotationMcpResourceProviderFactory;
import com.callibrity.mocapi.resources.annotation.DefaultAnnotationMcpResourceProviderFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import java.util.List;

@AutoConfiguration
@AutoConfigureBefore(MocapiAutoConfiguration.class)
@ConditionalOnClass(McpResourcesCapability.class)
@EnableConfigurationProperties(MocapiResourcesProperties.class)
@PropertySource("classpath:mocapi-resources-defaults.properties")
@RequiredArgsConstructor
public class MocapiResourcesAutoConfiguration {

private final MocapiResourcesProperties props;

@Bean
@ConditionalOnProperty(prefix = "mocapi.resources", name = "enabled", havingValue = "true", matchIfMissing = true)
public McpResourcesCapability mcpResourcesCapability(List<McpResourceProvider> resourceProviders) {
return new McpResourcesCapability(resourceProviders);
}

@Bean
public McpResourceProvider mcpResourceBeansProvider(List<McpResource> beans) {
return () -> List.copyOf(beans);
}

@Bean
public AnnotationMcpResourceProviderFactory annotationMcpResourceProviderFactory() {
return new DefaultAnnotationMcpResourceProviderFactory();
}

@Bean
public ResourceServiceMcpResourceProvider resourceServiceMcpResourceProvider(ApplicationContext context) {
return new ResourceServiceMcpResourceProvider(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright © 2025 Callibrity, Inc. ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.callibrity.mocapi.autoconfigure.resources;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "mocapi.resources")
public class MocapiResourcesProperties {
private boolean enabled = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright © 2025 Callibrity, Inc. ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.callibrity.mocapi.autoconfigure.resources;

import com.callibrity.mocapi.resources.McpResource;
import com.callibrity.mocapi.resources.McpResourceProvider;
import com.callibrity.mocapi.resources.annotation.AnnotationMcpResource;
import com.callibrity.mocapi.resources.annotation.ResourceService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;

import java.util.List;

@RequiredArgsConstructor
public class ResourceServiceMcpResourceProvider implements McpResourceProvider {

private final ApplicationContext context;
private List<AnnotationMcpResource> resources = List.of();

@Override
public List<McpResource> getMcpResources() {
return List.copyOf(resources);
}

@PostConstruct
public void initialize() {
resources = context.getBeansWithAnnotation(ResourceService.class).values().stream()
.flatMap(bean -> AnnotationMcpResource.createResources(bean).stream())
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
com.callibrity.mocapi.autoconfigure.tools.MocapiToolsAutoConfiguration
com.callibrity.mocapi.autoconfigure.prompts.MocapiPromptsAutoConfiguration
com.callibrity.mocapi.autoconfigure.MocapiAutoConfiguration
com.callibrity.mocapi.autoconfigure.resources.MocapiResourcesAutoConfiguration
com.callibrity.mocapi.autoconfigure.MocapiAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Mocapi Resources Configuration Defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright © 2025 Callibrity, Inc. ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.callibrity.mocapi.autoconfigure.resources;

import com.callibrity.mocapi.resources.McpResourceProvider;
import com.callibrity.mocapi.resources.McpResourcesCapability;
import com.callibrity.mocapi.resources.ReadResourceResult;
import com.callibrity.mocapi.resources.annotation.AnnotationMcpResourceProviderFactory;
import com.callibrity.mocapi.resources.annotation.Resource;
import com.callibrity.mocapi.resources.annotation.ResourceService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;

class MocapiResourcesAutoConfigurationTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(MocapiResourcesAutoConfiguration.class));

@Test
void shouldAutoConfigureResourcesCapability() {
contextRunner.run(context -> {
assertThat(context).hasSingleBean(McpResourcesCapability.class);
assertThat(context).hasSingleBean(MocapiResourcesProperties.class);
});
}

@Test
void shouldAutoConfigureResourceServiceProvider() {
contextRunner
.withUserConfiguration(TestResourceServiceConfiguration.class)
.run(context -> {
assertThat(context).hasSingleBean(ResourceServiceMcpResourceProvider.class);
var provider = context.getBean(ResourceServiceMcpResourceProvider.class);
assertThat(provider.getMcpResources()).hasSize(1);
});
}

@Test
void shouldConfigureResourceProviders() {
contextRunner
.withUserConfiguration(TestResourceProviderConfiguration.class)
.run(context -> {
var capability = context.getBean(McpResourcesCapability.class);
var response = capability.listResources(null);
assertThat(response.resources()).hasSize(1);
assertThat(response.resources().get(0).name()).isEqualTo("Test Resource");
});
}

@Test
void shouldDisableWhenPropertySet() {
contextRunner
.withPropertyValues("mocapi.resources.enabled=false")
.run(context -> {
assertThat(context).doesNotHaveBean(McpResourcesCapability.class);
});
}

@Test
void shouldConfigureMcpResourceBeansProvider() {
contextRunner
.withUserConfiguration(TestResourceBeanConfiguration.class)
.run(context -> {
var providers = context.getBeansOfType(McpResourceProvider.class);
assertThat(providers).hasSize(2);
assertThat(providers).containsKeys("mcpResourceBeansProvider", "resourceServiceMcpResourceProvider");
});
}

@Test
void shouldConfigureAnnotationMcpResourceProviderFactory() {
contextRunner.run(context -> {
assertThat(context).hasSingleBean(AnnotationMcpResourceProviderFactory.class);
var factory = context.getBean(AnnotationMcpResourceProviderFactory.class);
assertThat(factory).isNotNull();
});
}

@Test
void shouldConfigureResourceServiceMcpResourceProvider() {
contextRunner.run(context -> {
assertThat(context).hasSingleBean(ResourceServiceMcpResourceProvider.class);
var provider = context.getBean(ResourceServiceMcpResourceProvider.class);
assertThat(provider).isNotNull();
});
}

@Test
void shouldConfigurePropertiesBean() {
contextRunner.run(context -> {
assertThat(context).hasSingleBean(MocapiResourcesProperties.class);
var properties = context.getBean(MocapiResourcesProperties.class);
assertThat(properties.isEnabled()).isTrue();
});
}

@Test
void shouldConfigurePropertiesWithCustomValue() {
contextRunner
.withPropertyValues("mocapi.resources.enabled=false")
.run(context -> {
assertThat(context).hasSingleBean(MocapiResourcesProperties.class);
var properties = context.getBean(MocapiResourcesProperties.class);
assertThat(properties.isEnabled()).isFalse();
});
}

@Configuration
static class TestResourceServiceConfiguration {
@Bean
public TestResourceService testResourceService() {
return new TestResourceService();
}
}

@Configuration
static class TestResourceProviderConfiguration {
@Bean
public McpResourceProvider testResourceProvider() {
return () -> java.util.List.of(new TestMcpResource());
}
}

@Configuration
static class TestResourceBeanConfiguration {
@Bean
public TestMcpResource testMcpResource() {
return new TestMcpResource();
}
}

@ResourceService
static class TestResourceService {
@Resource(name = "Test Resource")
public ReadResourceResult getTestResource() {
return ReadResourceResult.text("test content", "text/plain");
}
}

static class TestMcpResource implements com.callibrity.mocapi.resources.McpResource {
@Override
public String uri() { return "test://resource"; }
@Override
public String name() { return "Test Resource"; }
@Override
public String title() { return "Test Resource"; }
@Override
public String description() { return "A test resource"; }
@Override
public String mimeType() { return "text/plain"; }
@Override
public ReadResourceResult read(java.util.Map<String, String> parameters) {
return ReadResourceResult.text("test content", "text/plain");
}
}
}
Loading