"""
Example: Basic Notepad++ automation with notepadpp-mcp
"""
import asyncio
import logging
from notepadpp_mcp.tools.server import (
find_text,
get_current_file_info,
get_status,
insert_text,
new_file,
save_file,
)
# Configure structured logging for example
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Create console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# Create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
console_handler.setFormatter(formatter)
# Add handler to logger
logger.addHandler(console_handler)
async def basic_file_operations() -> str:
"""Demonstrate basic file operations."""
logger.info("=== Basic File Operations Example ===")
# Check Notepad++ status
logger.info("1. Checking Notepad++ status...")
status = await get_status()
logger.info(f"Status: {status}")
# Create new file
logger.info("2. Creating new file...")
result = await new_file()
logger.info(f"New file result: {result}")
# Insert some text
logger.info("\n3. Inserting text...")
sample_text = """# Sample Document
This is a sample document created by notepadpp-mcp.
Features demonstrated:
- File creation
- Text insertion
- File saving
- Search functionality
Generated by: Notepad++ MCP Server
"""
result = await insert_text(sample_text)
logger.info(f"Insert text result: {result}")
# Save file
logger.info("\n4. Saving file...")
import tempfile
temp_file = tempfile.mktemp(suffix=".md")
result = await save_file()
logger.info(f"Save file result: {result}")
# Note: To save to a specific path, you would need to:
# 1. Use File > Save As in Notepad++ manually, or
# 2. Copy the file to the desired location after saving
logger.info(
f"File saved to current location. To save to specific path: {temp_file}"
)
# Get file info
logger.info("\n5. Getting current file info...")
file_info = await get_current_file_info()
logger.info(f"File info: {file_info}")
logger.info("\n=== Basic operations completed! ===")
return temp_file
async def search_operations(file_path: str) -> None:
"""Demonstrate search operations."""
logger.info("\n=== Search Operations Example ===")
# Search for text
logger.info("\n1. Searching for 'Features'...")
result = await find_text("Features", case_sensitive=False)
logger.info(f"Search result: {result}")
logger.info("\n2. Searching for 'mcp' (case sensitive)...")
result = await find_text("mcp", case_sensitive=True)
logger.info(f"Search result: {result}")
logger.info("\n=== Search operations completed! ===")
async def main() -> None:
"""Main example function."""
try:
# Run basic operations
temp_file = await basic_file_operations()
# Wait a moment
await asyncio.sleep(1)
# Run search operations
await search_operations(temp_file)
logger.info(f"\nExample completed! Temporary file created: {temp_file}")
logger.info("You can delete this file manually if needed.")
except Exception as e:
logger.info(f"Error during example: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
print("Notepad++ MCP Server - Basic Example")
print("=====================================")
print("This example demonstrates basic functionality.")
print("Make sure Notepad++ is installed and the MCP server is running.")
print()
asyncio.run(main())