find_project_by_name
Search for WeWork projects by name with customizable similarity threshold to retrieve the most relevant project details from the MCP server.
Instructions
Tìm dự án theo tên với độ tương đồng
Args:
project_name: Tên dự án cần tìm
threshold: Ngưỡng tương đồng tối thiểu (default: 0.3)
Returns:
Thông tin dự án phù hợp nhất
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_name | Yes | ||
| threshold | No |
Implementation Reference
- wework_mcp_server.py:204-254 (handler)The main handler function for the 'find_project_by_name' tool, decorated with @mcp.tool() for automatic registration and schema inference. It fetches all projects and uses similarity matching to find the best match.@mcp.tool() def find_project_by_name(project_name: str, threshold: float = 0.3) -> Dict[str, Any]: """ Tìm dự án theo tên với độ tương đồng Args: project_name: Tên dự án cần tìm threshold: Ngưỡng tương đồng tối thiểu (default: 0.3) Returns: Thông tin dự án phù hợp nhất """ try: if not wework_client: return {'error': 'WeWork client not initialized'} logger.info(f"Finding project by name: {project_name}") projects = wework_client.fetch_projects() if not projects: return { 'error': 'No projects available or failed to fetch projects', 'success': False } best_project, similarity_score = wework_client.find_best_project_match( project_name, projects, threshold ) if best_project: return { 'success': True, 'found': True, 'project': best_project, 'similarity_score': similarity_score, 'search_term': project_name } else: return { 'success': True, 'found': False, 'similarity_score': similarity_score, 'search_term': project_name, 'message': f'Không tìm thấy dự án phù hợp với "{project_name}" (ngưỡng: {threshold})', 'available_projects': [p.get('name', 'Unknown') for p in projects[:5]] # Show first 5 for reference } except Exception as e: logger.error(f"Error in find_project_by_name: {e}") return {'error': str(e), 'success': False}
- wework_mcp_server.py:205-215 (schema)Input/output schema defined by type hints (project_name: str, threshold: float=0.3 -> Dict[str, Any]) and docstring describing parameters and return value.def find_project_by_name(project_name: str, threshold: float = 0.3) -> Dict[str, Any]: """ Tìm dự án theo tên với độ tương đồng Args: project_name: Tên dự án cần tìm threshold: Ngưỡng tương đồng tối thiểu (default: 0.3) Returns: Thông tin dự án phù hợp nhất """
- data/wework_client.py:212-269 (helper)Supporting utility method in WeWorkClient that implements the fuzzy matching logic using TF-IDF cosine similarity (sklearn) or fallback set-based string similarity.def find_best_project_match(self, target_name: str, projects: List[Dict], threshold: float = 0.3) -> Tuple[Optional[Dict], float]: """ Tìm dự án phù hợp nhất bằng cosine similarity (nếu sklearn có sẵn) hoặc simple string matching """ if not projects: return None, 0 # Tạo danh sách tên dự án project_names = [project['name'].lower() for project in projects] target_name_lower = target_name.lower() # Kiểm tra khớp chính xác trước for i, name in enumerate(project_names): if target_name_lower in name or name in target_name_lower: return projects[i], 1.0 # Sử dụng TF-IDF và cosine similarity nếu sklearn có sẵn if SKLEARN_AVAILABLE: try: vectorizer = TfidfVectorizer(analyzer='char', ngram_range=(2, 3)) all_names = project_names + [target_name_lower] tfidf_matrix = vectorizer.fit_transform(all_names) # Tính cosine similarity giữa target và tất cả project names target_vector = tfidf_matrix[-1] project_vectors = tfidf_matrix[:-1] similarities = cosine_similarity(target_vector, project_vectors).flatten() # Tìm similarity cao nhất best_idx = np.argmax(similarities) best_similarity = similarities[best_idx] if best_similarity >= threshold: return projects[best_idx], best_similarity else: return None, best_similarity except Exception as e: print(f"Lỗi khi tính cosine similarity: {e}") # Fallback: Simple string similarity best_match = None best_score = 0 for project in projects: name_lower = project['name'].lower() # Simple similarity based on common characters common_chars = len(set(target_name_lower) & set(name_lower)) total_chars = len(set(target_name_lower) | set(name_lower)) similarity = common_chars / total_chars if total_chars > 0 else 0 if similarity > best_score and similarity >= threshold: best_match = project best_score = similarity return best_match, best_score